I have an issue with this assignment, the instructor says that I use the brackets incorrectly that is why the program will not compile. Please provide feedback to correct program.

#include <iostream>
using namespace std;

int main()
{
    system("color 4");
    restart:
    int number, ans;

      number = rand() % 100 + 1;

      int guess, tries=5;

      do {
            cout << "Can you guess my secret number? You have 5 tries, (1-100). "<< endl;
            cout << "Enter guess number: ";

            cin >> guess;

            if (guess < number)

                  cout << "Too low, try again!" << endl;

            else if (guess > number)

                  cout << "Too high, try again!" << endl;

            else if (guess == 42);

                  cout << "Your guess is right!" << endl;
                  }                  
}                                       

           if (tries > 5)
           {
           cout << "You haven't guess my secret number. The secret number is 42. Would you like to start again?";
           cout << "Press 'Y' or 'y', " << endl;
           cin >> ans;

           else if (ans != 'Y' || ans != 'y')
              {
              goto restart;
              }

}     
              system("PAUSE");
              return(0);
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I have an issue with this assignment, the instructor says that I use the brackets incorrectly that is why the program will not compile. Please provide feedback to correct program.

Rule 1 when debugging brackets issues. Use code tags. All spacing is stripped out otherwise.

Rule 2 when debugging brackets issues. Constant indentation. Don't mix tabs and spaces. 4 spaces (or 5 or whatever. Just keep in the same.) indentation per nested block of code. Bracket problems stick right out.

Rule 3 when debugging brackets issues. Have some system for vertical spacing. Blank lines when a new nested block of code STARTS or ENDS can be confusing. You want blocks to stand out.

Thus, lines 46 and 47 are indented way too far, line 45 not enough, line 42 should be indented farther than lines 41 and 43, get rid of the blank spaces on lines 21, 25, etc., stuff like that. You need a system.

Count starting and ending brackets. They had better be the same number. Again, good indentation is a must because sometimes there is the right number of brackets, but one is MISPLACED.

With all THAT said, I'm not sure brackets are even the problem. They might be, but in addition, I see a "do" without a "while"? Or am I missing it?

#include <iostream>
using namespace std;

int main()
{
    system("color 4");
    restart:
    int number, ans;

      number = rand() % 100 + 1;

      int guess, tries=5;

      do {
            cout << "Can you guess my secret number? You have 5 tries, (1-100). "<< endl;
            cout << "Enter guess number: ";

            cin >> guess;

            if (guess < number)

                  cout << "Too low, try again!" << endl;

            else if (guess > number)

                  cout << "Too high, try again!" << endl;

            else if (guess == 42);

                  cout << "Your guess is right!" << endl;
                  }                  
}                                       

           if (tries > 5)
           {
           cout << "You haven't guess my secret number. The secret number is 42. Would you like to start again?";
           cout << "Press 'Y' or 'y', " << endl;
           cin >> ans;
           
           else if (ans != 'Y' || ans != 'y')
              {
              goto restart;
              }

}     
              system("PAUSE");
              return(0);
}

Use [ code] [ /code] tags. You are missing a the while part
in your do while loop.

Using goto will get you killed (not really) in the c++ community.

Using goto will get you killed (not really) in the c++ community.

Maybe not killed, but laughed at, and your colleagues will implant viruses on your computer.

Maybe not killed, but laughed at, and your colleagues will implant viruses on your computer.

After that, who would want to live?

Just looking at the code quickly I can see a couple of bracket errors. Every if statements need brackets. Google for an if statement tutorial. For example:

if (guess < number)

cout << "Too low, try again!" << endl;

Should be:

if (guess < number) {
cout << "Too low, try again!" << endl;
}

Also I believe there is an extra bracket after the do loop, and an extra semicolon after an else if statement.

Also I have read that using the system function is dangerous and it should be avoided. Use cin.get();

I believe you should also initiliaze the rand function as null using the srand function (which needs the ctime header). Google it.

Also you declare ans as an int but you use it as a char.

You should not use the goto function.

Another error is when asking if the user want to start playing again.

else if (ans != 'Y' || ans != 'y')
{
goto restart;
}

The error is that you use !=, which means if ans DOESN'T equal "y".
You should use the == operator.

There you go, try fixing all those programs, and then tell us how it went.

Every if statements need brackets.

No!!!

if (guess < number)

cout << "Too low, try again!" << endl;

Should be:

if (guess < number) {
cout << "Too low, try again!" << endl;
}

These are the exact same thing. If the block of code to be executed is only one line, no brackets are needed, though they don't hurt.

No!!!


These are the exact same thing. If the block of code to be executed is only one line, no brackets are needed, though they don't hurt.

True. The compiler doesn't care of which way you use it,however, since
you are starting, you should use brackets so you can save mistakes from happening.

Comments
A consistent approach over option syntax will definitely save trouble at some point - good

Oh I didn't knew that but I still think he should use brackets, it looks more organize and clean.

Oh I didn't knew that but I still think he should use brackets, it looks more organize and clean.

Yes, it's not a bad idea at all, especially in the beginning. Eventually most people will drop the brackets (but keep the indentation) as they get more comfortable, but it can be useful to ALWAYS do it for a while till you get the hang of it since it definitely doesn't hurt anything, and it can help.

Many of the commercial coding standards I've come across insist on using braces in all contexts, even the optional braces in 1-liner statements.

If you're using a syntax-driven editor, they come for free anyway, so removing them is work.

It also prevents "Arrgg!" moments, when attempting to add debug to say

if ( i < 10 )
    i++;
    printf( "i is now %d\n", i );

Coupled with bad indentation, and something less obvious than a printf, and it could be a while before you figure it out.

Basically, if there is one way which always works, then use it always.
Then by habit, that aspect of the program will always be correct.

If you have a special case you treat differently, then bugs appear when it ceases to be a special case and you don't notice the transition.

This question has already been answered. Start a new discussion instead.