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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
Click For 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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
9K
  • · Replies 39 ·
2
Replies
39
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
2
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
4K
Replies
12
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K