Understanding Cin and Cout Order in C++

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The discussion clarifies the order of operations between the output stream (cout) and the input stream (cin) in C++. It establishes that cout is buffered, meaning output may not appear immediately until the buffer is flushed, which can be done using endl or flush. The getline function is explained as a method to read input until the Enter key is pressed, storing the result in a specified string variable. The participants confirm that while cin can be used independently, it is common practice to prompt the user with cout before reading input.

PREREQUISITES
  • Understanding of C++ input/output streams (cin and cout)
  • Familiarity with C++ buffering concepts
  • Knowledge of the getline function in C++
  • Basic syntax of C++ for reading and writing data
NEXT STEPS
  • Explore C++ stream buffering and flushing techniques
  • Learn about the differences between cin and getline in C++
  • Investigate best practices for user input prompts in C++ applications
  • Study error handling techniques for input operations in C++
USEFUL FOR

C++ developers, programming students, and anyone looking to understand input/output operations in C++ applications.

ineedhelpnow
Messages
649
Reaction score
0
why does cin come AFTER count and not before?
 
Technology news on Phys.org
ineedhelpnow said:
why does cin come AFTER count and not before?

If you print something with count, it is buffered before being shown.
Sometimes you need to flush count's buffer.
Either [m]flush[/m] or [m]endl[/m] will flush count'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.
 
count << "whatever the heck" << usernum << " is.";
cin >> usernum

why do you do cin after count

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

why do you do cin after count

cant you just do cin >> s?

It's normal to print a question before reading input.
Typically you'd do
[m]count << "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 7 ·
Replies
7
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K