Hi I am kind of stuck on one aspect of a project that I am currently working on. I have a code for a number guessing game in "C" I need the program to ask the player if they want to play again after the game is done and. I have everything writen right but somthing is worng please help.

Attachments
include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
  int runagain=0;
  srand(time(NULL));
  int randnum=rand()%100 +1;
  int guess=0;


      do{

        printf("Enter a number between 1 and 100\n");
        scanf("%d",& guess);
        if (guess<randnum)

        printf(" to low enter higher number");

        if (guess > randnum)

        printf("to high enter lower number");
       if(guess==randnum)

        printf( "You got it!\n");
        printf("wolud you like to play again (y or n)?\n");
        scanf("%d", & runagain);
        }while(runagain=='y'||runagain=='Y');


return 0;
}

-- INSERT --

I have everything writen right but somthing is worng please help.

Could we have a bit more detail?

when I run the program it runs and ask me to enter a number 1 and 100 then it jumps to the ends asking me if i want to play again

The programs runs and ask to enter a guess number. When I enter a guess number it jumps right to asking if I want to play again then it ends.

Hi I am kind of stuck on a guessing game project that I am currently working on. I have a code for a number guessing game in "C" I need the program to ask the player if they want to play again after the game is done and. I have everything writen right but somthing is worng please help. the program runs it ask enter the guess number then it just ends.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
  int runagain=0;
  srand(time(NULL));
  int randnum=rand()%100 +1;
  int guess=0;


      do{

        printf("Enter a number between 1 and 100\n");
        scanf("%d",& guess);
        if (guess<randnum)

        printf(" to low enter higher number");

        if (guess > randnum)

        printf("to high enter lower number");
       if(guess==randnum)

        printf( "You got it!\n");
        printf("wolud you like to play again (y or n)?\n");
        scanf("%d", & runagain);
        }while(runagain=='y'||runagain=='Y');


return 0;
}

You are declaring the variable runagain as an integer, but in line 29, you are comparing it to characters. I changed runagain to a character, and changed the scanf on line 28 to take a character. Also, it appeared that after doing that, the scanf command for runagain on line 28 was grabbing the leftover newline from after guess was entered, so I added:

getchar ();

after the line 16 scanf command. That grabbed the newline character and allowed the scanf command to pause for user input in line 28. Making those changes seemed to work for me.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
  char runagain='y';
  srand(time(NULL));
  int randnum=rand()%100 +1;
  int guess=0;


      do{

        printf("Enter a number between 1 and 100\n");
        scanf("%d",& guess);
        getchar(); 
        if (guess<randnum)

        printf(" to low enter higher number");

        if (guess > randnum)

        printf("to high enter lower number");
       if(guess==randnum)

        printf( "You got it!\n");
        printf("wolud you like to play again (y or n)?\n");
        scanf("%c", & runagain);
        }while(runagain=='y'||runagain=='Y');


return 0;
}

You are declaring the variable runagain as an integer, but in line 29, you are comparing it to characters. I changed runagain to a character, and changed the scanf on line 28 to take a character. Also, it appeared that after doing that, the scanf command for runagain on line 28 was grabbing the leftover newline from after guess was entered, so I added:

getchar ();

after the line 16 scanf command. That grabbed the newline character and allowed the scanf command to pause for user input in line 28. Making those changes seemed to work for me.

ok it works but when the game ask would you like to play again you type yes it repeats and the same guesse number repeats. What I mean is that lets say u got the guessed number right and its 30 the second time it ask if you want to play again you say yes
You play and the guess number the second time is the same the first time 30.

ok it works but when the game ask would you like to play again you type yes it repeats and the same guesse number repeats. What I mean is that lets say u got the guessed number right and its 30 the second time it ask if you want to play again you say yes
You play and the guess number the second time is the same the first time 30.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
  char runagain='y';
  srand(time(NULL));
  int randnum=rand()%100 +1;
  int guess=0;


      do{

        printf("Enter a number between 1 and 100\n");
        scanf("%d",& guess);
        getchar(); 
        if (guess<randnum)

        printf(" to low enter higher number");

        if (guess > randnum)

        printf("to high enter lower number");
       if(guess==randnum)

        printf( "You got it!\n");
        printf("wolud you like to play again (y or n)?\n");
        scanf("%c", & runagain);
        }while(runagain=='y'||runagain=='Y');


return 0;
}

Try moving line 9 to a location inside the Do-While loop. Relocate line 9 to line 14. That will generate a new random number each time you go through the loop. Currently you're only generating it once.

I did that but it is generateing new number every time it ask enter a number

I did that but it is generateing new number every time it ask enter a number

Well, that's because it is starting a new game. So do you want the person to have more than one guess for an individual game? If so, you would need some type of nested loop within your DO-WHILE loop. If the player gets 5 guesses, this could be a for-loop like this:

guessCorrect = false;
for (i = 0; i < 5 && !guessCorrect; i++)
{
     // code for asking for a guess and checking
     // whether guess is correct.
}

The loop above would be inside your DO-WHILE loop. If the player gets to guess until the right number is guessed, it would probably be in the form of a while loop:

guessCorrect = false;
while (!guessCorrect)
{
     // code for asking for a guess and checking
     // whether guess is correct.
}

This too would be inside the DO-WHILE loop. guessCorrect in either case is a boolean loop control variable initialized to false initially. If the right answer is given, set guessCorrect to true to exit this inner loop.

sorry I did not explain it right. what happens is. when I Try moving line 9 to a location inside the Do-While loop. line 14 so it can generate a new random number each time it go through the loop it generates continues numbers not allowing person to guess the number.

sorry I did not explain it right. what happens is. when I Try moving line 9 to a location inside the Do-While loop. line 14 so it can generate a new random number each time it go through the loop it generates continues numbers not allowing person to guess the number.

Post your modified code and I'll run it and see if it does the same for me.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>


int main(void)
{
char runagain='y';
srand(time(NULL));
int randnum=rand()%100 +1;
int guess=0;



do{
int randnum=rand()%100 +1;
printf("Enter a number between 1 and 100\n");
scanf("%d",& guess);
getchar();
if (guess<randnum)


printf(" to low enter higher number");


if (guess > randnum)


printf("to high enter lower number");
if(guess==randnum)


printf( "You got it!\n");
printf("wolud you like to play again (y or n)?\n");
scanf("%c", & runagain);
}while(runagain=='y'||runagain=='Y');



return 0;
}

Edited 3 Years Ago by happygeek: fixed formatting

I ran your code. It asked me to guess once each time through the loop. It did give me a line like this:

to low enter higher numberwolud you like to play again (y or n)?

Is that the problem you are referring to? How many chances are they supposed to get to guess the number before another one is generated? Currently you are only asking for one guess. You display this:

to low enter higher number

but you never prompt the user for that number. Instead you immediately prompt the user whether they want to play again, which I assume means they don't get to guess again. So your two prompts run together into this:

to low enter higher numberwolud you like to play again (y or n)?

You have two prompts for user input, but only one scanf for that input. However, I did not get the infinite loop you were referring to (I think) where I never got to enter another guess. Here is your code, formatted. Please use code tags. It preserves formatting. Please note that you have printf commands in lines 21 and 26, but no corresponding scanf for a second guess, if that is what you intend.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    char runagain = 'y';
    srand(time(NULL));
    int randnum = rand() % 100+1;
    int guess = 0;


    do
    {
        int randnum = rand() % 100+1;
        printf("Enter a number between 1 and 100\n");
        scanf("%d", &guess);
        getchar();
        if (guess < randnum)
        {
            printf(" to low enter higher number");
        }

        if (guess > randnum)
        {
            printf("to high enter lower number");
        }

        if (guess == randnum)
        {
            printf("You got it!\n");
        }
            
        printf("wolud you like to play again (y or n)?\n");
        scanf("%c", &runagain);
    }
    while (runagain == 'y' || runagain == 'Y');

    return 0;
}

What you need, in addition to learning how to use CODE tags, is two loops. Loop 1 is for each game. Inside that loop is loop 2 for each guess. You are using one loop for guessing and winning which is very difficult to handle.

The first thing in the outer loop is calculating the number to guess.
The inner loop exits only when the number is guessed.
Then the question about playing again is the last thing in the outer loop.

This article has been dead for over six months. Start a new discussion instead.