Nov 21, 2011 at 4:56pm UTC
Hi guys,
I got this code, but dont understand why its not working.
It has something to do with cin and getline, but i dont see it..
Could you help me?
This is the code:
int number;
string street_nr, location_name;
cout << "Give number: " ;
cin >> number;
cout << "Street name+housenr :" ;
getline (cin, street_nr) ;
cout << "location: ";
getline (cin, location_name);
This is the result:
Give number: 1234
Street name+housenr : Location : _
What happens? :(
Nov 21, 2011 at 5:21pm UTC
This should work;
int number;
string street_nr, location_name;
cout << "Give number: " ;
cin>>number;
cin.get();
cout << "Street name+housenr :" ;
getline (cin, street_nr) ;
cout << "location: ";
getline (cin, location_name);
problem was that when you enter the integer "number" and press "enter", the program reads the newline as an input to the cin.getline() and that's the reason it looks like it has skipped it. Hope that helps!
Nov 21, 2011 at 5:23pm UTC
Thanks for response.
Im going to try it :D
Nov 21, 2011 at 6:02pm UTC
What was nice?
This code doesnt give me any result at all :(.
It just "process returned" after inputting number, street name+housenr and location..
Nov 21, 2011 at 6:36pm UTC
Its semi-working now :D
Thanks