0

I have been programming a bit with C++ in CodeBloks before and since some time i started to program in MS Studio. At the moment im programming a little game from the book: Programming: Principles and Practice using C++. Exercise 12 of chapter 5. I have been looking around on the internet to fix the range memory error which keeps occuring whatever I try (see printscreen link). The description about the program is in the code itself. I also did make the vectors a hard coded (if I say that right) size to make the program stop complaining about their memory range errors.

Note: Yes this is a school exercise/practice as well. But I rather ask what I did wrong then copy paste the code from the internet..

Please look over my code and see or I made some mistake with me vectors, cause that is where my errors are probably comming from.

Update: placed as answer in this topic. Link to update

Error:

Unhandled exception at 0x7529CBC2 in Tweede project.exe: Microsoft C++ exception: Range_error at memory location 0x006FF510.

Program:

#include "std_lib_facilities.h"

int main(){
    bool while_bit = false;
    vector<int>bulls_cows(4);
    vector<int>inputs_arr(4);

    //Generate 4 random numbers.
    for (int a = 0; a < 4; ++a){
        bulls_cows[a] = a + 1;//randint(a) % 9 + 0; //random number between 0 and 9.
    }
    //bulls_cows[1] = randint(10);
    cout << "For this game, called Bulls and Cows, you have to guess the four right numbers between 0 and 9.\n";
    cout << "When one or more digets are right and in the right position the program will say the number of Bulls.\n";
    cout << "When one or more digets are right but not in the right position the program will say the number of Cows.\n";
    cout << "Please enter 4 number by filling in ONE positive diget and press enter. Do this four times and wait for the result.\n";

    for (int z = 0; z < 4; ++z) {
        cout << bulls_cows[z] << "\n";
    }

    while (while_bit == false) {
        int input = 0; //Reset of input, cows and bulls every round.
        int cows = 0;
        int bulls = 0;

        cin >> input;

        //Test for legit input. If legit then it writes it to the array called "input_arr"
        if (input < 0 || input > 9) {
            cout << "Number must be between 0 and 9.\n";
        }
        else {
            inputs_arr.push_back(input);
        }
        //Check or 4 numbers have been given.
        if (sizeof(inputs_arr) < 4) {
            //Check for equal numbers on same spot.
            for (int b = 0; b < 4; ++b) {
                if (inputs_arr[b] == bulls_cows[b]) {
                    bulls + 1;
                }
            }
            //Check for a number in all spots.
            for (int c = 0; c < 4; ++c) {
                if (inputs_arr[c] == bulls_cows[0] || bulls_cows[1] || bulls_cows[2] || bulls_cows[3]) {
                    cows +1;
                }
            }
        }

        if (bulls < 4) {
            cout << "You did not guess the right combination.\n";
            cout << "Number of bulls: " << bulls << "\n";
            cout << "Number of cows: " << cows << "\n";
            inputs_arr[0, 0, 0, 0]; //Empty array.
        }

        if (bulls == 4) {
            cout << "You guessed the right combination!\n";
            while_bit = true;
    }

    }

    keep_window_open(); //To keep terminal open since MS Studio doesn't itself.

    return 0;
}
rem97
  • 11
  • 1
  • 3
  • 2
    *Please* refrain from linking pictures of text. Just enter the text itself, maybe marked as a quote or as code. – Deduplicator Apr 02 '18 at 13:43
  • `std_lib_facilities.h` what's that? – Daniel Jour Apr 02 '18 at 13:46
  • 1
    Google what the sizeof operator does – Daniel Jour Apr 02 '18 at 13:48
  • std_lib_facilities.h is a lib from the book. It has most basic libs in it like , and if im right . It also has some extra functions like the one that keeps the prompt/terminal open so you can see the results. – rem97 Apr 02 '18 at 14:31
  • Will do @Deduplicator. Will edit the post. – rem97 Apr 02 '18 at 14:32
  • `bulls + 1;` and `cows + 1;` have no effect. What were you trying to do there? – Ben Voigt Apr 02 '18 at 14:53
  • Adding one digit to the integerd. Could be written as cows ++1. :) – rem97 Apr 02 '18 at 14:57
  • Welcome to Stack Overflow! Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Apr 02 '18 at 17:52
  • Ok, thank you for your advise. I will apply it to my next post, if that is allright, since my problem slowly got solved by tweaking the brackets and some other minor things. – rem97 Apr 03 '18 at 14:00

3 Answers3

0

Using the sizeof operator with your inputs_array vector will give you the size in bytes of that object in memory. Perhaps you've used this with arrays before.

Instead, I think you want the number of items you've pushed into the vector. std::vector has a function called size which gives the number of items pushed to it.

  • Yes. I want indeed the number of items in the array. I will look into it. Thank you! :) – rem97 Apr 02 '18 at 14:58
  • @rem97 If you're struggling with the differences between vector and array, [here is a listing of their qualities](https://stackoverflow.com/questions/15079057/arrays-vs-vectors-introductory-similarities-and-differences/15079462#15079462). –  Apr 02 '18 at 15:44
  • I got some view on it but maybe I might look at vector like a array. Will look at it tho and see what I can learn. Thanks! :) – rem97 Apr 02 '18 at 20:23
0

In Visual Studio you should really, really set up the warning level to level 4. Then the compiler will tell you about several of the odd things you try to do.

For example:

 warning C4552: '+': operator has no effect; expected operator with side-effect 1>
                bulls + 1;

You compute a result, but never store it anywhere. Same thing for the cows.

error C4548: expression before comma has no effect; expected expression with side-effect
            inputs_arr[0, 0, 0, 0]; //Empty array.

This is not the way to empty an array. To remove all elements, you do inputs_arr.clear();.

warning C4127: conditional expression is constant
        if (sizeof(inputs_arr) < 4) {

Like we have already seen, sizeof is the size of the vector object (always the same), not the number of elements it holds.


Then there are some logic errors.

vector<int>inputs_arr(4);

This creates a vector originally holding 4 ints.

But later when you do

inputs_arr.push_back(input);

it adds more elements to the vector, so now it might hold up to 8 ints. You have to decide if you want to create it with the full size up front, or add elements as you go along. But not both.

Another problem is with the condition

if (inputs_arr[c] == bulls_cows[0] || bulls_cows[1] || bulls_cows[2] || bulls_cows[3]) {

Even though there are languages where you can write conditions similar to this, in C++ you cannot. To compare a variable x to several other values, you have to spell it out:

if (x == y || x == z || x == w)
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Thank you for your feedback. I will work on it. I did indeed not know that you could not compare different things with multiple or statements. Also taught redeclaring the array with zero's would clear its former values. Learned a lot from this post :) – rem97 Apr 02 '18 at 16:01
0

Update: (wasn't sure or I had to add it to the question or as answer. If wrong, please let me know. Im new to Stack overflow ;) )

It seems some statement/loop brackets where not placed right. I edit that and a bit: Sadly it seems that my intergers cows and bulls do not reset at line 61 and 62.. Anyone a idea what I might have done wrong there.

LOG:

cin did not not loop. Also edited else-statement 'inputs_arr.pushback(input)' and added all for-loops and statements below it. Fixed a part of the cin problem.

Replaced bulls, cows and input integers to top of program, and the reset at the end of the false/if-statement.

Put all loops and statements below the if-statement '(int b = 0; b < 4; ++b)' into it.

Edited the for-loop to >4 which couts "Size of array" to see what values it has. Gets deleted later on cause it is for test only.

#include "std_lib_facilities.h"

int main()
{
bool while_bit = false;
int input = 0;
int cows = 0;
int bulls = 0;
vector<int> bulls_cows(4); //size needed to prevent memory size error at line 11.
vector<int> inputs_arr;

//Generate 4 random numbers. Need to add seed later.
for (int a = 0; a < 4; ++a){
    bulls_cows[a] = randint(a) % 9 + 0; //random number between 0 and 9.
}

cout << "For this game, called Bulls and Cows, you have to guess the four right numbers between 0 and 9.\n";
cout << "When one or more digets are right and in the right position the program will say the number of Bulls.\n";
cout << "When one or more digets are right but not in the right position the program will say the number of Cows.\n";
cout << "Please enter 4 numbers by filling in ONE positive diget and press enter. Do this four times and wait for the result.\n";

for (int z = 0; z < 4; ++z) { //Gives the generated numbers for testing.
    cout << bulls_cows[z] << "\n";
}

while (while_bit == false) {

    cin >> input;

    //Test for legit input. If legit then it writes it to the array called "input_arr"
    if (input < 0 || input > 9) {
        cout << "Number must be between 0 and 9.\n";
    }
    else {
        inputs_arr.push_back(input);

        //Check or 4 numbers have been given.
        if (inputs_arr.size() > 3) {
            //Check for equal numbers on same spot.
            for (int b = 0; b < 4; ++b) {
                if (inputs_arr[b] == bulls_cows[b]) {
                    ++bulls;
                }
            }

            //Check for a number in all spots.
            for (int c = 0; c < 4; ++c) {
                if (inputs_arr[c] == bulls_cows[0] || inputs_arr[c] == bulls_cows[1] || inputs_arr[c] == bulls_cows[2] || inputs_arr[c] == bulls_cows[3]) {
                    ++cows;
                }
            }

            /*for (int x = 0; x < 4; ++x) { //Couts again the fresh entered numbers for a better overview.
                cout << "Size of array: " << inputs_arr[x] << "\n";
            }*/

            if (bulls < 4) {
                cout << "You did not guess the right combination.\n";
                cout << "Number of bulls: " << bulls << "\n";
                cout << "Number of cows: " << cows << "\n";
                int cows = 0; //Reset of cows and bulls each round.
                int bulls = 0;
                inputs_arr.clear(); //Empty the array.
                cout << "Please enter 4 numbers:\n";
            }

            if (bulls == 4) {
                cout << "You guessed the right combination!\n";
                while_bit = true;
            }
        }
    }
}

keep_window_open(); //To keep terminal open since MS Studio doesn't itself.

return 0;
}
rem97
  • 11
  • 1
  • 3
  • Update: Fixed my problem with resetting cows and bulls. I declared them instead of called them. I removed int: "cows = 0;" and it fixed my problem. Such a small thing which I could not find. Probably tired ^^ – rem97 Apr 03 '18 at 15:32