Understanding Cin and Cout Order in C++

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++
In summary: It is more efficient to just use cout to print the value directly.In summary, the conversation discusses the order of using cout and cin in a program. It is explained that cout is used before cin because it buffers the output before displaying it, and sometimes cin's buffer needs to be flushed. The purpose of getline(cin, s) is to get input until the Enter key is pressed, and it is stored in the string s. The conversation also mentions that it is common to print a question before reading input, but if only the current value needs to be printed, it is more efficient to use cout instead of reading it with cin.
  • #1
ineedhelpnow
651
0
why does cin come AFTER cout and not before?
 
Technology news on Phys.org
  • #2
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.
 
  • #3
i don't understand. you can't SEE a cin statement though.

also what is a getline statement like getline (cin, s)
 
  • #4
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.
 
  • #5
cout << "whatever the heck" << usernum << " is.";
cin >> usernum

why do you do cin after cout

cant you just do cin >> s?
 
  • #6
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]
 
  • #7
oh i see. so for the example i gave cin was not necessary?
 
  • #8
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.
 

FAQ: Understanding Cin and Cout Order in C++

1. What is the purpose of using cin and cout in C++?

The cin and cout functions in C++ are used for input and output operations, respectively. They allow the user to interact with the program by providing input and receiving output from the program.

2. What is the difference between cin and cout in C++?

Cin is used for input operations, while cout is used for output operations. Cin can be used to read data from the user, while cout can be used to display data to the user. Additionally, cin is used with the extraction operator (>>) and cout is used with the insertion operator (<<).

3. In what order do cin and cout statements execute in C++?

Cin and cout statements are executed in the order in which they are written in the code. This means that cin statements will be executed before cout statements. This order is known as sequence or execution order.

4. How can I change the order of execution for cin and cout statements in C++?

The order of execution for cin and cout statements can be changed by using the manipulators endl or flush. Endl manipulator ends the current line and flushes the output buffer, while flush manipulator just flushes the output buffer without ending the line.

5. Can I use cin and cout in the same statement in C++?

Yes, cin and cout can be used in the same statement in C++. For example, you can use the insertion operator (<<) with cin to prompt the user for input, and then use the extraction operator (>>) with cout to display the input. However, it is recommended to use separate statements for better readability and debugging.

Similar threads

Back
Top