Reputation: 21
In GNOME terminal and XTerm in Ubuntu, I'm facing this problem:
I am forced to input values for all cin
statements, irrespective of where they appear in source code, and only at the end executes all the cout
statements. For instance:
int main()
{
int a;
cout<<"Enter a :";
cin>>a;
cout<<"\n";
return 0;
}
When I run this code (using g++), I am forced to to input the value for a
before the first cout
statement runs.
kanishk509@kanishk509-hp:~/Hackerearth$ g++ -Wall -o sample sample.cpp
kanishk509@kanishk509-hp:~/Hackerearth$ ./sample
5
Enter a :
The '5' is the input that I am forced to provide to the statement cin>>a
before the any cout
statement runs.
Upvotes: 1
Views: 2095
Reputation: 21
Using std::flush
solved the problem.
int main()
{
int a;
cout<<"Enter a :"<<flush;
cin>>a;
cout<<"\n";
return 0;
}
kanishk509@kanishk509-hp:~/Hackerearth$ g++ -Wall -o sample sample.cpp
kanishk509@kanishk509-hp:~/Hackerearth$ ./sample
Enter a :5
Upvotes: 1