Expert C++ Problem

When I set a random number to compnumbers[5] and only that variable, somehow it sets playernumbers[0] to the same thing. there is no reference to that variable to be set to compnumbers 5. Please help if you can.


source code:
http://w3.tvi.cc.nm.us/~salted/Game.cpp

Comments

  • Here's your problem:
    [code]
    int playernumbers[5];
    int compnumbers[5];
    [/code]
    Notice how playernumbers is an array of 5 integers? Well, that means you can set index position 0, 1, 2, 3, and 4, but _not_ 5. By setting index position 5, you cross over the boundaries that it holds in memory. Notice what's after playernumbers? Well compnumbers is after it (in memory, since it is declared directly after playernumbers), so its _first_ index (0) is overwritten with that value. Check your source code to make sure you only use playernumbers[0 to 4] or make playernumbers size 6, so that it has six positions (0 to 5).


    http://druidgames.cjb.net


  • : When I set a random number to compnumbers[5] and only that variable, somehow it sets playernumbers[0] to the same thing. there is no reference to that variable to be set to compnumbers 5. Please help if you can.
    :
    :
    : source code:
    : http://w3.tvi.cc.nm.us/~salted/Game.cpp
    :
    Dat is because u declared compnumbers to be an array of 5 integers and u r using the 6'th element, compnumbers[5] is the 6'th element since arrays in C begins at 0. That means u have overrun ur array. try declaring it like this

    int compnumbers[6];

  • : When I set a random number to compnumbers[5] and only that variable, somehow it sets playernumbers[0] to the same thing. there is no reference to that variable to be set to compnumbers 5. Please help if you can.
    :
    :
    : source code:
    : http://w3.tvi.cc.nm.us/~salted/Game.cpp
    :
    Dat is because u declared compnumbers to be an array of 5 integers and u r using the 6'th element, compnumbers[5] is the 6'th element since arrays in C begins at 0. That means u have overrun ur array. try declaring it like this

    int compnumbers[6];



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories