So I'm creating a program that's a guessing game. It's supposed to ask player 1 to enter a number between 0 and 99, if they enter anything outside of that the code should ask them to retry this.
Then it will ask for a number of guesses/attempts Player 2 has to enter what they think the number is. If it's too high the program will say so, if it's too low the program will say so. If it's just right- Player 2 wins and if they run out of guesses the program is to say that too.
My problem is with the second half of this code. Where I'm trying to fit in the number of guesses with the remaining guesses. The loop isn't working; here's the code:
int secretnumber;
int guesses;
int secretnumberguess;
int remainingguesses;
while (1) {
printf("Player 1: Type a number between 0 and 99 and press return:\n");
scanf(" %d",&secretnumber);
if (secretnumber > 99 || secretnumber < 0) {
printf("Secret number cannot be greater than 99 or below 0.\n");
continue;
}
break;
}
printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);
remainingguesses = guesses - 1;
while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",&remainingguesses);
scanf(" %d",&secretnumberguess);
if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
else if (secretnumberguess < secretnumber)
printf("Your guess was less than the secret number.\n");
else (secretnumberguess == secretnumber)
printf("Your guess was equal to the secret number. You win!\n");
continue;
}
break;
}
if (remainingguesses == 0)
printf("Sorry you are out of guesses. You lose.\n");
I'm entirely new to this and trying to debug and fix my code. But loops are truly a nightmare for me especially when you throw if statements into the mix. I realize I'm very incompetent and I apologize but could someone help me as I'm totally confused on the second half of my code.
Aucun commentaire:
Enregistrer un commentaire