Public Group

# Strings

This topic is 4408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.
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");
}



##### Share on other sites
Use std::getline and std::count.

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;}

##### Share on other sites
When I tried to compile the example you gave me, I get an error saying that count is not a member of std.

##### Share on other sites
std::count is in header <algorithm>, have you included that?

##### Share on other sites
Quote:
 Original post by FadedMaximusWhen 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>.

##### Share on other sites
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:
#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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by FadedMaximusI 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!

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by FadedMaximusWhen 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.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633695
• Total Posts
3013375
×