0

Why did it ask for input only once and not 5 times?

#include <iostream> using namespace std; int main() { int myArr[5]; for(int x=0; x<5; x++) { cin >> myArr[x]; cout << x << ": " << myArr[x] << endl; } return 0; } /* Why did it ask for input only once and not 5 times and my input was 10, but output: 0: 10 1: 0 2: 3 3: 0 4: 37 Modified code on: https://code.sololearn.com/229/#cpp */

19th Aug 2018, 8:42 PM
Dmitry D
3 Réponses
+ 2
When you use cin the program doesn't ask for input. Instead, it continues reading from the stdin file. On a computer this is usually mapped to a command line the operating system manages. When it attempts to read, if you haven't written anything, it blocks the application, and waits for your input. On Sololearn stdin is mapped to the input you provide at the beginning. Try " 10 20 30 40 50 " and you should get the result you want.
19th Aug 2018, 8:57 PM
Vlad Serbu
Vlad Serbu - avatar
+ 1
In Sololearn this does not work, because all required input is processed and the final result is returned. No intermediate step, maybe this would require too many ressources because of waiting time, I don't know exactly. Only for web code it works. I also find this pretty unfortunate in a leaening app :/ For multiple inputs rather try the code on PC. If you send only one input, the rest is random garbage and does not mean anything.
19th Aug 2018, 8:58 PM
Matthias
Matthias - avatar
0
Thanks guys.
20th Aug 2018, 1:29 AM
Dmitry D