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

This is a discussion on loop to get greatest and least number, its not working please help within the C++ Programming forums, part of the General Programming Boards category; What if amount is zero? If you wanted to be picky about English, too, you're using lots of single 't's ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    What if amount is zero?

    If you wanted to be picky about English, too, you're using lots of single 't's where there should be two.



    [edit]
    Good now? What are VLAs?
    Variable Length Arrays.

    Good now?
    No, you still have a VLA. [/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.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Good now? What are VLAs?
    No, use vectors. A VLA is a variable length array, which is what you used first. It's not valid in C++ but is supported as an extension by gcc.

  3. #18
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,689
    Not valid in C++!!!
    I never use vectors, always and only VLAs!
    But I guess so long as it works it should be okay...
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Riiight. Well, I guess we know which compiler you have.

    Besides, that's the point. It wouldn't work if your compiler didn't have that extension.

    And it will still break if you enter 0 for amount. Or "cat" or something. But it's just a demonstration, after all.
    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.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> But I guess so long as it works it should be okay...
    How do you know if it works?

    I'm guessing you'll spend a lot more time debugging and fixing bugs in your game with that attitude than you would if you tried to use better tools in the first place.

  6. #21
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the compiler supports them as an extension, they should work - in a limited way. If you have very large amounts of data, you may not be able to store it all in a VLA (it has rather limited memory capacity), whereas a vector would handle that amount of data just fine.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #22
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Just noticed you declared this:

    Code:
    int *num = new int[amount];
    But you did not delete the allocated memory... thus you have a dangling pointer.

    And I dont see the reason for allocating memory in such a simple program, you can accomplish
    the task without using a pointer at all.
    I'm just trying to be a better person - My Name Is Earl

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Just noticed you declared this:
    Code:
    int *num = new int[amount];
    But you did not delete the allocated memory... thus you have a dangling pointer.
    Fourth-last line.
    Code:
    delete [] num;
    And I dont see the reason for allocating memory in such a simple program, you can accomplish
    the task without using a pointer at all.
    Unless you using a standard container, you need to allocate memory dynamically if you want any number of numbers to be supported. (And of course, the standard containers like std::vector just allocate memory behind the scenes.)

    Assuming that you have to store the values at all, of course. I'd use something like this:
    Code:
    #include <iostream>
    #include <climits>
    
    int main() {
        int num, max = INT_MIN, min = INT_MAX;
        bool did = false;
    
        while(std::cin >> num) {
            if(num < min) min = num;
            if(num > max) max = num;
            did = true;
        }
    
        if(!did) std::cout << "No numbers entered.\n";
        else {
            std::cout << "Minimum: " << min << std::endl
                << "Maximum: " << max << std::endl;
        }
    
        return 0;
    }
    (Disclaimer: I have not tested it, only proved it correct. [Donald E. Knuth] Actually, I didn't even prove it correct. )

    [edit] The exact quote is "I have only proved it correct, not tried it." [/edit]
    Last edited by dwks; 09-26-2007 at 03:07 PM.
    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. #24
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Ah sorry dwks I missed that line and yes of course you are correct
    I'm just trying to be a better person - My Name Is Earl

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    Ah sorry dwks I missed that line
    Yet another reason why RAII rocks
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Sep 2007
    Posts
    12
    where does it say only enter 5 numbers? and if your doing a loop i would do it like this

    Code:
    int input,least, greastest;
    
    do
    {
          cout<<"Please enter a number.";
          cin>>input;
    
          least = input;
          greatest = input;
    
          if(input < least)
          {
              least = input;
          }
          if(input > greatest)
          {
             greatest = input;
          }
    }while(input != -99);

  12. #27
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Cool, then both the least and the greatest will be -99 when the loop exits.
    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.*

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    IF the loop exits - which won't happen if the user presses Ctrl+Z/Ctrl+D or enters a letter or the input comes from a file where the last number isn't -99 or which contains a letter.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21