问题
I need to count the number of an input character there is in an input sentence. I am so close however I keep getting this error:
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;
Can anyone lend a hand?
回答1:
There's no such thing as an empty character.
Just write
if (sentence == "")
{
cout << "Please enter a valid answer:\n";
break;
}
回答2:
Problems with this code:
1. C++ does not take empty chars : if(character == '')
2. The arguments from your function WordOccurrenceCount do not match your declaration.
3. sentence.begin() is of String_iterator type, cannot be converted to string. (As expected by your WordOccurrenceCount function)
4. Again, sentence.end is also of String_iterator type, cannot be converted to int (As expected by your function declaration) or string (as expected by your function definition).
回答3:
After counting (please mark erroneous lines somehow in the future) one of the problems was this line:
if(character == '' || sentence == "" )
In C++ (and C) you can't have empty character literals.
When you read the character and nothing is entered you get the newline, so the first check should be character == '\n'.
As for the string, there is a very simple method of checking if a string is empty: std::string::empty:
sentence.empty()
So the complete condition should be
if (character == '\n' || sentence.empty()) { ... }
As for the other errors, there are really multiple errors: To start with you declare WordOccurenceCount to take two arguments, a string and an integer. You then call it with three arguments, none of which are of the correct type.
Then in the definition of WordOccurenceCount you have different arguments compared to the declaration.
Finally, if you want to count the number of time a certain character is in a string, then you might want to look at the standard algorithms available in C++, especially std::count:
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);
来源:https://stackoverflow.com/questions/19929758/c-count-chars-in-a-string