Please help me in this code! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

9th Apr 2020, 4:34 AM
Laukik Shah
Laukik Shah - avatar
2 Answers
+ 6
You need cin.ignore() between two inputs:because you need to flush the newline character out of the buffer in between. Here👇
9th Apr 2020, 5:00 AM
Arsenic - avatar
+ 1
Thanks for the help:) This works👍
9th Apr 2020, 5:25 AM
Laukik Shah
Laukik Shah - avatar