Thanks for the helpful reply.We got it to compile, but now we get different results on different compilers:
on my compiler, it runs & I get :
Welcome to The Subtraction (NIM) Game.
Please enter a starting total.
The starting total must be higher than one.
Please re-input a new starting total and press enter.
The starting total must be higher than one.
Please re-input a new starting total and press enter.
The starting total must be higher than one.
Please re-input a new starting total and press enter.
The starting total must be higher than one.
Please re-input a new starting total and press enter.
The starting total must be higher than one.
but on Codeblocks, it runs the game, but every time the game gets to 6 it just asks for a new number. Always at 6:
"i enter 9
i enter 2 as the number to subtract
then it subtracts 1
then i subtract 2
it gets to 6
and it asks me if i want to play again"
This time I see no way to enter the code separately. Would it be better to mark the original post as solved & start another one?
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
|
#include <iostream>
using namespace std;
int main(){
while(true){
int total;
int subtract;
int total_subtract;
int play_again;
cout << "Welcome to The Subtraction (NIM) Game." << endl;
cout << "Please enter a starting total." << endl;
cin >> total;
while (total < 1){ //Can also be (total <=0)
cout << "The starting total must be higher than one." << endl;
cout << "Please re-input a new starting total and press enter." << endl;
cin >> total;
}
cout << "Please input the highest amount you would like ";
cout << "to be able to subtract and press enter." << endl;
cin >> total_subtract;
while (total_subtract >= total){
cout << "The highest amount you would like to be able to";
cout << " subtract must be lower than your starting total." << endl;
cout << "Please re-input a new amount to subtract and press enter." << endl;
cin >> total_subtract;
}
while (total > 0){ //Can also be (total >= 1) or (true)******
if (total % 3 == 2){
total = total - 2;
cout << "I am subtracting 2." << endl;
cout << "The new total is " << total << "." << endl;
}else{
total = total - 1; //Can also be total--
cout << "I am subtracting 1." << endl;
cout << "The new total is " << total << "." << endl;
}
//In the book's code they wrote (total == 0), but in the pseudocode they
//wrote if the total is 0 OR LESS. Therefore I wrote (total <= 0).
if (total <= 0){ //Can also be (total < 1)
cout << "I win!" << endl;
break;
}
cout << "Please enter the amount you wish to subtract ";
cout << "and press ENTER." << endl;
cin >> subtract;
//In the book's code they wrote (subtract < 1 || subtract > 2), but in the
//pseudocode they wrote while input IS NOT 1 or 2. Therefore I wrote:
while (subtract < 1 || subtract > total_subtract){
cout << "Please re-enter the amount you wish to subtract ";
cout << "and press ENTER." << endl;
cout << "The amount must be between 1 and ";
cout << total_subtract << " ." << endl;
cin >> subtract;
}
total = total - subtract;
cout << "The new total is " << total << "." << endl;
//In the book's code they wrote (total == 0), but in the pseudocode they
//wrote if the total is 0 OR LESS. Therefore I wrote (total <= 0).
if (total <= 0){ //Can also be (total < 1)
cout << "You win!" << endl;
// break;
}
cout << "Would you like to play again?" << endl;
cout << "If yes, type 1. If not, type 0." << endl;
cin >> play_again;
if (play_again == 1){
cout<< "Great! I love this game!" <<endl;
}
if (play_again == 0){
break;
}
}
break;
}
//system("PAUSE");
return 0;
}
[code]
|