0
votes

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;
}
2
You want to learn what "loops" are. They are how you repeat things in programs: learn-c.org/en/For_loops - Benjamin Maurer
They already have a for loop - so presumably they understand how those work. I would suggest a while loop. - Johnny Mopp
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. - Weather Vane
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. - h0r53

2 Answers

1
votes

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.

1
votes

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.