Problem with getline

This program is for counting words in a string. It would work fine if I could get the second getline function inside of my while loop. For some reason it skips over it.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>

using namespace std;


string counter(string a);
int main()
{
    
    char answer;
    string s;
    string t;
    cout <<"Please enter a sentence." << endl;
    getline(cin,s);
    counter(s);
    cout << "Would you like to run this again?" << endl;
    //Removed the cin line and made it into a getchar(). 
    //Also made the loop from a while into a do while.
   
    do
      {
       answer=getchar();
       cout << "Please enter a sentence." << endl;
       getline(cin,t);//Doesn't want to run getline in the while loop.
       cout << "\n\n\n\n\nchecking value...:"<< t << "\n\n\n\n";
       system("pause");
       counter(t);
       cout << "Would you like to run this again?" << endl;
       answer=getchar();
       
      
      }while(toupper(answer) == 'Y');
        system("EXIT_SUCCESS");
        return 0;   
}
string counter(string a)
{
    int count = 1; 
    for(int i = 0; i < a.length(); i++)
        {
            if(a.substr(i,1) == " ")    
            {count++;}                           
        }
    system("cls");
    cout <<"There are " << count << " word(s) in this s \""<< a << "\"\n\n\n";    
    a.clear();//Empties the string.    
    cout <<"Sentence Deleted\n\n" << a << endl;//Makes sure it is emptied. 
    return a; 
}
Last edited on
Dont mix getline() and cin>> up. In fact, it's best to always use getline(): http://www.cplusplus.com/forum/articles/6046/
Alright, I changed it from a cin >> into a getchar. That still didn't solve the problem with the do while where I am trying to get input for string t.
getchar() has the same problem as cin>>. The last character (enter) isn't removed from the input-buffer. This doesn't give problems when using just cin>> and similar functions, but if you use getline() after it, the last character (enter) is read and the program seems to skip over the whole command.

The best solution is to always use getline(). In that article I posted is explained how to use getline() to get strings, chars and numbers.

Aldo a do/while-loop is better in this case, because the code needs to be executed at least once, but hasn't anything to do with your problem.
Using that method to get a char for something this simple is more than I want to do. Instead I just used a string with getline and that did the trick.

1
2
3
 
getline(cin,s);     
}while(s == "Y");


Thank you very much for getting me on the right track!
Glad you solved it :)

I agree it's a lot work for something quit simple, I normally use functions to get the input. And indeed, most things that can be done with a char can be done with a string to.
Topic archived. No new replies allowed.