I am attempting to create a source code that uses a while statement to determine both the largest and the second largest numbers of ten numbers input by a user from the keyboard.
I was able to display the largest number entered by the user using a while statement via separate source code, but am having a little trouble with constructing the code to find the largest and the 2nd largest.
Am not sure whether or not I should use a while statement to find the second largest. The way the code is now constructed, it accepts only 3 numbers input by the user, and then goes no further. I was able to get it to accept 4 numbers, but the same thing happened.
Any help that can be offered would be very much appreciated.
Thanks
CODE
// Program Q4.19.cpp: This program uses a while statement to determine and print the 2 largest of 10 numbers entered by a user from the keyboard.
#include <iostream>
// Declare using declarations
using std::cout;
using std::cin;
using std::endl;
int main() // Begin program execution
{
// Initialize variables
int counter = 1;
int num;
int largest = 0;
int seclargest = 0;
while ( counter <= 10 ) // Begin 1st while loop
{
cout << "Please enter a number from the keyboard.\n" << endl; // Prompt user to enter a number from the keyboard
cin >> num; // Read in number entered by user
cout << "\n"; // Insert blank line
if ( num > largest ) // Begin loop to determine the largest of 10 numbers entered by user from the keyboard
{
largest = num;
}
else
{
while ( counter <= 8 )
{
if ( num > seclargest )
seclargest = num;
} // End if/else
} // End 2nd while
} // End 1st while
cout << "\nThe largest of the numbers entered is " << largest << ".\n" << endl; // Display the second largest number entered by user
cout << "\nThe second largest of the numbers entered is " << seclargest << ".\n" << endl; // Display the second largest number entered by user
counter++; // Advance counter by 1 for each loop
return 0; // Terminate program successfully
} // End main and program execution