0

I have written following code where 2 numbers are generated randomly and added into third variable. It then asks the user for accurate answer. Currently right answer is always 'A'. a1 is the actual answer whereas a2,a3,a4 are the randomly generated answers.

I have put condition to see if the answer given is in the range of a,b,c,d or A,B,C,D using ASCII values. If it does not fit then message given is invalid answer.

If the answer is 'A' or 'a' it gives message "Answer is correct!\nDo you want to play again? (y/n)" else it shows "Answer is incorrect!\nDo you want to play again? (y/n)".

After any answer it asks whether the user wants to continue. However after "Do you want to play again?" it exits the compiler even the scanf function is there.

I want to keep it repeating till user answers anything else than 'y' or 'Y'. As the char data type was not working I have tried to uses integer variable 'condition' but still not getting desired answer.

#include <stdio.h>
#include <conio.h>

void main() {
    int n1, n2, a1, a2, a3, a4, condition = 1;
    char again = 0, answer;
    srand(time(0));

    while (condition == 1) {
        n1 = rand() % 10;
        n2 = rand() % 10;

        printf("\n\n%d + %d=\n\n", n1, n2);

        a1 = n1 + n2;
        a2 = rand() % a1;
        a3 = rand() % a1 + 10;
        a4 = rand() % a1 + 2;

        printf("Your options are:\n\nA) %d\nB) %d\nC) %d\nD) %d\n\n "
               "What is your answer:\n", a1, a2, a3, a4);

        scanf("%c", &answer);

        if (answer > 64 && answer < 69) {
            if (answer == 'a' || answer == 'A') {
                printf("Answer is correct!\nDo you want to play again? (y/n)");
            } else {
                printf("Answer is incorrect!\nDo you want to play again? (y/n)");
            }
        } else if (answer > 96 && answer < 101) {
            if (answer == 'a' || answer == 'A') {
                printf("Answer is correct!\nDo you want to play again? (y/n)");
            } else {
                printf("Answer is incorrect!\nDo you want to play again? (y/n)");
            }
        } else {
            printf("Invalid answer!\nDo you want to play again? (y/n)");
        }

        scanf("%c", &again);

        if (again == 'y' || again == 'Y') {
            condition = 1;
        } else {
            condition = 0;
        }
    }

    getch();
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Linus
  • 39
  • 4
  • 2
    `scanf("%c",&again);`--> `scanf(" %c",&again);` and `scanf("%c",&answer);` --> `scanf(" %c",&answer);` – kiran Biradar Aug 09 '19 at 06:38
  • 1
    See this as the perfect time to [learn how to debug your programs](https://blog.hartleybrody.com/debugging-code-beginner/). Use a debugger to step through your code, statement by statement, while monitoring variables and their values. – Some programmer dude Aug 09 '19 at 06:39
  • 1
    And some critique of your code: First of all don't use [*magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)) like `64` or `101`. Then if `answer>64 && answer<69` is true you know that `answer == 'a'` *will* be false so you don't need to check for it. And all of that code checking letters could be changed to a single `switch` (and with simpler cases if you use e.g. [`tolower`](https://en.cppreference.com/w/c/string/byte/tolower)). – Some programmer dude Aug 09 '19 at 06:43
  • @Someprogrammerdude: I agree with your comment, the OP should use `if (isalpha((unsigned char)answer))` and `if (tolower((unsigned char)answer) == 'a')` – chqrlie Aug 09 '19 at 07:08

2 Answers2

2

Change

scanf("%c",&again);

to

scanf(" %c",&again);

see How to do scanf for single char in C

or read David's comment below.

Jon Koelzer
  • 350
  • 2
  • 9
  • 4
    You should explain how the `"%c"` and `"%[...]"` conversion specifiers do not consume leading whitespace. (`'\n'` being whitespace). So every time the user presses [Enter] and generates a `'\n'` character in `stdin`, the next call to `scanf` using the `"%c"` conversion specifier happily accepts the `'\n'` as the users input to the next question. You should explain how by placing a literal `space` in front of the conversion specifier, e.g. `" %c"` you force the removal of all whitespace characters leaving the wanted character to be read on the next call to `scanf`. – David C. Rankin Aug 09 '19 at 07:00
  • Yeah, you're right. While the link explains exactly that, I could probably have elaborated... Your explanation is lovely as well though so I'll just point to that. Thank you @DavidC.Rankin :) – Jon Koelzer Aug 09 '19 at 07:04
-2

this is scanf's bug. you need use getchar() after use scanf to avoid your 'Enter' input to this function.

scanf("%c",&again); ->scanf("%c",&again); getchar()

蓝莲花
  • 1
  • 1