Reputation: 5
The code is as follows: guessing game , 10 guesses max, prompts user for replay. It crashes after one run. It works fine without the replay() module but then I can't incorporate the replay option. I have tried several different things to no avail. Please kindly help me resolve this in a timely manner. Help is appreciated in advance. thanks
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guessGame ();
int replay();
int main()
{
int selection;
printf("Welcome to the Number Guess Game! I choose a number between 1 and 100 and you\nhave only 10 chances to guess it!");
do
{
printf("\n\nok, I made my mind!");
guessGame();
replay();
}
while (replay() != 0);
printf("Thank you! have a nice day.\n");
return 0;
}
void guessGame()
{
int attempt,guess;
srand(time(NULL));
int r = rand () % 100 + 1;
for (guess = 1; (guess < 11 && attempt != r); guess = guess + 1)
{
printf("\nWhat is your guess> ");
scanf("%d",&attempt);
if (attempt < 1 || attempt > 100)
{
printf("Invalid guess!\n");
guess = guess - 1;
}
else
{
if (attempt > r && guess < 10)
printf("My number is smaller than %d\n",attempt);
else if (attempt < r && guess < 10)
printf("My number is larger than %d\n",attempt);
}
if ((guess < 9) && (attempt >= 1 && attempt <= 100))
printf("%d guesses left.\n",(10 - guess));
if ((guess == 9) &&(attempt >= 1 && attempt <= 100))
printf("%d guess left.\n",(10 - guess));
}
if (attempt == r)
{
printf("You did it! My number is %d.\nYou did it in %d guesses.\n",r,guess);
}
if (guess >= 10 && attempt != r)
{
printf("SORRY! you couldn't guess it with 10 guesses.\nMy number was %d. Maybe next time!\n",r);
}
}
int replay()
{
char selection;
printf("\nDo you want to play again");
scanf("%c",selection);
if (selection == 'N')
return 0;
else
return 1;
}
Upvotes: 0
Views: 76
Reputation: 782775
You're calling scanf()
incorrectly. You need to give the address of the variables to store into:
scanf("%c", &selection);
Upvotes: 1