Hey there, so I am new to this forum and to C++ it is my first year in computer science and I am having some problems with this assignment, which is easy enough but I have hit a hitch and would appreciate if someone could point me in the right direction.

"Write a program that reads a line of text from the input, then prompts
for a character, and reports the number of occurrences of the character in the text."

#include <iostream>
#include <string>
using namespace std;

int main()
{
   while(1)
   {
      string text;
      char letter;
      string exit = "ZZZ";
      int count =0;

      cout<<"Please enter a line of text (ZZZ to Quit):";
      getline(cin,text);
      if (text == exit)
      {
         cout<<"Goodbye!"<<endl;
         break;
      }
      cout<<"please enter a charecter: ";
      cin>>letter;

   //Calculations and Output                                                                                                                                                      
      for (int i=0; i < text.length(); i++)
      {
         if (text[i] == letter);
         count++;        
      }

      if (count == 1)
         cout<<"There is 1 occurance of "<<letter<<" in the text."<<endl;
      else
         cout<<"There are "<<count<<" occurances of "<<letter<<" in the text."<<endl;
   }
   return 0;
}

I know it is an easy assignment but I have run into two problems, the first being that the count that comes out regardless of what I enter as the character always comes out as the total size of the string. ie) when I enter frogs and ask for how many f's there are I get 5 not one and I really can't see where I have gone wrong.
My second issue is that this program is supposed to keep looping until ZZZ is entered but I can only input the string text once when it loops around it goes straight to enter a character.
Thanks in advance for any help
in the mean time I will keep playing around with it myself HOPING A GET THIS GOING!

Recommended Answers

All 9 Replies

Line 27.. the ; at the end of your if
remove that, and it may work

...I never cease to amaze myself. thanks that solved problem 1, but I still can't enter a new string when it loops back around.

You can use the string's find method.

I'm fixing it.. I hope you don't mind..

I'm fixing it.. I hope you don't mind..

I guess not... I know I still have a lot to learn :/

while(1)
	  {
		 
		  count=0;
		   cout<<"Please enter a line of text (Q to Quit):";
		   cin.ignore(100,'\n');
		   getline(cin,text);
		   if (text =="Q" || text== "q")
			{

Now, the dude who posted about the string's find method..
it works for me..

#include <iostream>
#include <string>
using namespace std;

int main()
{
	  string text;
      char letter;
      int count =0;
	  cout << "Press enter to begin...";
	  while(1)
	  { 
		   count=0;
		   cin.ignore(100,'\n');
		   cout<<"Please enter a line of text (Q to Quit):";
		   getline(cin,text);
		   if (text =="Q" || text== "q")
			{
				cout<<"Goodbye!"<<endl;
				break;
			 }

			cout<<"please enter a character: ";
			cin>>letter;
			
			for (unsigned int i=0; i < text.length(); i++)
      {
				if (text[i] == letter)
					count++;        
      }

			cout<<"There are "<<count<<" occurances of '"<<letter<<"' in the text."<<endl;
	  }
	  return 0;
}

Can I work for daniweb?

#include <iostream>
#include <string>
using namespace std;

int main()
{
	  string text;
      char letter;
      int count =0;
	  cout << "Press enter to begin...";
	  while(1)
	  { 
		   count=0;
		   cin.ignore(100,'\n');
		   cout<<"Please enter a line of text (Q to Quit):";
		   getline(cin,text);
		   if (text =="Q" || text== "q")
			{
				cout<<"Goodbye!"<<endl;
				break;
			 }

			cout<<"please enter a character: ";
			cin>>letter;
			
			for (unsigned int i=0; i < text.length(); i++)
      {
				if (text[i] == letter)
					count++;        
      }

			cout<<"There are "<<count<<" occurances of '"<<letter<<"' in the text."<<endl;
	  }
	  return 0;
}

alright thanks again turns out all I was really missing was the cin.ignore(100,'\n'); before my input prompt, it loops fine now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.