[try Beta version]
Not logged in

 
 
string.getline() doesn't work

Mar 1, 2013 at 4:47pm
I wrote:

1
2
string test;
getline(cin, test);


I should be able to put into the string "test" a whole line of input, am I right?

However, it doesn't even starts the input... Any help?
Mar 1, 2013 at 5:17pm
There is nothing wrong with the snippet you posted. You need to provide more content.

Mar 1, 2013 at 8:59pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string test;
        
cout << "Insert string" << endl;
        
while (1) {
            
    getline(cin, test);
            
    if (test == "stop") {
        break;
    }
            
    /*...*/
}
        
/*...*/


This is my real code, sorry. Well, it doesn't start the input.
Mar 1, 2013 at 10:01pm
Can you post the output you get when you compile it?
Mar 2, 2013 at 5:06pm
The output is only the "cout" output.

Insert string:
Mar 2, 2013 at 5:44pm
Remember you need to press [enter] after you have completed your input.

I've a feeling you wanted the input to be recognised as completed when you press some other key. If that's what you want, you'd need to monitor individual keystrokes, instead of simply using cin.
Mar 2, 2013 at 6:09pm
fpiro i tried your code in VS2012 and it works perfectly.
Mar 3, 2013 at 1:39am
Still the same problem. I mixed up the extraction operator ">>" with the getline function. Solved! √
Topic archived. No new replies allowed.