#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?
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.
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");
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");
}
Related
I just started learning C and I'm trying to create a simple "Guess the Number" game.
Player 1 will enter a number that is between 1 and 1000.
Player 2 will be given 10 chances to guess the number entered by Player 1.
If Player 2's guess is beyond the range (1 to 1000), the system should display "Invalid. Out of range." instead of "Too high" or "Too low".
Currently, my program does not validate whether Player 2's guess is within the range (1 to 1000). Instead, it will just display "Too high" even if Player 2's guess is 2000 which is beyond the range (1 to 1000)
Here is my code:
#include <stdio.h>
int main()
{
int number, guess, count = 10;
printf("Enter a number between 1 and 1000:\n");
scanf("%d",&number);
while(number < 1 || number > 1000)
{
printf("Number is out of range.\n");
printf("Enter a number between 1 and 1000:\n");
scanf("%d",&number);
}
while(count >= 1 && count <= 10)
{
printf("Player 2, you have %d guesses remaining.\n", count);
printf("Enter your guess:\n");
scanf("%d", &guess);
count = count - 1;
if (guess >= 1 || guess <= 1000)
{
if (guess > number)
{
printf("Too high.\n");
}
else if (guess < number)
{
printf("Too low.\n");
}
else if (guess == number)
{
printf("Player 2 wins.\n");
}
}
else
{
printf("Invalid. Out of range.");
}
}
if (count == 0)
{
printf("Player 1 wins.");
}
return 0;
}
You need to change
if (guess >= 1 || guess <= 1000)
to
if (guess >= 1 && guess <= 1000)
to avoid values outside 1 and 1000. Otherwise, an input of 2000, will match the condition guess >= 1 and as per the short circuit property, it'll evaluate the if condition as truthy and control will never go to else block.
guess >= 1 || guess <= 1000 should be guess >= 1 && guess <= 1000
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;
}
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.
I'm working on a program that is a guessing game.
The problem is I have to set remainingguesses to 0 so that the amount of guesses a person can make decreases by 1. But at the same time the condition set in the loop is based on the remainingguesses not being 0. Meaning once it is 0 the loop terminates and moves on.
I don't know how to solve this while making the condition in the loop work properly.
Here's the loop in question:
printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);
remainingguesses = 0;
while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
scanf(" %d",&secretnumberguess);
remainingguesses = guesses - 1;
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{
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");
Here's the full code in question if needed:
#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);
remainingguesses = 0;
while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
scanf(" %d",&secretnumberguess);
remainingguesses = guesses - 1;
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{
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;
Simplified code:
#include <stdio.h>
int main()
{
int secretnumber;
int guesses;
int secretnumberguess;
int flag=0;
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 (guesses > 0 && flag==0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",guesses);
scanf(" %d",&secretnumberguess);
guesses=guesses - 1;
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{
printf("Your guess was equal to the secret number. You win!\n");
flag=1;
}
}
if (guesses == 0 && flag==0)
printf("Sorry you are out of guesses. You lose.\n");
return 0;
}
Here is probably a simple way of coding this.
read guesses;
while (guesses > 0) {
read input;
if (input == secret) {
print "you win!!";
return;
}
else {
print "try again!";
}
guesses--;
}
print "Sorry! You are out of guesses";
I am making a number guessing game program and am having some trouble with my else statements. In the main block where the number is trying to be guessed, the if and if else statements work, but the else statement does nothing. I am trying to make it where a number outside of the range 0 < number < 100 trigger the else statement.
Furthermore, I am trying to make the game repeat itself if '1' is entered but no matter what is entered, the program crashes.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
/*Number guessing game: The number that needs to be guessed is 52*/
int guess;
int attempt = 6;
int playAgain = 1;
printf("Guess the integer that I am thinking of between 1 and 100\n");
printf("I will tell you if you guess too high or too low\n");
printf("You have 6 attempts\n");
printf("What is your first guess?\n");
if (playAgain == 1)
{
while (attempt > 0)
{
scanf_s("%d", &guess);
if (guess > 0)
{
if (guess < 52)
{
attempt--;
printf("Sorry! Too low! You have %d more tries:\n", attempt);
}
}
else if (guess <100)
{
if (guess > 52)
{
attempt--;
printf("Sorry! Too high! You have %d more tries:\n", attempt);
}
}
else if (guess == 52)
{
printf("Correct! You win!\n");
attempt = 0;
}
else
{
printf("Invalid input: Please enter an integer between 1 and 100:\n");
}
}
printf("Enter '1' to play again or anything else to terminate\n");
scanf_s("%d", playAgain);
attempt = 6;
}
else
{
printf("Thanks for playing!\n");
}
return 0;
}
When you use if else if without brackets, make sure it can't be ambigous.
When you do :
if (true)
if (true)
{
}
else if (false)
{
}
How to know if the else if correspond to the first or the second if ? That's why everyone yell at you to put brackets.
if (true)
{
if (true)
{
}
}
else if (false)
{
}
Corrected and simplified version :
Your program crashes because you forgot the & sign inscanf("%d", &playAgain);.
The logic in your program is wrong, you intermix the test if the number is lower, equal or higher than the input with the test if the input is lower than 0 or higher than 100.
In this corrected version the "invalid input" problem is separated from the actuel "number guessing" problem.
Furthermore the number to be guessed (52) is nor longer hard coded but a variable numbertobeguessed is used instead. Later you should enhance the program so that a random number is generated.
int main(void)
{
/*Number guessing game: The number that needs to be guessed is 52*/
int numbertobeguessed = 52 ;
int guess;
int attempt = 6;
int playAgain = 1;
printf("Guess the integer that I am thinking of between 1 and 100\n");
printf("I will tell you if you guess too high or too low\n");
printf("You have 6 attempts\n");
printf("What is your first guess?\n");
if (playAgain == 1)
{
while (attempt > 0)
{
scanf_s("%d", &guess);
if (guess < 0 || guess > 100)
{
printf("Invalid input: Please enter an integer between 1 and 100:\n");
}
else
{
if (guess < numbertobeguessed)
{
attempt--;
printf("Sorry! Too low! You have %d more tries:\n", attempt);
}
else if (guess > numbertobeguessed)
{
attempt--;
printf("Sorry! Too high! You have %d more tries:\n", attempt);
}
else
{
printf("Correct! You win!\n");
attempt = 0;
}
}
}
printf("Enter '1' to play again or anything else to terminate\n");
scanf_s("%d", &playAgain);
attempt = 6;
}
else
{
printf("Thanks for playing!\n");
}
return 0;
}
the else statement which gives message "Invalid input: Please enter an integer between 1 and 100:\n" is considered as the else part of inner most if-else-if statement. there fore that statement is never executed as the execution enters that if-else-if statement only if 0 < guess < 100. so use {} properly to make the proper combination of if-else statements.
Your nesting is wrong. Put brackets for each if and else to make your code work (quickfix), and use proper indentation to make it readable to humans (if you wish).
Here's an example of how things can go wrong (pseudo code):
a = 4
if (a > 0)
if (a < 3)
a = 2
else
a = 3
What do you expect is the end value of a?
Anyway, your:
if (guess > 0)
if (guess < 52)
should become this:
if (guess > 0 && guess < 52)
and your:
else if (guess <100) // this is where the problems start
if (guess > 52)
should become:
else if (guess < 100 && guess > 52)
and your code will work.
hope this helps
#include <stdio.h>
#include <stdlib.h>
int main()
{
int guess; //variable to hold the number from player
int attempts = 6;
char play = 'y';
do
{
while(attempts)
{
printf("\nEnter you guess: "); scanf("%d", &guess);
attempts--;
if(guess>0 && guess <100 )
{
if(guess>52)
printf("\nThat's not it. Try something lower than that.");
else if(guess<52)
printf("\nThat's not the number. Try something higher than that.");
else if(guess==52)
{ printf("\nYou got it!. You won the game.");
attempts = 0; //we're setting it to zero; we don't want the loop to run anymore
}
}
else
printf("\nNumber enter is not in range!");
}//end of while loop
printf("\nDo you want to play again? (y/n): "); scanf("%c", &play);
}while(play=='y' || play=='Y'); //run till player enter 'Y'
return 0;
}
#include <stdio.h>
#include <time.h>
int main (void)
{
int pickedDoor, remainingDoor, hostDoor, winningDoor, option, games = 0, wins = 0;
float frequency = 0;
srand (time(NULL));
while (1)
{
printf ("Pick one of the three doors infront of you, which do you want?\n");
scanf ("%d", &pickedDoor);
if (pickedDoor > 3 || pickedDoor <= 0)
{
break;
}
winningDoor = rand() % 3 + 1;
do
{
hostDoor = rand() % 3 + 1;
} while (hostDoor == pickedDoor || hostDoor == winningDoor);
do
{
remainingDoor = rand() % 3+1;
} while (remainingDoor == pickedDoor || remainingDoor == hostDoor);
printf ("The door the host picked is %d\n", hostDoor);
do
{
printf("Do you want to switch doors? Please enter in the door you want:\n", hostdoor);
scanf("%d", &option);
if (option > 3 || option <= 0)
{return 0;}
}while (option == hostDoor);
if (option == winningDoor)
{
printf("You Won!\n");
wins++;
}
else
{
printf("YOU LOSE!\n");
}
games++;
}
frequency = ((float) wins / games) *100;
printf ("The number of games that you won is %d\n", wins);
printf ("The frequency of winning is %.0f%%\n", frequency);
return 0;
}
Hi, this is my version of the monty hall game show, im getting unexpected results though.
sometimes when I enter in a door for my option it just brings me back to the "pick one of the three doors infront of you" statement, when it should tell me if i have won or lost.
I think this is because the "option" door is equal to the "hostDoor.
I thought having "option != hostDoor" would fix it but it does not.
If i am correct in that assumption how can I fix it? If not why is it not working and how can I fix it?
To simulate correctly, OP needs to show the host door.
do {
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
if (option > 3 || option <= 0 ) {
return 0;
}
} while (option == hostDoor);
// instead of
#if 0
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
if (option > 3 || option <= 0 ) { return 0; }
#endif
To deal with OP " it should tell me if i have won or lost." problem, change
else if (option == remainingDoor)
to
else
Your scanf("%d", &option) is OK. I prefer the fgets()/sscanf() combo and its alway useful to check the result of scanf() family, but that is not your issue here.
Its because of these:
scanf ("%d", &pickedDoor);// reads \n after the last input
scanf("%d", &option); // reads \n after the last input
**option != hostDoor; // completely useless .. get rid of it**
I would suggest putting a getchar() after each scanf to get rid of the \n character
so something like this:
#include <stdio.h>
#include <time.h>
int main (void)
{
int pickedDoor, remainingDoor, hostDoor, winningDoor, option, games = 0, wins = 0;
char collect; //variable to collect any extra input like \n
float frequency = 0;
srand (time(NULL));
while (1)
{
printf ("Pick one of the three doors infront of you, which do you want?\n");
scanf ("%d", &pickedDoor);
collect = getchar(); // get rid of the \n from the input stream
printf("collect = %c\n",collect);
if(collect!='\n'){ // is it actually a \n or did you take in something else
putchar(collect); // if it isn't \n put it back
}
if (pickedDoor > 3 || pickedDoor <= 0)
{
break;
}
winningDoor = rand() % 3 + 1;
do
{
hostDoor = rand() % 3 + 1;
} while (hostDoor == pickedDoor || hostDoor == winningDoor);
do
{
remainingDoor = rand() % 3+1;
} while (remainingDoor == pickedDoor || remainingDoor == hostDoor);
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
collect = getchar(); // get rid of the \n from the input stream
printf("collect = %c\n",collect);
if(collect!='\n'){ // is it actually a \n or did you take in something else
putchar(collect); // if it isn't \n put it back
}
if (option > 3 || option <= 0 )
{
return 0;
}
if (option == winningDoor)
{
printf("You Won!\n");
wins++;
}
else if (option == remainingDoor)
{
printf("YOU LOSE!\n");
}
games++;
}
frequency = ((float) wins / games) *100;
printf ("The number of games that you won is %d\n", wins);
printf ("The frequency of winning is %.0f%%\n", frequency);
return 0;
}
Another more efficient way would be to use fgets or to have error checks on scanf() itself
srand and rand belongs to stdlib header file
#include<stdlib.h> // include this header file
option != hostDoor; // this statement does not do anything
and your else if (option == remainingDoor) should be else { printf("you lose");}