Thread: Size of Array, user inputted..

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not initialise amount, presumably to 0.
    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

  2. #17
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You never initialized the variable amount.
    Domething like this would do the trick
    Code:
    int amount = 0;             //The amount the number was found
    Kurt

  3. #18
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Code:
    How many numbers do you wish to input?: 9
    Number? 12
    Number? 23
    Number? 45
    Number? 4332432141
    Number? 34
    Number? 23
    Number? 12
    Number? 9
    Number? 2332432145254325325
    What number do you wish to search for?: 23
    The number 23 appeared 2 times.
    Thanks everyone! It's working correctly.. =)

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's good
    Now, as hk_mp5kpdw hinted, you can actually use a function, count(), from <algorithm>, and thus replace:
    Code:
    for (counterfind = 0; counterfind < length; ++counterfind)
            {
            if(list[counterfind] == number)
                    {
                    ++amount;
                    }
            }
    
    std::cout << "The number " << number << " appeared " << amount << " times. \n";
    with:
    Code:
    std::cout << "The number " << number << " appeared "
              << std::count(list, list + length, number)
              << " times. \n";
    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

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Now, as hk_mp5kpdw hinted, you can actually use a function, count(), from <algorithm>

    Of course, that's good knowledge to remember along with vector for after you submit your assignment, since it is unlikely that such code would be appropriate to use there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM