+ 2

Please help me in this code!

Simultanious getline (cin, string) not working. I want a string, a integer and again a string as input ( in same sequence) as separate inputs i. e. on different lines. But second getline function is not showing output(probably not accepting input). But if you change sequence, i.e. use two getline next to each other it works fine. Please guide me what should I do and why this is happening? #include <iostream> #include <string> #include <cstring> using namespace std; int main() { string myName, otherName; int num; getline(cin, myName); cin >> num; getline(cin, otherName); cout << myName << "\n" << num << "\n"<< otherName; return 0; } Sample Input: ABC 5 XYZ Expected output: ABC 5 XYZ Actual Output: ABC 5 https://code.sololearn.com/c7R9gt4k6SpY/?ref=app

9th Apr 2020, 4:34 AM
Laukik Shah
Laukik Shah - avatar
2 ответов
+ 6
You need cin.ignore() between two inputs:because you need to flush the newline character out of the buffer in between. Here👇 https://code.sololearn.com/cs0OyRp31K09/?ref=app
9th Apr 2020, 5:00 AM
Arsenic
Arsenic - avatar
+ 1
Thanks for the help:) This works👍
9th Apr 2020, 5:25 AM
Laukik Shah
Laukik Shah - avatar