I got my previous question figured out and now I am on a different example in the eBook I am learning from. I need to make a program repeat after the game has finished. I have made the NIM game where you choose a total and then choose to subtract 1 or 2 from the total until the total is 0.
How would I make that program repeat? I have looked at others code and I cannot figure out how to make it repeat.
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main() {
int total, n;
cout << "Welcome to NIM. Pick a starting total: ";
cin >> total;
while (total < 1) {
cout << "Input must be greater than 1" << endl;
cout << "Re-enter: ";
cin >> total;
}
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 num to subtract (1 or 2): ";
cin >> n;
while (n < 1 || n > 2) {
cout << "Input must be 1 or 2." << endl;
cout << "Re-enter: ";
cin >> n;
}
total = total - n;
cout << "New total is " << total << endl;
if (total == 0) {
cout << "You win!" << endl;
break;
}
}
return 0;
}
If you want that code to repeat, then add another while loop above while(total>1). You can prompt the user for another round or just do while(true) to make it play until the user clicks the red X. =)
The loop needs to have the entire game in its body. Take that parenthesis and move it down to the bottom, just above your return 0; statement. Those loops will execute all commands within it's body and nothing else.
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main() {
int total, n;
while (true){ //While declaration here
cout << "Welcome to NIM. Pick a starting total: ";
cin >> total;
while (total < 1) {
cout << "Input must be greater than 1" << endl;
cout << "Re-enter: ";
cin >> total;
}
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 num to subtract (1 or 2): ";
cin >> n;
while (n < 1 || n > 2) {
cout << "Input must be 1 or 2." << endl;
cout << "Re-enter: ";
cin >> n;
}
total = total - n;
cout << "New total is " << total << endl;
if (total == 0) {
cout << "You win!" << endl;
break;
}
}
} /* Put closing bracket here. You want all instructions above this point to repeat.*/
return 0;
}
Remember that while (true) will loop forever. There is nothing in its parenthesis that will eventually equal false.
You need to put a condition inside the while loop that will be false at some point.
What you could do is this (assuming again is a char type):
while(again != 'N')
This will cause the loop to repeat until the user types 'N'
Or you could do this:
while(again == 'Y')
This will cause the loop to repeat as long as the user types 'Y' and nothing else.
The only other thing to note is that you should put your question about playing again down at the bottom, just above the closing bracket. The reason is because once the end of the loop is reached, control will return to the top of the loop, and the condition will be tested again.