Thread: loop to get greatest and least number, its not working please help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    1

    loop to get greatest and least number, its not working please help

    I can not seem to get this program to work, I am having no luck with loops for anything, very lost. This is my latest attempt and it is not working. Any help I can get would be greatly appriciated. Thank you in advance.
    Code:
    // This program should use a loop that lets the user enter a series of integers.
    // The user should enter -99 to signal the end of the series.
    // After all the numbers have been entered, 
    // the program should display the largest and smallest numbers entered.
    
    #include <iostream?
    using namespace std;
    
    int main()
    
    {
    	  //enter up to 5 integers
    	int least, int greatest;        //determine least and greatest
    
    
    double num1 = 0;
    double num2 = 0;
    double num3 = 0;
    double num4 = 0;
    double num5 = 0;
    
    
    		cout << " Please enter up to 5 integers:\n ";
    		cin >> num1,  num2,  num3, num4, num5;
    
    	cout << " The integer with the least value is: \n";
    	cin >>  least;
    
    		cout << " Of the integers you choose the one with the greatest value is: \n"
    		cin >>  greatest;
    
    if ( num1 < num2 )
    	least = num1;
    
    	else if ( num2 < num3)
    		least = num2;
    
    	else if ( num3 < num4)
    		least = num3;
    
    	else if ( num4 < num5)
    		least = num3;
    
    	else if ( num 5 < num1 )
    			least = num5
    
    if ( num1 > num2 )
    	greatest = num1;
    
    	else if ( num2 > num3)
    		greatest = num2;
    
    	else if ( num3 > num4)
    		greatest = num3;
    
    	else if ( num4 > num5)
    		greatest = num3;
    
    	else if ( num 5 > num1 )
    			greatest = num5
    
    
    if( user input == -99 ) break;
    
    return 0;
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For starters a loop will need a loop construct such as for or while.

    Find the missing semicolon(s) to make this code compile.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Also, do post the errors that you get!

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if( user input == -99 ) break;
    What's this supposed to be? It's two variables (user, input) that haven't been declared at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And whilst it's not REALLY wrong (it would be worse the other way around), your text says "Enter 5 integers", whilst your variables are of the type double, which means that they are floating point variables - it's a bit confusing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #include <iostream?
    Perhaps the first trivial problem?
    Replace the ? with a >

    Just so you know, just saying "there are errors" isn't good enough sometimes. You need to be specific, by posting actual compile logs for example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    The list of 'if else's is really quite a bad way of doing things, you need to loop. And don't do num1, num2, ect, rather do num[5].

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps you should have a look at some tutorials, such as
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Here. As you can see, loops make my code alot smaller and more flexible.
    Code:
    // By Yarin, public domain.
    
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
       int amount;
       cout << "How many arguments are you inputing? ";
       cin >> amount;
    
       int *num = new int[amount];
       int x;
    
       cout << "Enter your arguments: ";
       for(x = 0; x < amount; x++)
          cin >> num[x];
       cout << "Arguments submited..." << endl;
     
       // Now we find the max and min numbers:
       int max = 0, min = 0;
       for(x = 0; x < amount; x++)
       {
          if(num[x] > num[max]) max = x;
          if(num[x] < num[min]) min = x;
       }
    
       cout << "The largest argument submited is " << num[max] << "." << endl;
       cout << "The smallest argument submited is " << num[min] << "." << endl;
    
       delete [] num;
       cin.get();
       return 0;
    }
    I'm in an okay mood today. So you don't expect handouts from everyone on the board like this.
    Welcome to the boards btw.
    Last edited by Yarin; 09-26-2007 at 12:22 PM. Reason: Code fixes. :D

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A warning: Yarin's code uses variable length arrays, which is a non-standard feature provided as an extension by some compilers. getch() is also non-standard and probably unnecessary in this case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It wouldn't even compile.
    Code:
    for(x = 0; x < amount, x++)
    ->
    Code:
    for(x = 0; x < amount; x++)
    And what's this? . . .
    Code:
       cout << "The largest argument submited is " << num[max] << "." << endl;
       cout << "The smallest argument submited is " << num[max] << "." << endl;
    [edit] Also, getch() would likely be in <conio.h>. But cin.get() is a much better idea anyway. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    True. Hey, I didn't compile to check, and those 3 problems are very small. Fixed anyway.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I didn't compile to check, and those 3 problems are very small.
    I thought "they are there to test stressedstudent" would be a better comeback

    Honestly though, fix the use of VLAs (variable length arrays) and remove conio.h. It is a bad habit to inculcate in a stressed student.
    Last edited by laserlight; 09-26-2007 at 12:24 PM. Reason: VLA ::= Variable Length Array
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't change the VLA's to dynamic array, change it to a vector. There's no good reason to use a dynamic array over a vector here.

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Good now? What are VLAs?

Popular pages Recent additions subscribe to a feed