Thread: getting 2 largest nos with while loop

  1. #1
    Unregistered
    Guest

    Question getting 2 largest nos with while loop

    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.

  2. #2
    Unregistered
    Guest
    Hi.

    Do you want your prog to end as soon as it sees two numbers the same? or do you want it to input them all then check them? this way would be easier i think.

    Either way, you are going to need a variable for each number input otherwise the program 'forgets' the earlier numbers inputted.

    Keep going you are almost there. If you get stuck again, reply to this post and i will see if i can help more.

  3. #3
    Unregistered
    Guest
    thankx for peeping into my query.But I am still unclear of what u said.Can u code it ...pllleeaseeeee??

  4. #4
    Unregistered
    Guest

    Unhappy plleeaaaaaaaaaseeeeeeee heeeeeelp!!

    waiting for some clue??????????plleasse heeeeeeelppp!!!!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int var1=-1,var2=-2,someVar=-1,x=1;
    while(x)
    {
        cout <<"Enter a number: "; cin>> someVar;
    
        if( someVar > var1 )
        {
            if( var1 > var2 )
                var2 = var1;
            var1 = someVar;
        }
        else
        if( someVar > var2 )
            var2 = someVar;
    
        ...prompt for exit...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Unregistered
    Guest
    thankx,
    but how to exit??what is the word used for exit in VC++,how to use it in the above code??
    I am just frustrated over myself...just cant go further.
    thankx for ur help.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, you're in a loop; do you know how to break from a loop?

    cout << "Hit Q to quit."
    cin >> x;
    if( x == 'q' || x == 'Q' ) break;

    You can exit from a loop using 'break', using 'return' (to exit the function), or 'exit( )' to quit the whole program.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Unregistered
    Guest

    Thumbs up thanks

    thanks a heap quzah.fantastic..U r great!

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    60
    A comment on using cin / cin.getline


    int count=0,arr[5]={0};
    while(count<5){
    do{
    cout << "Input Nr. " << (count+1) << ": ";
    cin.getline(bf,5); /* You should prefer cin.getline(),
    it avoids nasty problems ocurring when the
    user inputs something else as you expect
    with cin >> nr; */
    if(bf[0]=='0'){ // For the case the user wants to input 0
    {arr[count]=0; break;} // This break ends the do...while
    }while(!(arr[count]=atoi(bf))); /* This forces the user to give you
    an integer (and not a string)*/
    count++;
    } // End of while loop

    /* After getting your array filled with numbers use any
    algorithm to sort it (There are as many as ants) */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  2. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 05:16 AM