Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,397 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,104 people online right now. Registration is fast and FREE... Join Now!




Displaying largest & 2nd Largest of 10 numbers

 
Reply to this topicStart new topic

Displaying largest & 2nd Largest of 10 numbers, Numbers entered from keyboard: find largest/2nd largest

UncleScruffy
29 Oct, 2006 - 05:27 PM
Post #1

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 7


My Contributions
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

User is offlineProfile CardPM
+Quote Post

monolyth
RE: Displaying Largest & 2nd Largest Of 10 Numbers
29 Oct, 2006 - 06:12 PM
Post #2

New D.I.C Head
*

Joined: 29 Oct, 2006
Posts: 2


My Contributions
two problems i see first your while loop isnt being incremented, leading to an infinite loop

and second as is when you set your largest you lose what was previously there which would be your new second largest

your code was seemed to stop because it was getting stuck in your second while loop, which is uneeded.

so you can change your while loop to

CODE


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
          {
    seclargest = largest;
    largest = num;
    
          }    
     else if ( num > seclargest )
        seclargest = num;
        
    counter++;
}



This post has been edited by monolyth: 29 Oct, 2006 - 06:16 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:57AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month