I understand now that there is a more efficient way to write this program, but although my way is a bit convoluted i still dont understand why it doesnt work. The program is meant for the user to type 10 numbers and then it will display the highest number and then the second highest number. If you enter numbers 1-10 in sequence for the 10 inputs, the highest number will display correctly as 10, but the second highest number will read as zero. I dont understand why this statement isnt working. I was trying to say if the number is greater than the current largest number then assign this as the new largest number otherwise if the number is less than the current largest number AND greater than the current second largest, then assign the number to the second number. Seems like it should work but the statement below always produces a zero for the second number. Thanks in advance for any help!
(this is the code fragment i am having issues with, the full code is below)
CODE
if (number > largest)
largest = number;
else
if (number < largest && number > next)
next = number;
(Full Code)
CODE
int main(int argc, char *argv[])
{
int counter=1,number=0,next=0,largest=0;
while (counter < 11)
{
cout << "Enter number (" << counter << " of 10): ";
cin >> number;
if (number > largest)
largest = number;
else
if (number < largest && number > next)
next = number;
counter++;
}
cout << "The largest number is: " << largest << endl;
cout << "The second largest number is: " << next << endl;
system("PAUSE");
return EXIT_SUCCESS;
}