void Menu::printE()
{
string sentence;
int nume = 0;
int numE = 0;
cout << "Please enter a sentence: ";
cin >> sentence;
cout << endl;
for (int i = 0; i < MAX; i++)
{
if (sentence == 'e')
{
nume++;
}
else if (sentence == 'E')
{
numE++;
}
}
cout << "In your sentence, there are " << nume << " e's and there are " << numE << " E's.";
cout << endl;
cout << sentence << endl;
system("pause");
}
Strings
In this function, I am trying to have the user input a sentence. After the user has inputted the sentence, I want it to count how many e's and E's are in the sentence. This function worked when a string was inputed without any spaces. I tried changing the code slighly and rather then having cin >> sentence, I used getline(cin, sentence). This also didn't work. Can somebody please help me so I will be able to have the user input a complete sentence and count the number of e's and E's that are in it. Thanks.
Use std::getline and std::count.
Example:
Example:
void Dummy(){ std::string InputBuffer; std::cout << "Input: " << std::flush; std::getline(std::cin, InputBuffer); if(InputBuffer.empty()) { std::cout << "Invalid input." << std::endl; return; } std::cout << "e: " << std::count(InputBuffer.begin(), InputBuffer.end(), 'e') << "\nE: " << std::count(InputBuffer.begin(), InputBuffer.end(), 'E') << std::endl;}
When I tried to compile the example you gave me, I get an error saying that count is not a member of std.
Quote:Original post by FadedMaximus
When I tried to compile the example you gave me, I get an error saying that count is not a member of std.
I think you need to #include <algorithm>.
Also it's always better to find your error before using an alternative. How did you use std::getline in your original code? Also what is MAX? The following works fine here:
BTW you should use iterators to iterate (std::string::const_iterator) through a string or any other std container for that matter.
#include <iostream>#include <ostream>#include <string>int main(){ std::string sentence; int nume = 0; int numE = 0; std::cout << "Please enter a sentence: "; std::getline( std::cin, sentence); for (unsigned int i = 0; i < sentence.length(); ++i) { if (sentence == 'e') { ++nume; } else if (sentence == 'E') { ++numE; } } std::cout << "\nIn your sentence, there are " << nume << " e's and there are " << numE << " E's.\n"; std::cout << sentence << std::endl; return 0;}
BTW you should use iterators to iterate (std::string::const_iterator) through a string or any other std container for that matter.
I used getline in the original code, but then I took it out and replace it with what I had previously, that would be why its not there. MAX was a const int set to 80 when I was trying to do it with c-style strings. I forget to change it back. The code that you posted CTar still doesn't seem to work for me.
Quote:Original post by FadedMaximus
I used getline in the original code, but then I took it out and replace it with what I had previously, that would be why its not there. MAX was a const int set to 80 when I was trying to do it with c-style strings. I forget to change it back. The code that you posted CTar still doesn't seem to work for me.
Define "doesn't work" [smile]
Are you getting compilation errors? Post them!
Are you getting linker errors? Post them!
Incorrect output / other problem? Elaborate!
When I compiled the code that CTar gave me it compiled fine and built fine. When I ran the program it takes the users input as a sentence, but after that it just sits there and doesn't display the # of e's and E's or doesn't display the sentence.
Quote:Original post by FadedMaximus
When I compiled the code that CTar gave me it compiled fine and built fine. When I ran the program it takes the users input as a sentence, but after that it just sits there and doesn't display the # of e's and E's or doesn't display the sentence.
Well, it compiled and ran just fine for me.
You could trace the flow of execution in a debugger, or you could add some print statements inside the counting loop...
Hard to imagine whats going wrong here.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement