Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;计算字符串中的字符数_C++_String_Character - Fatal编程技术网

C++ C++;计算字符串中的字符数

C++ C++;计算字符串中的字符数,c++,string,character,C++,String,Character,我需要计算输入句子中输入字符的数量。我是如此接近,但我不断得到这个错误: countchar.cpp:19:19: error: empty character constant countchar.cpp: In function â: countchar.cpp:26:75: error: could not convert â from â to â #include <string> #include <fstream> #include <iost

我需要计算输入句子中输入字符的数量。我是如此接近,但我不断得到这个错误:

countchar.cpp:19:19: error: empty character constant
countchar.cpp: In function â:
countchar.cpp:26:75: error: could not convert â from â to â



#include <string> 
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
void WordOccurenceCount(string, int);
int main()
{
    char character;
    string sentence;
    char answer;
    string cCount;
    while(1) {

                cout << "Enter a char to find out how many times it is in a sentence: ";                       
        cin >> character;
        cout << "Enter a sentence and to search for a specified character: ";
        cin >> sentence;
        if(character == '' || sentence == "" )
    {
            cout << "Please enter a valid answer:\n";
            break;

    }
    else {
        cCount = WordOccurenceCount(sentence.begin(), sentence.end(), character);
        cout << "Your sentence had" << cCount << character 
             << "character(s)"; 
     }

cout << "Do you wish to enter another sentence (y/n)?: ";
cin >> answer;
if (answer == 'n'){
    break;
    }
}
return 0;
}

int WordOccurrenceCount( string const & str, string const & word )
{
   int count;
   string::size_type word_pos( 0 );
   while ( word_pos!=string::npos )
   {
           word_pos = str.find(word, word_pos );
           if ( word_pos != string::npos )
           {
                   ++count;

     // start next search after this word 
                   word_pos += word.length();
           }
   }

   return count;
countchar.cpp:19:19:错误:空字符常量
countchar.cpp:在函数中–中:
countchar.cpp:26:75:错误:无法将-from-转换为-
#包括
#包括
#包括
#包括
使用名称空间std;
void wordoccurrencecount(字符串,int);
int main()
{
字符;
串句;
答案;
字符串帐户;
而(1){
性格;
cout>语句;
如果(字符=“”| |句子=“”)
{

cout没有空字符这回事

只要写

if (sentence == "")
{
        cout << "Please enter a valid answer:\n";
        break;
}
if(句子==“”)
{
cout计数后(请在将来以某种方式标记错误的行),其中一个问题是这行:

if(character == '' || sentence == "" )
<>在C++(和C)中,不能有空字符文字。

当您读取
字符时,没有输入任何内容,您将得到换行符,因此第一个检查应该是
字符=='\n'

对于字符串,有一种非常简单的方法可以检查字符串是否为空:

所以完整的条件应该是

if (character == '\n' || sentence.empty()) { ... }

至于其他错误,确实存在多个错误:首先声明
wordoccurrencecount
接受两个参数,一个字符串和一个整数。然后用三个参数调用它,但所有参数的类型都不正确

然后在
wordoccurrencecount
的定义中,与声明相比,您有不同的参数


最后,如果你想计算某个字符串中某个字符的时间,那么你可能需要查看C++中的可用性,尤其是:

std::字符串语句;
std::cout字符;
长计数=标准:计数(标准:开始(句子),标准:结束(句子),字符);
此代码的问题: 1、C++不带空字符:<代码>(字符==)< <代码> 2.函数
WordOccurrenceCount
中的参数与声明不匹配。 3.
语句.begin()
是字符串迭代器类型,无法转换为字符串。(正如您的
WordOccurrenceCount
函数所预期的那样)
4.同样,
语句.end
也是String\u迭代器类型,不能转换为int(如函数声明所预期)或String(如函数定义所预期)。

您的标题和问题是“count chars”,但您的代码看起来像是在计算单词?您是指“String”的数量吗在一些文本中?或者字符串中的字符数???@ArneMertz您发布的线程与java有关,而不是C++@stellarossa刚刚注意到的,谢谢。下面是其他:,@user2939276更新的回答当您将字符作为输入时,用户从不输入任何内容(我相信这就是为什么会出现这种if语句的原因)。即使用户不输入任何内容并按enter键,他仍然按enter键,这是一个字符。请尝试检查用户提供的字符是否为“\n”。
if (character == '\n' || sentence.empty()) { ... }
std::string sentence;
std::cout << "Enter a sentence: ";
std::getline(std::cin, sentence);

char character;
std::cout << "Enter a character to be found: ";
std::cin >> character;

long count = std::count(std::begin(sentence), std::end(sentence), character);