How program takes input of 7 each time via cin?

I got code via link in a post at stackoverflow ( https://stackoverflow.com/a/10907207/3693431 ), but unable to understand how in the code, a fixed value of 7 is taken as input, while the code asks none.

Code is given by link at: https://ideone.com/OR4VZ via comment given to answer at: https://stackoverflow.com/a/10907207/3693431

Here, cin does not ask for input, and 'n' simply yields 7 each time the program is run
Last edited on
??? the code in the first link is fed a command line value (do you understand this?), 4 in the only example I see where they ran it, and other values in the shell script that I didn't take any time to study but presumably that script calls it with various values as needed for whatever they are doing.

the other one reads what the user typed in, and the user typed in 7. It isn't fixed, its an example test run with 7 as the input.



Last edited on
jonnin: thanks as in standard input had specified 7. But, have an additional issue. If specify no value in Standard input, then takes 32766 as input value? Why it takes the highest value of 32767 -1 = 32766.
are you modifying the code to do that? Variables in c++ need an initial value or you get whatever happens to be in that piece of memory at that moment in time, could be anything.
try
int whatever{42}; //the value is 42 now, until changed in your code.
instead of
int whatever; //could be anything
> But, have an additional issue. If specify no value in Standard input, then takes 32766 as input value?

You appear to be using an outdated compiler.
The current default behaviour on input failure is to set the value of n to zero if no int could be read from stdin.
(In legacy C++, the old value of of n would have been left unchanged)


Topic archived. No new replies allowed.