Sign Finder

I'm coding a "sign finder", which lets you enter a sign (as an example a letter, an exclamation mark etc.) and then a sentence. After that it counts how many of those signs there are in the sentence. From the beginning it only counted how many a:s there were in a sentence, and that worked perfectly. But as soon as I wanted to make the program "universal" or whatever, it stopped working. This is what it outputs:

What sign do you want to test? B
Enter a sentence in English: You did not enter a proper sentence!

It is kind of right, I did not enter a proper sentence, but that was because it didn't even let me do it -.- Does anyone know how to fix this irritating error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

int main(){
    char sign;
    cout << "What sign do you want to test? "; cin >> sign;
    for(;;){
        string sentence;
        bool test = true;
        int x=0, counter=0;
        cout << "Enter a sentence in English: "; getline(cin, sentence);
        if(sentence.find(' ')==string::npos){
            cout << "You did not enter a proper sentence!";
            break;
        }
        while(test){
            if(sentence.find(sign, x)!=string::npos){
                counter++;
                x=sentence.find(sign, x)+1;
            }else{
                test=false;
            }
        }
        if(counter==1){
            cout << "Your sentence contained 1 " << sign << "!\n\n";
        }else{
            cout << "Your sentence contained " << counter << " " << sign << ":s!\n\n";
        }
    }
    return(0);
}
Last edited on
After this cin >> sign; there is a newline character '\n' remaining in the input buffer. The next getline() will pick up everything until the newline which is of course an empty string.

Get rid of the unwanted newline by adding cin.ignore(); after the cin >>sign;
Wow, you really know everything about this, do you? Thanks a lot :)
Topic archived. No new replies allowed.