Vector Iteration Problems

This is a discussion on Vector Iteration Problems within the C++ Programming forums, part of the General Programming Boards category; Hi. I've declared a vector and now I want to add 1 to every spot in it that comes up ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    Vector Iteration Problems

    Hi. I've declared a vector and now I want to add 1 to every spot in it that comes up from a random number generator. So if the number 6 came up I would want to increment vector[6] by 1. I have code that looks like it should be working but gives an error everytime the program is run.


    Code:
    vector <unsigned> Dicegame::PlayGame (unsigned numPlays) 
    { 
    
            for (unsigned i = 0; i < myPlaysPerGame; i++) 
            { 
                    unsigned j = TossNDice(); 
                    myGameResult[j]++; 
            } 
    
    
            return myGameResult.size(); 
    }

  2. #2
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    Delete double post. You probably are getting an access violation, i.e. j is larger than the vector size.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You should probably give more information about the error, and maybe more code (like how you initialze your vector). Also, please copy and paste your code when you post (if possible) because typos make it hard to distinguish the actual problem you are having.

    Just like with a regular C-style array, you cannot access an element in the vector with [] unless an element actually exists (that index is not out of the bounds of the array). Your code is only safe if you know that TossNDice will only give you values within a particular range (e.g. 1-12) AND your vector has a size of one more than the highest number in that range (e.g. size of 13 because the indexes start at 0).

    If you don't know the range that TossNDice will return, or if the range is too big to use a vector efficiently, try using a map. Your code will work properly as is.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Without seeing more of your Dicegame class it is more difficult to guess what the types of your variables are but I can try...
    Quote Originally Posted by PunkyBunny300
    vector <unsigned> Dicegame::PlayGame (unsigned numPlays)
    ...
    return myGameResult.size();
    If we are to assume that myGameResult is the vector, then you are returning an integer value (size_t type) when you have declared the return type to be a vector. This would seem to be one error.

    Quote Originally Posted by PunkyBunny300
    vector <unsigned> Dicegame::PlayGame (unsigned numPlays)
    ...
    for (unsigned i = 0; i < myPlaysPerGame; i++)
    You are passing in a variable that doesn't seem to get used at all. I can only guess, but perhaps this loop should read "i < numPlays". Again, without more knowledge of the structure of the class or purpose behind what the code is trying to do it is difficult to say.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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