0

what's wrong with this code?? #include <iostream> using namespace std; int main() { int n; int max=0; while (n!=0) { cin>>n; if(n>max) max=n; } cout <<max; return 0; }

4th Oct 2016, 2:32 PM
Saptarshi Saha
Saptarshi Saha - avatar
5 Respuestas
+ 2
It's because n doesn't hold a value when the while loop begins. Set n to a non-zero value, such as 1 or -1, or alternatively you could use a do/while loop rather than a while loop.
4th Oct 2016, 2:40 PM
Cohen Creber
Cohen Creber - avatar
+ 1
if n isnt assigned any value then the compiler automatically assigns a garbage value
4th Oct 2016, 4:22 PM
Suhail Pappu
Suhail Pappu - avatar
0
set n to non zero
4th Oct 2016, 2:55 PM
Krishna
Krishna - avatar
0
n does not have any initial value assigned
4th Oct 2016, 4:04 PM
Nikhil Jaiswal
Nikhil Jaiswal - avatar
0
thanks guys
26th Oct 2016, 3:10 PM
Saptarshi Saha
Saptarshi Saha - avatar