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(); 
    }
    Last edited by PunkyBunny300; 04-05-2004 at 04:32 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It looks like PlayGame() is supposed to return a vector of unsigned ints according to the initial line in the definition, but it is returning a single int (or maybe a size_type) according to the return statement.

    PlayGame takes a parameter called numPlays, but numPlays isn't used anywhere in the function.

    Place debugging lines like these or use your debugger to see if you can find where an error is that way:

    unsigned j = TossNDice();
    cout << "myGameResult[" << j << "] before the change is " << myGameResult[j] << endl;
    myGameResult[j]++;
    cout << "myGameResult[" << j << "] after the change is " << myGameResult[j]++ << endl;
    cout << "myPlays

    try substituting

    myGameResult[j] = myGameResult[j] + 1;

    for

    myGameResult[j]++;

    if you aren't getting the results you expect.

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