+ 3
C++ Question
Consider the following code: #include <iostream> int main() { int nums[9]; for( int i = 0; i < 9; i++ ) { std::cin >> nums[i]; std::cout << nums[i] << '\n'; } return 0; } When I run the code, it first asks to input all the 9 elements and then, it prints the message nine times, each seperated by a new line. But if we think with respect to the for loop then, the output should be: 1st input, print. 2nd input, print..... for each element of nums array. Then, why is it so?
3 Answers
+ 9
Yes , you are right.
If you run this code in a real ide i.e. in your computer , it will work as you said.
But Sololearn's code playground takes all input at the same time. So, it seams so.
+ 10
cout doesn't really mean print this variable..
it put the data in a stream buffer and the data will be streamed all together once.
To synchronize the stream buffer you can use std::flush
put this after the cout statement..
std::cout << std::flush;
+ 2
hit enter to add new input so you can have multiple things happen