C++ Question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

15th Mar 2018, 2:44 PM
777
777 - avatar
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.
15th Mar 2018, 3:06 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 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;
15th Mar 2018, 4:04 PM
AZTECCO
AZTECCO - avatar
+ 2
hit enter to add new input so you can have multiple things happen
16th Mar 2018, 8:56 PM
Levi Morrill
Levi Morrill - avatar