- #1
Valour549
- 57
- 4
My code is to find the largest and second-largest numbers, from an arbitrary number of postive integers.
For some reason, the part of the code in italics is not being recognized. How do I know? Because:
- Let's say we choose to compared two numbers, if my first number is 5, and second number is 9, the result is correct. In other words it shows the largest number as being 9 and second-largest as being 5.
- However, if my first number is 9, and second number is 5, the result is incorrect. It shows the largest number as being 9 and the second-largest as being 0. In other words, it does not recognize the condition when an input is greater than secondlargest.
I have checked for ages and even compared it with examples online, and can't find where my code is wrong. Any help would be greatly appreciated.
#include<iostream>
using namespace std;
int main()
{
int number, largest, secondlargest, counter, c;
largest=0; secondlargest=0; counter=1;
cout<<"This program will find the largest and second largest numbers for you."<<endl;
cout<<"Please enter only positive integers for comparison."<<endl<<endl;
cout<<"How many numbers are you comparing? ";
cin>>c;
cout<<endl;
while(counter<=c)
{
cout<<"Please enter the "<<counter<<"st number: ";
cin>>number;
if (number>largest)
{ secondlargest=largest;
largest=number; }
else if (number>secondlargest)
{ secondlargest=number; }
counter++;
}
cout<<endl;
cout<<"The largest number is "<<largest<<endl;
cout<<"The second largest number is "<<secondlargest<<endl;
system("pause");
return 0;
}
For some reason, the part of the code in italics is not being recognized. How do I know? Because:
- Let's say we choose to compared two numbers, if my first number is 5, and second number is 9, the result is correct. In other words it shows the largest number as being 9 and second-largest as being 5.
- However, if my first number is 9, and second number is 5, the result is incorrect. It shows the largest number as being 9 and the second-largest as being 0. In other words, it does not recognize the condition when an input is greater than secondlargest.
I have checked for ages and even compared it with examples online, and can't find where my code is wrong. Any help would be greatly appreciated.
#include<iostream>
using namespace std;
int main()
{
int number, largest, secondlargest, counter, c;
largest=0; secondlargest=0; counter=1;
cout<<"This program will find the largest and second largest numbers for you."<<endl;
cout<<"Please enter only positive integers for comparison."<<endl<<endl;
cout<<"How many numbers are you comparing? ";
cin>>c;
cout<<endl;
while(counter<=c)
{
cout<<"Please enter the "<<counter<<"st number: ";
cin>>number;
if (number>largest)
{ secondlargest=largest;
largest=number; }
else if (number>secondlargest)
{ secondlargest=number; }
counter++;
}
cout<<endl;
cout<<"The largest number is "<<largest<<endl;
cout<<"The second largest number is "<<secondlargest<<endl;
system("pause");
return 0;
}
Last edited by a moderator: