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

耗尽温柔 提交于 2019-12-08 11:44:26

问题


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

回答1:


Your remainingguesses is causing you to not enter the loop. Perhaps instead of using that as a check use a flag and inside the loop check for remaining guesses in an if loop. And inside the if loop change the flag accordingly.



来源:https://stackoverflow.com/questions/43244415/while-loop-failing-to-begin-ignoring-if-statements-and-conditions-as-a-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!