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;
}
Related
#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");
}
I'm taking an intro to C programming course, and I'm trying to understand pointers, and how exactly they work. I tried to pass just the variable counter through gameRuntime();, but counter wasn't being returned towards after the while loop. So, I opted to use a pointer. And now I'm in a bigger mess than I started.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *gameRuntime(int *counter)
{
srand(time(NULL));
int random = rand() % 100 + 1;
printf("> %d <\n", random);
int guess;
*counter = 1;
int local_counter = (*counter);
while ((guess != random) && (local_counter <= 10))
{
printf("\n-----------------------\n");
printf("What is your guess?\n-----------------------\n> ");
scanf("%d", &guess);
if ((guess > 100) || (guess < 1))
{
printf("Please choose a number between 1 and 100\n");
}
else if (guess < random)
{
printf("My number is larger than %d!\n", guess);
local_counter++;
printf("%d guesses left!\n", (11 - local_counter));
}
else if (guess > random)
{
printf("My number is less than %d!\n", guess);
local_counter++;
printf("%d guesses left!\n", (11 - local_counter));
}
else if (guess == random)
{
printf("You guessed it! My number was %d!\n", random);
printf("You found it with just %d guess(es)!\n", local_counter);
}
}
(*counter) = local_counter;
return counter;
}
int main()
{
char quit;
int counter;
int random;
printf("Welcome to the Number Guess Game!\n");
printf("I chose a number between 1 and 100 and you have only 10 chances to guess it!\n\n");
printf("Continue? [Y\\N]\n\n> ");
scanf("%c", &quit);
if ((quit == 'n') || (quit == 'N'))
{
printf("Exiting....");
return 0;
}
else
{
printf("\n=*=+=*=+=*=+=*==*=+=*=+=*=+=*==*=+=*=+=*=+=*==*=+=*=+=+*\n");
printf(" ~ ~ ~ Ready? ~ ~ ~ \n");
printf("=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*\n");
printf("\n");
printf("\n-----------------------");
printf("\nOk, I've made up my mind!\n");
printf("-----------------------\n");
}
gameRuntime(counter);
printf("\n---%d---\n", counter);
char continueGame;
while ((continueGame != 'N') || (continueGame != 'n'))
{
printf("\n---%d---\n", counter);
if (counter >= 10)
{
printf("SORRY! You could not find my number with 10 guesses!\n");
printf("My number was %d\n", random);
printf("Maybe next time!\n");
printf("\nTry again? [Y\\N]\n");
scanf(" %c", &continueGame);
if ((continueGame == 'Y') || (continueGame == 'y'))
{
gameRuntime(counter);
}
else
{
printf("Thanks for playing! See you later!");
return 0;
}
}
else
{
printf("Play again? [Y\\N]\n> ");
scanf(" %c", &continueGame);
if ((continueGame == 'Y') || (continueGame == 'y'))
{
gameRuntime(counter);
}
else
{
printf("\nThanks for playing! See you later!");
return 0;
}
}
}
}
Any help would be much appreciated, TIA
Change
gameRuntime(counter);
to
gameRuntime(&counter);
You are passing the value of counter, and then you used it like if it was the address of something (you dereferenced the pointer). It does compile because pointers and ints are interchangeable in c, but that doesn't mean that the behavior is defined. It should however, generate a warning about pointer to int conversion. You can also, convert all warnings to errors to prevent compilation in case of a warning.
Warnings can be ignored in very rare cases, so as a rule of thumb make your compiler warn about everything it can and if possible, let it treat warnings as errors.
By applying the suggested fix, you pass the address of counter, which is a pointer holding the location of counter in memory, thus you can then affect the value at that location by dereferencing the pointer.
I wanted to practice a little with separating logic into functions and using basic recursion in a primitive "Guess my number game", just as a sort of way to see where I'm at in my C programming.
Here's the code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
char playAgain()
{
printf("\nDo you wanna play again? ");
char resp = 0;
while (TRUE) {
scanf("%c", &resp);
if (resp == 'y') break;
if (resp == 'n') break;
}
return resp;
}
void play()
{
srand((int)time(NULL));
int num = rand() % 10 + 1;
int guess = 0;
int flag = 0;
int attempts = 0;
printf("\nGuess the number: \n");
while (TRUE) {
scanf("%d", &guess);
if (num > guess)
{
printf("Too low! ");
attempts++;
}
else if (num < guess)
{
printf("Too high! ");
attempts++;
}
else if (num == guess)
{
attempts++;
printf("You won! You did it in %d attempts", attempts);
char yo = playAgain();
if (yo == 'y') play();
else if (yo == 'n') exit(0);
else {
printf("Error!");
exit(1);
}
}
}
}
int main()
{
return play();
}
Everything works but I've only managed to make it quit when the user says 'n', by using exit(0) and I've heard it's bad practice. But having spent a couple of hours just noodling around and trying all the other ways I could think of (using flags, for example), I just couldn't make it work. It works for 'y', but as soon as I enter 'n', it doesn't quit and just calls the playAgain() function one more time, or freezes altogether (nothing happens).
I feel ashamed for not being able to solve this, but I'm out of ideas. Is there any other way, other than exit(0), to make it jump out of play() straight to return 0 in main()? The problem seems to be that I have an infinite game loop, so when the answer is 'n', I need to break out of the loop AND out of the play() function, and that has proven to be problematic. I remember being able to do this easily when I had no functions, but just all the relevant code in main(), but the whole point of this is to use functions. For example, I can't figure out how to make use of a return type of play(), so that it knows when to quit.
P.S. As always, seeing how often people get accused of this, I assure you this is not homework because I am, and always have been, doing programming strictly as a hobby. There's no professor I answer to, apart from you :-) And trust me that I did try to figure out a solution, but nothing seems to work. It's a question of good vs. bad design, so it's especially important for my self-teaching. exit(0) seems to be a hackish solution and serves no educational purpose.
P.P.S. By the way, I'm using xCode, and the program runs in its output window. Probably not, but maybe that's the reason quitting with 'n' doesn't work?
SUMMARY: The code works fine except when the answer is 'n', in which case it just asks "Do you wanna play?" again, or doesn't do anything at all.
Make your play function return a result:- int play( void ) {
then instead of exit(1) and exit(0); use return 1; and return 0;
at the end of play put return 0;
then in main
{
return play();
}
instead of recursion, just make a loop
int play( void)
{
srand((int)time(NULL));
int num;
int guess;
int flag;
while(1)
{
num = rand() % 10 + 1;
guess = 0;
flag = 0;
printf("\nGuess the number %d: \n", num);
while (1) {
scanf("%d", &guess);
if (num > guess)
{
printf("Too low! ");
}
else if (num < guess)
{
printf("Too high! ");
}
else if (num == guess)
{
printf("You won!");
char yo = playAgain();
if (yo == 'y') break;
else if (yo == 'n') return 0;
else {
printf("Error!");
return 1;
}
}
}
}
}
and if you want more of a game engine type approach
enum {
GUESS,
GAMEOVER,
QUIT,
} GAME_STATES;
typedef struct
{
enum GAME_STATES state;
int num;
int game_over;
} guessing_game_t;
void init_game(guessing_game_t* g)
{
g->state = GUESS;
g->game_over = 0;
g->num = rand() % 10 + 1;
}
void evaluate_guess(guessing_game_t* g, int guess)
{
if(g->num == guess)
{
printf("You won!");
g->state = GAMEOVER;
}
else if (g->num > guess)
{
printf("Too low! ");
}
else if (g->num < guess)
{
printf("Too high! ");
}
}
int input_guess( void )
{
int guess;
scanf("%d", &guess);
return guess;
}
void play_again( guessing_game_t* g)
{
char resp = 0;
printf("\nDo you wanna play again? ");
scanf("%c", &resp);
if (resp == 'y') init_game(g);
if (resp == 'n') g->state = QUIT;
}
void play( guessing_game_t* g)
{
while(g->state != QUIT)
{
switch(g->state)
{
case GUESS:
evaluate_guess(g, input_guess());
break;
case GAMEOVER:
play_again(g);
break;
}
}
}
int main()
{
guessing_game_t game;
init_game(&game);
play(&game);
}
You can put a flag before going on with the loop
Change the while(true) to while (continue_play), continue_play is the name of flag for an example
Then,
if (yo == 'n') exit(0);
can be change to
if (yo == 'n') continue_play=false;
and do the similar for the exit(1) case
continue_play then will be a flag to exit from the function.
You have one more thing that the program will end: the logic of your code is made like that u can also use break; instead of exit(0) and exit(1) because you use while loop and you will break it and the program will end.
Since you may recursive call you play() function, I think exit it with exit(0) is a proper choose.
Acutall, I do not think using recursion to implement play() is a good idea at all, two infinite loops are enough. You could implemet it like this:
void play(void)
{
srand((int)time(NULL));
int num;
int guess = 0;
int attempts = 0;
int try_again = 1;
while (try_again) {
num = rand() % 10 + 1;
printf("\nGuess the number: \n");
while (TRUE) {
scanf("%d", &guess);
if (num > guess)
{
printf("Too low! ");
attempts++;
}
else if (num < guess)
{
printf("Too high! ");
attempts++;
}
else if (num == guess)
{
attempts++;
printf("You won! You did it in %d attempts", attempts);
char yo = playAgain();
if (yo == 'n') try_again = 0;
break;
}
}
}
}
The game works fine my first time through, although; the second time it only gives you two lives... I have tried to change the number of lives but still can't figure out what I'm doing wrong.
// C_program_random_number_game
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
int main()
{
srand(time(NULL));
int num1,x = 0;
char game, cont, replay;
printf("Would you like to play a game? : ");
scanf("%c",&game);
if (game == 'y' || game == 'Y')
{
printf("\nThe rules are simple. You have have 5 tries to guess the computers number. \n \n If you succeed you win the game, if you dont you lose the game. Good luck!");
do
{
int r = rand()%5 +1;
printf("\n\nEnter a number between 1 and 5 : ");
scanf("\n%d",&num1);
x++;
if(num1 > 0 && num1 < 5)
{
do
{
if(num1 < r)
{
printf("\nClose! try a little higher... : ");
x++;
}
else if (num1 > r)
{
printf("\nClose! try a little lower...: ");
x++;
}
scanf("%d",&num1);
}while(num1!=r && x <3);
if(num1 == r)
{
printf("\nWinner! >> you entered %d and the computer generated %d! \n",num1, r);
}
else if(num1 != r)
{
printf("\nBetter luck next time!");
}
printf("\n\nWould you like to play again? (y or n) : ");
scanf("\n%c",&replay);
}
else
{
printf("Sorry! Try again : ");
scanf("%d",&num1);
}
}while(replay == 'y'|| replay == 'Y');
}
else if (game == 'n' || game == 'N')
{
printf("Okay, maybe next time! ");
}
else
{
printf("Sorry, invalid data! ");
}
return 0;
}
There are all kinds of issues with you code (most of them are minor in terms of programming). Most of the errors are typos in what you want done via this question and what you printf().
As is, this code will random between 1-25, accept an input of any valid int, see if you matched it, and only give you 5 tries. (I didn't add error checking to enforce that the input is between 1-25. That should probably be added.)
I commented my code below with all my changes and went by that you had in the printf()s.
Note: See my comments above for explanations of my changes since I already pointed them out. I also formatted it so its a little more easy to read.
Note2: I did this quickly using an online compiler. If you find anything wrong with this or not working as you'd like, just comment below and I'll address it.
// C_program_random_number_game
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
int main()
{
srand(time(NULL));
int num1,x = 0;
char game, cont, replay;
printf("Would you like to play a game? : ");
scanf("%c",&game);
if (game == 'y' || game == 'Y')
{
printf("\nThe rules are simple. You have have 5 tries to guess the computers number. \n \n If you succeed you win the game, if you dont you lose the game. Good luck!");
do
{
int r = rand()%25 +1;
printf("\n\nEnter a number between 1 and 25 : ");
scanf("%d",&num1);
do
{
printf("r = %d\n", r);
if(num1 < r)
{
printf("\nClose! try a little higher... : ");
x++; //Increment x if wrong guess
}
else if (num1 > r)
{
printf("\nClose! try a little lower...: ");
x++; //Increment x if wrong guess
}
scanf("%d",&num1);
}while(num1!=r && x < 5); //If x is 5 or more, they ran out of guesses (also, you want an && not an ||)
if(num1 == r) //Only print "winner" if they won!
{
printf("\nWinner! >> you entered %d and the computer generated %d! \n",num1, r);
}
printf("\nWould you like to play again? (y or n) : ");
scanf("\n%c",&replay);
}while(replay == 'y'|| replay == 'Y');
}
printf("Thanks for playing! ");
if (game == 'n' || game == 'N')
{
printf("Okay, maybe next time! ");
}
return 0;
}
There are a combination of two problems. The first is that you're not breaking out of the "for" loop when the number matches. Therefore the match is only checked on every third guess.
The second problem is in this logic check:
}while(num1!=r || x <= 3);
We see that this turns into "true" if the for loop is broken out of early.
#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");}