C/C++ Understanding Cin and Cout Order in C++

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion centers on the sequence of using cout and cin in C++ programming, specifically why cin typically follows cout. It highlights that cout is buffered, meaning output may not appear immediately unless the buffer is flushed, which can be done using flush or endl. The participants clarify that cin statements do not produce visible output, as they are used for input rather than displaying information. The getline function is explained as a way to read input until the Enter key is pressed, storing the result in a specified string. The conversation emphasizes that it is common practice to prompt the user with cout before using cin to read input, reinforcing the logical flow of user interaction in programming.
ineedhelpnow
Messages
649
Reaction score
0
why does cin come AFTER cout and not before?
 
Technology news on Phys.org
ineedhelpnow said:
why does cin come AFTER cout and not before?

If you print something with cout, it is buffered before being shown.
Sometimes you need to flush cout's buffer.
Either [m]flush[/m] or [m]endl[/m] will flush cout's buffer so you can see it.
 
i don't understand. you can't SEE a cin statement though.

also what is a getline statement like getline (cin, s)
 
ineedhelpnow said:
i don't understand. you can't SEE a cin statement though.

Maybe I misunderstood.
What is your question? (Wondering)

also what is a getline statement like getline (cin, s)

It gets input until Enter is pressed.
The result is stored in the string s.
 
cout << "whatever the heck" << usernum << " is.";
cin >> usernum

why do you do cin after cout

cant you just do cin >> s?
 
ineedhelpnow said:
cout << "whatever the heck" << usernum << " is.";
cin >> usernum

why do you do cin after cout

cant you just do cin >> s?

It's normal to print a question before reading input.
Typically you'd do
[m]cout << "Type a usernum: ";
cin >> usernum;[/m]
 
oh i see. so for the example i gave cin was not necessary?
 
ineedhelpnow said:
oh i see. so for the example i gave cin was not necessary?

If you only want to print the current value of [m]usernum[/m], there's no point in reading it afterwards.
 
Back
Top