NIM game

Why does my program keep running even after the user inputs a number over 8 and under 1 in my NIM game???

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
  // NIM game!
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()

{
	int total, n;

	cout << "Please enter a number between 1 and 7 and press ENTER:" << endl;
	cin >> total;

	

	if (total>=7 || total<=1)

	{ cout << "Please enter a number between 1 and 7 and press ENTER:" << endl;
	  }
	else{
	while (true) {

		if ((total % 3) == 2){
			total = total - 2;

			cout << "I am subtracting 2." << endl;
		}else{
			total--;
			cout << "I am subtracting 1." << endl;

		}
		cout << "New total is" << total << endl;

		if (total == 0) {
			cout << "I win!" << endl;
			break;
		}
	    
		cout << "Enter number to subtract between 1 and the new total: ";
		cin >> n;
		while (n < 1 || n > total) {
			cout << "Input must be between 1 and new total." << endl;
			cout << "Re-enter: ";
			cin >> n;
		}
		total = total - n;
		cout << "New total is " << total << endl;
		if (total == 0) {
			cout << "You Win!" << endl;
			break;
		}
	
    system("PAUSE");
    return 0;
}
}
}
Probably because of the if statement instead of a loop
Maybe you meant

1
2
3
4
5
while( total > 7  || total < 1 )
{
    cout << "Please enter a number between 1 and 7 and press ENTER: ";
    cin >> total;
}


*forgot code tag
Last edited on
sigh...such a simple solution...ty so much giblit!!
Topic archived. No new replies allowed.