0

I'm a beginner in C programming and i would appreciate if i could get some tips on how to set a program to restart? I'm currently building a guessing game, where the user has 10 attempts to guess the secret number which is provided randomly. I want the program to be able to offer the user a new round of game from start (Attempt number 1 Guess the number:), meaning re-run the program.

Here is the program:

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

#define guessLimit 10

int main()
{

    int secret_number;
    int guess;
    int guessCount = 0;
    int outofGuesses = 0;
    int i;

    setbuf(stdout, NULL);

    srand(time(0));
    secret_number = rand() % 100;

    printf("\n---GUESS THE SECRET NUMBER---\n");


    for(i=1; i < 11; i++){
        printf("Attempt number %d Guess a number: ", i);
        scanf("%d", &guess);
        if(guess == secret_number){
            printf("Correct number!\n"); 
            break;   
        }   
    
        if(guess < secret_number){
            printf("sorry, number too small.\n");
        }
        else if(guess > secret_number){
            printf("Sorry, number too big.\n");
        }
        if(i==10){
            printf("Out of Attempts"); 
        }
        if(guess>99 || guess<0){
            printf("Out of Range.\n");
       }
    }    

    return 0;
}
4
  • 2
    You want to learn what "loops" are. They are how you repeat things in programs: learn-c.org/en/For_loops Commented Jun 23, 2020 at 12:34
  • 1
    They already have a for loop - so presumably they understand how those work. I would suggest a while loop. Commented Jun 23, 2020 at 12:35
  • 1
    Put the code for one whole game in another function, and call that in a loop from main(). Except srand(time(0)); which should be called only once. Commented Jun 23, 2020 at 12:41
  • As a side note - giving the user 10 chances to guess a number in the range 1-100 is too generous if you're providing "higher/lower" feedback. If my calculations are correct, a binary search would find the answer in maximally log2(100)=6.64... attempts. In other words, you should be able to find the answer in no more than 7 attempts if you know what you're doing. A binary search works of course by guessing the number in between the bounds and then adjusting the bounds according to your feedback. Commented Jun 23, 2020 at 13:03

3 Answers 3

2

You could encapsulate your for loop in a while loop and have the conditional be an input from the console to indicate the user is done playing.

Sign up to request clarification or add additional context in comments.

Comments

2

The best thing to do is to wrap the primary routine within a while loop and use a condition to determine if you want to either repeat or exit the loop. In this case, the do while construct works nicely. Simply ask the user if they would like to play again at the end of the loop. If not, then exit. Otherwise, repeat the code. Be mindful not to call srand(time(0)) within your loop or you reset the random sequence.

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

#define guessLimit 10

int main()
{

    int secret_number;
    int guess;
    int guessCount = 0;
    int outofGuesses = 0;
    int i;
    char play;

    srand(time(0));

    do {
        secret_number = rand() % 100;

        printf("\n---GUESS THE SECRET NUMBER---\n");


        for(i=1; i < 11; i++){
        printf("Attempt number %d Guess a number: ", i);
        scanf("%d", &guess);
        if(guess == secret_number){
            printf("Correct number!\n");
            break;
        }

        if(guess < secret_number){
            printf("sorry, number too small.\n");
        }
        else if(guess > secret_number){
            printf("Sorry, number too big.\n");
        }
        if(i==10){
            printf("Out of Attempts");
        }
        if(guess>99 || guess<0){
            printf("Out of Range.\n");
           }
        }
        printf("\nPlay again? (y/n): ");
        scanf(" %c", &play);
    } while (play == 'y');

    return 0;
}

As a side note - giving the user 10 chances to guess a number in the range 1-100 is too generous if you're providing "higher/lower" feedback. If my calculations are correct, a binary search would find the answer in maximally log2(100)=6.64... attempts. In other words, you should be able to find the answer in no more than 7 attempts if you know what you're doing. A binary search works of course by guessing the number in between the bounds and then adjusting the bounds according to your feedback.

Comments

1

Rename your current main() function to something like play_game(). Then add a new main() function with a loop. Here's one way:

int main() {
    char again[10];
    do {
        play_game();
        printf("Do you want to play again? ");
        scanf("%10s", again);  // TODO: ensure return value is 1
    } while (again[0] == 'y' || again[0] == 'Y');
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.