Hi,
I have just Started selflearning C++.
Q is to find first 2 highest nos from 5 nos.with while loop.But condn is it should check ,that not any numbers r repeated(ie.each no. must be unique).If not error message.
I have completed the code ,by showing first 2 highest nos.also i can display the error message if any no. is repeated,but once the error message is displayed ,it doesnt quit ,it takes remaining nos. and finds out the 2 highest nos.
My code:
//prog to demonstrate largest and secondlargest among 5 nos.with while loop//
#include <iostream.h>
int main()
{
int num,large,seclarge,counter,x;
num=0;
large=0;
seclarge=0;
counter=0;
while(counter<5)
{
cout<<"enter number"<<endl;
cin>>num;
if (large<num)
{
seclarge=large;
large=num;
}
else if (large==num)
{
cout<<"same no"<<endl;
}
if ((seclarge<num) &&(large>num))
{
seclarge=num;
}
x=counter++;
}
cout<<"the largest is"<<large<<endl;
cout<<"the second large is"<<seclarge<<endl;
return 0;
}
any help is appreciated.Thanks in advance.