CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Beginner help?

  1. #1
    Join Date
    Sep 2011
    Posts
    1

    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;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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 &#37; 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;
    		}

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured