Is there a way to make two while loop condition

91 Views Asked by At
#include <stdio.h>

#define MAXLIMIT 1000

#define MINLIMIT 1

int main()
{int number = 0, valid=0;

    do {
        printf("Player 1, enter a number between 1 and 1000:\n");
        scanf("%d", &number);
        valid = number >= MINLIMIT || number <= MAXLIMIT;
        if (!valid) {
            printf("That number is out of range.\n");
        }
    } while (!valid);

    int guess = 0, chance = 10;
    // Allow player 2 to guess and check
    do {
        printf("Player 2, you have %d guesses remaining\n", chance);
        printf("Enter your guess:\n");
        scanf("%d", &guess);
        if (guess < number){
            printf("Too low.\n");
        } else if (guess > number) {
            printf("Too high.\n");
        } else if (guess == number){
            printf("Player 2 wins.\n");
        }
          else if (guess != number && chance == 0)
            printf("Player 1 wins.\n");
    } while (guess != number && chance > 0);
}

This is currently my code. I'm stucked at the last where once the user has use up their 10 chances, Player 1 wins. Is there anyway for two while loop condition to happen?

3

There are 3 best solutions below

1
Vlad from Moscow On

For starters the logical expression

valid = number >= MINLIMIT || number <= MAXLIMIT;

is invalid. You need to use the logical AND operator instead of the logical OR operator

valid = number >= MINLIMIT && number <= MAXLIMIT;

This syntactically incorrect part with do statement

do {
    while (guess != number && chance = 0)
        printf("Player 1 wins. \n")
}

is redundant.

It is enough to write

if ( guess != number )
{
    printf("Player 1 wins. \n");
}

EDIT: After you changed your code in the question then write the if statement within the do-while loop like

    if (guess < number){
        printf("Too low.\n");
    } else if (guess > number) {
        printf("Too high.\n");
    } else
        printf("Player 2 wins.\n");
    }

And after the do-while loop write

    if (  guess != number )
        printf("Player 1 wins.\n");
0
paulsm4 On

SUGGESTION:

Refactor your code:

  • Store information about each player (e.g. "name" and "#/guesses") in a struct.
  • Create an array of players: struct player players[2];
  • Move your "make a guess" code into a function: void guess(int number, struct player * player).
  • Whenever you call "guess()", simply check if the #/guesses for that player has been exceeded.
0
Bob__ On

I won't address the errors in the posted code(s) because the details of this question keeps changing.

Is there anyway for two while loop condition to happen?

Of course we can write suitable conditions to end the do while loops, but it seems to me that it would be more simple to break out when the second player guesses the number and print the winner only after.

int chance = 10;
do {
    printf("Player 2, you have %d guesses remaining\n", chance);
    printf("Enter your guess:\n");
    int guess = 0;
    scanf("%d", &guess);
    if (guess < number){
        printf("%d is too low.\n", guess);
    } else if (guess > number) {
        printf("%d is too high.\n", guess);
    } else {                                // No need to check equality.
        printf("%d is correct\n", guess);
        break;                              // <-- Exit the loop.
    }
    --chance;                               // Don't forget to update this.
} while ( chance > 0 );
 
if ( chance == 0 ) {
    printf("Player 1 wins.\n");
} else {                                    
    printf("Player 2 wins.\n");
}