I really can't figure this out...

I am trying to create my own version of the NIM game. Ok so when I build and run this every thing is smooth until it is computer's turn in which it subtracts 19880494... or something like that every time.. I believe my problem lies within the % operator, but I'm not exactly sure how yet..

Need Help ASAP! Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include <iostream>
using namespace std;

int main()
{
    int total, remainder, x, high, input, replay, com;
    x = high;
    replay = 1;

    while (replay == 1)
    {
        while (true)
        {
            cout << "Welcome to the Subtraction Game!" << endl;
cout << "I'm the best at this game." << endl << "OK let's play!" << endl;
cout << "Please enter a total to start from(must be above 2).";
            cin >> total;
            while (total <= 2)
            {
                cout << "THE NUMBER MUST BE ABOVE 2." << endl << "Re-enter.";
                cin >> total;
            }
            cout << "Now enter the highest number we can subtract by(Must be above 1).";
            cin >> high;
            while (high <= 1)
            {
                cout << "THE NUMBER MUST BE ABOVE 1." << endl << "Re-enter.";
                cin >> high;
            }
            cout << "The total is " << total << "." << endl << "Subtract from it.";
            cin >> input;
            while (input < 1 || input > high)
            {
                cout << "Sorry the number you subtract by must be between 1 and " << high << endl << "Please Re-enter." << endl;
                cin >> input;

                total = total - input;
                cout << "You subtracted by " << input << "." << endl << "The new total is " << total;
                if (total == 0)
                {
                    cout << "You win!";
                    break;
                }
            }
            remainder = total % x;
            remainder = com;
            if (remainder == 0)
            {
                com = 1;
            }
            total = total - com;

            cout << "I subtracted by " << com << "." << endl << "The new total is " << total << endl;
            if (total == 0)
            {
                cout << "I win!" << endl << endl;
                break;
            }
        }
        cout << "If you would like to play again enter a 1. If not enter a 2." << endl;
        cin >> replay;
        while (replay <1 || replay > 2)
        {
            cout << "You must enter 1 or 2" << endl;
            cin >> replay;
        }
    }
    return 0;
}
line 7: You set x equal to high before setting high to anything.
It didn't work.. Compile it for yourself and see.
1
2
3
4
      cout << "Now enter the highest number we can subtract by(Must be above 1).";
            cin >> high;
             x = high + 1;
            while (high <= 1)

This is what I did.
I can't compile it. Your code doesn't compile.

line 46: You set remainder equal to com before setting com to anything.

edit: You should put "x = high + 1;" after that next loop.
Last edited on
1
2
3
4
5
6
7
                total = total - input;
                cout << "You subtracted by " << input << "." << endl << "The new total is " << total;
                if (total == 0)
                {
                    cout << "You win!";
                    break;
                }
should be outside of the loop on line 32-44

1
2
            remainder = total % x;
            remainder = com;
what are you trying to do here?
Last edited on
1
2
3
4
5
6
7
total = total - input;
cout << "You subtracted by " << input << "." << endl << "The new total is " << total;
if (total == 0)
{
cout << "You win!";
break;
}
should be outside of the loop on line 32-44

1
2
remainder = total % x;
remainder = com;
Edit & Run
what are you trying to do here?


I'm trying to make the number the computer subtracts by equal to the remainder of total and x divided.
I've Solved it!
The problem is here!
1
2
remainder = total % x;
remainder = com;


1
2
3
4
5
6
 remainder = total % x;
            com = remainder;
            if (remainder == 0)
            {
                com = 1;
            }

I had remainder assigned to com(which had no value).
But I really wanted com assigned the value of remainder!
Thanks everyone!
Thanks Giblit for making me think about it.
Topic archived. No new replies allowed.