0

I've tried to use std::getline() in my program twice, but it doesn't work the second time I use it. Can someone help?

string bufName; //Made a string for Name
int age;  //Age will be entered in form of integer, if not the program won't crash
float temperature; // it's in float format because it can have a lot of decimal points
string bufCity; // Name of the city can be two words like San Francisco so we used Buf for it
    
cout << "Enter your Name please: " << endl;
getline(cin, bufName);  // Using Get line to read multiple String in a line
cout << "you Entered: " << bufName << endl;
    
cout << "Enter your Age: " << endl;
cin >> age ;
cout << "you Entered: " << age << endl;
    
cout << "Enter the Temperature at your city please: " << endl;
cin >> temperature ;
cout << "you entered: " << temperature << endl;
    
cout << "Enter your Name please: " << endl;
getline(cin, bufCity);  // Using Get line to read multiple String in a line
cout << "you Entered: " << bufCity << endl;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ali A.
  • 27
  • 1
  • Use cin.ignore() before getline(cin, bufCity). This will ignore \n that cin >> age left if you pressed enter. – Hossein Sep 04 '21 at 18:33
  • @NI3SSOH you mean `temperature`, not `age`. The reading of `temperature` will discard the `\n` that was left behind by the reading of `age` – Remy Lebeau Sep 04 '21 at 19:50

0 Answers0