NIM Game Error

Hey guys, I'm fairly new to c++ and I've been creating a NIM game. So the user picks the starting total and the maximum subtracting value. To win this game, you want to always subtract to a multiple of the maximum subtract value + 1. So for example if it was 10, you'd always want to subtract to 11 or 22 or 33 or 44. The problem im having is the computer will subtract to negative numbers to get to a multiple and i dont know why.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  // Nim game

#include <iostream>
#include "stdafx.h"

using namespace std;

int main()
{
	int total, userInput, subtractNum, computerMultiple, i, num_to_subtract;

	cout << "Welcome to NIM. Pick a starting total: " << endl; 
	cin >> total; //gets the game's starting total
	
	while (total < 0) //checks if the total is valid
	{
		cout << "Invalid input. Number must be greater than 0." << endl; 
		cout << "Re-enter: " << endl;
		cin >> total; //gets the game's new starting total if the first total was invalid
	}
	
	cout << "Enter the maximum substraction value: " << endl;
	cin >> subtractNum; //gets the game's maximum subtraction value

	while (subtractNum > total || subtractNum < 1)
	{
		cout << "Invalid input. Number must be greater than 0 and less than the total." << endl;
		cout << "Re-enter: " << endl;
		cin >> subtractNum; //gets the game's new subtract maximum
	}

	computerMultiple = subtractNum + 1;
	i = total; //loop increment
	//game loop
	while (true)
	{
		//Computer Strategy
		while ((total > 0))
		{
			while (i >= 1)
			{
				if (i == 1)
				{
					num_to_subtract = 1;
					break;
				}
				else if ((i % computerMultiple != 0))
				{
					i--;
				}
				else
				{
					num_to_subtract = total - i;
					break;
				}
			}
			break;
		}

		//subtracts the computer's number from the total
		total -= num_to_subtract;

		cout << "I'm subtracting " << num_to_subtract << endl;
		cout << "Total is now " << total << endl;
		//checks if the computer has won
		if (total == 0)
		{
			cout << "I win!" << endl;
			break;
		}

		// players turn
		cout << "Enter a number to subtract (1 - " << subtractNum << "): " << endl;
		cin >> userInput;

		//checks if the user input is a valid value to subtract
		while (userInput < 1 || userInput > subtractNum)
		{
			cout << "Invalid input. Number must be between 1 and " << subtractNum << endl;
			cout << "Re-enter: "; 
			cin >> userInput;
		}

		//subtracts the user input from the total
		total -= userInput;
		
		cout << "New total is " << total << endl;

		if (total == 0)
		{
			cout << "You win!" << endl;
			break;
		}

	}
	system("PAUSE");
	return 0;
	
}


Thanks!
Topic archived. No new replies allowed.