Yedige Ashmet
Yedige Ashmet

Reputation: 13

Number guessing game in C

I had a problem at doing my number guessing game in C. While running the program it shows up return function when I put the char element in there. Can someone help me with this? Also how can I improve my code in order to make it workable? I'M really stuck with this issue.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int main()
{
    char s, n, q;
    int b;
    bool (1=true), (0=false);
    int secret;
    secret = rand();
    int guess;
    int seed;
    srand(seed);
    printf("Welcome to the guessing game!\n");
    do{
        printf("Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:\n");
        scanf("%s, %s, %s", s, n, q);
        if((s==1))
        {
            printf("The secret number is Between 0 AND rand(). Guess\n");
            scanf("%s", b);
        }
        else if((n ==1))
        {
            printf("Enter a new MAXIMUM\n");
            scanf("%s", rand());
            if(( s ==1))
            {
                printf("The secret number is Between 0 AND rand(). Guess\n");
                scanf("%s", b);

                printf("The secret number is between 0 and rand() Guess:");
                scanf("%s", b);
                if(guess = rand()){
                    printf("Congratulations you won, You took %d guesses!", b);
                    break;
                }
                else if(guess > rand())
                {
                    printf("Too High, Guess again:");
                }
                else if(guess < rand()){
                    printf("Too Low, Guess Again:");
                }
                else{
                    printf("This number out of the number set!");
                }
            }
        }
        else{
            printf("Unrecognized command");
        }
    }while(q == 1);
    printf("You quited the game");

    return 0;
}

Upvotes: 1

Views: 2163

Answers (2)

ggorlen
ggorlen

Reputation: 56965

This code has myriad issues to resolve. I'd suggest approaching your code writing process in small steps. It appears as though you wrote the entire program in one burst, ran it, and found it didn't work instead of incrementally adding small features and running each one to verify it works before moving on to the next step. Not doing this results in a difficult to debug program and a lack of understanding about how the program operates.

To be specific, try writing a three or four line program that collects and prints user input. Is the output working as you expect? Did you test its robustness on a variety of input? Can you write it to use a variety of data types? If something isn't working, did you research the problem and resolve it before steaming ahead?

Some areas of your program to investigate:

  • bool (1=true), (0=false); doesn't compile and isn't necessary for the program. If you #include <stdbool.h> you don't need to do this (you can simply write if (something == true)).
  • srand() is not properly called or seeded. Seed it once per program using the time() call from the header you included and call rand() once per game. Use the % operator to set the function output between 0 and max.
  • On each turn, compare guess against the value you previously stored rand() in rather than calling rand() during each comparison, which makes the game logic arbitrary.
  • %s input isn't appropriate; read chars %c and ints %d, passing appropriate variable references to scanf. scanf("%s, %s, %s", s, n, q); collects 3 whitespace separated strings for input instead of 1 char as the prompt suggests.
  • There is no game loop. Add an inner loop to run a game when the user chooses s and move your guess/response logic there.
  • Use more verbose variable names and correct brackets and indentation to improve readability.

Putting it all together, here's one possible working version. It could make better use of functions and implement secure user input (exercises for the reader):

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

int main() {
    char menu_choice;
    int guess;
    int guesses;
    int secret;
    int max = 100;
    srand(time(NULL));
    printf("Welcome to the guessing game!\n");
    
    for (;;) {
        printf("\nMenu: (s) to start a new game, (n) to set a new range, or (q) to quit: ");
        scanf(" %c", &menu_choice);

        if (menu_choice == 's') {
            guesses = 0;
            secret = rand() % max;
            
            for (;;) {
                printf("\nThe secret number is between 0 and %d. Enter a guess: ", max);
                scanf("%d", &guess);
                guesses++;

                if (guess == secret) {
                    printf("\nCongratulations, you won! You guessed %d in %d guesses!\n", secret, guesses);
                    break;
                }
                else if (guess > secret) {
                    printf("Too high! Guess again.");
                }
                else if (guess < secret) {
                    printf("Too low! Guess again.");
                }
                else if (guess >= max) {
                    puts("Out of range");
                }
            }
        }
        else if (menu_choice == 'n') {
            printf("\nEnter a new maximum: ");
            scanf("%d", &max);
        }
        else if (menu_choice == 'q') {
            puts("\nGoodbye!");
            break;
        }
        else {
            puts("\nUnrecognized command.");
        }
    }

    return 0;
}

Sample run:

Welcome to the guessing game!

Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:  n

Enter a new maximum:  50

Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:  s

The secret number is between 0 and 50. Enter a guess:  25
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess:  37
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess:  43
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess:  47

Congratulations, you won! You guessed 47 in 4 guesses!

Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:  q

Goodbye!

Upvotes: 2

Hemanth
Hemanth

Reputation: 3

  printf("Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:\n");
  scanf("%s, %s, %s", s, n, q);

you dont need to use three variables s,n,q. you should ask the user to enter a single choice. either to start a new game or to quit or aything else.

secondly, rand() returns a random number every time. you are supposed to store random number somewhere. like this

rand_num=rand()

also

if(guess = rand())

is a wrong way of comparison. this should be

if(guess==rand_num)

finally binary search is the solution for your problem. please refer it on internet

Upvotes: 0

Related Questions