-
Beginner help?
Hey, I've been learning from the book C++ without Fear but I've run into a problem. When I try to compile and run the program it comes up with the error "expected '}' at end of input" on line 29. I don't know what the problem is. Here is the code.
#include <iostream>
using namespace std;
int main() {
int total, n;
cout << "Welcome to NIM. Pick a starting total: ";
cin >> total;
while (true) {
// Pick best response and print results.
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;
}
// Get user’s response; must be 1 or 2.
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;
}
-
Re: Beginner help?
If you learn to line braces up properly and use good indentation, errors like this pretty much solve themselves.
Code:
#include <iostream>
using namespace std;
int main()
{
int total, n;
cout << "Welcome to NIM. Pick a starting total: ";
cin >> total;
while (true)
{
// Pick best response and print results.
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;
}
// Get user’s response; must be 1 or 2.
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;
}