So when I try to input the required inputs for program I want user to enter them one by one. And when I enter the both of them at once with a space between them like:
5 10
It directly goes to the second cin's end.
#include <iostream>
using namespace std;
int main() {
int asd1, asd2;
cout << "Enter the first one: ";
cin >> asd1;
cout << endl;
if (asd1 == 5) {
cout << "First one is 5" << endl;
}
cout << "Enter the second one: ";
cin >> asd2;
cout << endl;
if (asd2 == 10) {
cout << "Second one is 10" << endl;
}
}
And when I enter two inputs together it outputs with a ugly way which is why I'm asking this.
Output:
Enter the first one: 5 10
First one is 5
Enter the second one: <<<< Here it doesn't cin.
Second one is 10
I tried using cin.get() but didn't really work.
The first read statement:
cin >> asd1reads5from the buffer, but leaves10in that buffer. The second read statement:cin >> asd2is able to immediately read10from the buffer without any further action being necessary.If you wish to ignore the rest of that
5 10input, you need to read the entire line into a string usingstd::get line, then parse the firstintout and assign that toasd1andasd2.For instance, you might write a function to do this. Bear in mind this has zero error checking.
At which point you could just write: