Let the user input integers and stop when input is 0, print out the second biggest number(excluding 0)
Nov 9, 2013 at 3:45pm UTC
I have come up with the code below but I don't know if I have covered all the cases, please help me with that. Also if you think you have a better solution then share it please (⊙ω⊙)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
using namespace std;
int main()
{
int n,max=0,max2=0,temp;
n=1;
while (n!=0)
{
if (max == 0)
max = n;
if (max2 == 0)
max2 = n;
if (n>max)
{
temp = max;
max = n;
max2 = temp;
}
cout<<"Input a number, or input 0 to stop: " ;
cin>>n;
}
if (max == max2)
cout<<"ERROR" <<endl;
else
cout<<"Second biggest: " <<max2<<endl;
system("pause" );
return 0;
}
Thank you
Topic archived. No new replies allowed.