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

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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,366
    >> 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
    2,158
    Not valid in C++!!!
    I never use vectors, always and only VLAs!
    But I guess so long as it works it should be okay...

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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,366
    >> 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,688
    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.
    Double Helix STL

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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 02: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,688
    Ah sorry dwks I missed that line and yes of course you are correct
    Double Helix STL

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah sorry dwks I missed that line
    Yet another reason why RAII rocks
    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. #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,005
    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,895
    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

Popular pages Recent additions subscribe to a feed