Jump to content
  • Advertisement
Sign in to follow this  
FadedMaximus

Strings

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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>.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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!

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!