How do inputs work in C++? [Solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How do inputs work in C++? [Solved]

When I enter inputs as 1 2 Abhay1 Abhay2 I get the following output, 1 2 2 2 Abhay1 Instead of 1 2 2 Abhay1 2 Abhay2 https://code.sololearn.com/cZ6E7P3Qt44r/?ref=app

12th Sep 2020, 9:35 PM
Abhay
Abhay - avatar
2 Answers
+ 8
std::getline will consume the newline character which is registered in your input stream by std::cin (when you press enter). This is why a newline is being output instead of "Abhay2". You can use std::cin.ignore() after the last std::cin call, prior to entering the inner loop, to discard the newline in the input stream. std::cout<<"Enter the number of lines follwing particular case"; std::cin>>N; std::cin.ignore(); // ... http://www.cplusplus.com/reference/istream/istream/ignore/
12th Sep 2020, 10:26 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Hatsy Rei ty very much!
12th Sep 2020, 10:42 PM
Abhay
Abhay - avatar