0

So recently I tried to make a game called "guessing game" (based on some YouTube videos). After studying it, I added some flavors to it, but then I encountered a problem where my "scanf" below my "game()" or "game function" is getting skipped. After asking "would you like to play the game again" It should supposedly get input from the user, but it's getting skipped instead.

Thank you in advance!!

Here's my code:

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

void game()
{
    int guessnum, conum, guesscounter, guesslimit = 3;
    
    srand(time(NULL));
    conum = rand() %10;
    
            while(guesscounter != guesslimit){
                printf("Type the number here: ");
                scanf("%d", &guessnum);
    
                if( guessnum == conum ){
                    printf("\nYou guess it right! Congratulations!.\n");
                    break;
                }
                else if( guessnum > conum ){
                    printf("\nYour guess is greater than the correct number.\n");
                }
                else if( guessnum < conum ){
                    printf("\nYour guess is less than the correct number.\n");
                }
                else {
                    printf("\nInvalid input.\n");
                }
                guesscounter++;
            }
        if( guesscounter == 3 ){
            printf("\nYou lose.\n");
        }
}

int main()
{
    char answer[20], yon;
    char* ans[] = {"yes", "Yes", "YES", "no", "No", "NO"};

    printf("Do you want to play this game?: ");
    scanf("%s", answer);

    if(strcmp( answer, ans[0] ) == 0 || strcmp( answer, ans[1] ) == 0 || strcmp( answer, ans[2] ) == 0){
        printf("\nOk! Let's play then!\n");
        printf("-----------------------------------------\n");
        printf("Press any key to continue.\n");
        printf("-----------------------------------------\n");
        getch();
        printf("So this game is called \"Guessing game\", and as for the mechanics, all you need to do is to keep guessing until you get the right answer.\n");
        printf("-----------------------------------------\n");
        printf("Press any key to continue.\n");
        printf("-----------------------------------------\n");
        getch();
        printf("Remember that you only have a maximum of 3 guesses to correctly guess the number.\n");
        printf("-----------------------------------------\n");
        printf("Press any key to continue.\n");
        printf("-----------------------------------------\n");
        getch();
        
        game();
        
        printf("\nWould you like to play the game again? \"Y\" or \"N\"?\n");
        scanf("%c", &yon);
        if( yon == 'Y' ){
                while( yon == 'Y' ){
                game();
                printf("\nWould you like to play the game again? \"Y\" or \"N\"?\n");
                scanf("%c", &yon);
            }
        }
        printf("\nThank you for playing!\n");
    }
    else if(strcmp( answer, ans[3] ) == 0 || strcmp( answer, ans[4] ) == 0 || strcmp( answer, ans[5] ) == 0){
        printf("\nGoodbye then.\n");
    }
    else{
        printf("\nInvalid input");
    }
    return 0;
}

enter image description here

0 Answers0