mercredi 5 avril 2017

While loop failing to begin ignoring if statements and conditions as a result

I'm working on a program that is a guessing game. However the loop for identifying whether an individual guessed the correct number fails to even begin I get this:

Player 1: Type a number between 0 and 99 and press return:
1
Type the number of guesses that player 2 gets and press return:
1
Sorry you are out of guesses. You lose.

The loop terminates before it even begins and I can't figure out how to make it work.

This is the code for the loop:

  printf( "Type the number of guesses that player 2 gets and press return: \n");
  scanf("%d",&guesses);

  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");
      remainingguesses = guesses - 1;
    }

    else if (secretnumberguess < secretnumber){
        printf("Your guess was less than the secret number.\n");
        remainingguesses = guesses - 1;
    }

    else{
          printf("Your guess was equal to the secret number. You win!\n");
    }
  }

  if (remainingguesses == 0)
    printf("Sorry you are out of guesses. You lose.\n");

I'm fairly new to programming so excuse me for my ignorance.

Here's the full code if you'd like to see that:

#include <stdio.h>

int main()
{

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

  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");
      remainingguesses = guesses - 1;
    }

    else if (secretnumberguess < secretnumber){
        printf("Your guess was less than the secret number.\n");
        remainingguesses = guesses - 1;
    }

    else{
          printf("Your guess was equal to the secret number. You win!\n");
    }
  }

  if (remainingguesses == 0)
    printf("Sorry you are out of guesses. You lose.\n");

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire