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!