0

I don't understand why this program will not compile please help.
This is what the program supposed to do.

After the correct number is guessed or all five guesses are used and the correct number is not guessed, the user will see a message that shows the secret number and tells how many guesses were used.

Ask the user if he wants to play again. If yes, repeat steps 2 through 5. If not, thank the user for playing and end the 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);
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by SeeTheLite
2

Oh good god, I don't even know where to begin:
-why do you need a goto statement? a loop would do just fine
-you are closing a nonexistent brackets
-42 is not the random number you generated
-and you also have improper brackets
-don't forget srand(time(0));

anyways, a simplified code would be like

int main() {
int guess,ans,try;
bool game = false;
try=0
char ag;

while(!game) {
if(try == 0)
ans = rand()%100+1;

cout<<"guess a number";
cin>>guess;

if(ans == guess) {
cout<<"you win! try again?y/n";
cin<<ag;

if(ag == 'y')
try=0;

else
game=true;
}
else
cout<<"wrong answer, try again"

if(tries==4)
game=true;

try++;
}

system("PAUSE");
return 0;
}

this wont tell if the answer is too high or too low, but I'll leave that to you. Good luck.

Votes + Comments
Good advice.
Good job!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.