0
Can somebody explain me this code
int num = 1; int number; while (num <= 5) { cin >> number; num++; }
4 Answers
+ 3
first line you are declaring a variable of type integer and assigned a value of 1. (the counting)
2nd line, you are declaring another integer with no value assigned.
in the while loop,
you will be asked to enter number continuously until you num becomes 5. there will not be any output
+ 2
this is what happens,
first loop.
while (1<=5){
cin>>number //user enters a number
n++;} //1+1
while (2<=5){
cin>>number //user enters number
n++;} //2+1
.....
while (5<=5){
cin>>number //user enters a number
n++;} //now 5+1 is greater than 5 so loop stops
in the entire process the value of int number gets overwritten by the user entered value.
To see output,
use this Instead.
while(num<=5){
cin>>number;
cout<<number;
num++;}
+ 1
thanks broo
+ 1
NP