Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I would like to ask how to fix this if else statement. (Specified as the Bold one).

+1 vote
asked Sep 19, 2021 by Lexus Guevara (1,010 points)

because it ends my program as soon as i clicked "N". only the else statement has the problem, the if is running good. thank you for your answers, it really helps me alot.

#include <stdio.h>

int main()
{
    char name[20];
    char address[50];
    char items[60];
    char answer;
    char pay[50];

    printf("Enter your name: ");
    fgets(name, 20, stdin);
    printf("Your name is %s", name);
    printf("Enter your specific address: ");
    fgets(address, 50, stdin);
    printf("Your address is %s", address);
    printf("Enter your items that you want to buy: ");
    fgets(items, 60, stdin);
    printf("Your item/s is/are %s", items);
    printf("Would you like to pay it after delivery? Enter Y or N: ");
    scanf (" %c", &answer);
    printf("Your answer is %c\n", answer);
    if (answer == 'Y') 
    {
        printf("Success!");
    } else
    {
        printf("How would you like to be paid?");
        printf("\nPlease specify how would you like to be paid: ");
        fgets(pay, 50, stdin);
        printf("%s", pay);
    }
    printf("Thank you!");

    return 0;
}

3 Answers

0 votes
answered Sep 19, 2021 by Ankan Roy (140 points)
Use Switch Case is lot easy to do
commented Sep 20, 2021 by Lexus Guevara (1,010 points)
Thank you very much, I hope I can learn how to use the switch case because I am a beginner and I don't really know much about programming.
0 votes
answered Sep 19, 2021 by Sumit (140 points)
Just remove the space before"%c" in : "scanf (" %c", &answer);".
commented Sep 20, 2021 by Lexus Guevara (1,010 points)
Thank you very much!
commented Sep 20, 2021 by Peter Minarik (84,720 points)
It doesn't work though.
0 votes
answered Sep 20, 2021 by Peter Minarik (84,720 points)

After reading a single character with scanf(), the input buffer still contains the newline character the user typed.

So the next time you use fgets(), it will pick up the newline character still on the standard input, hence your program seems to skip that line (, which did not, just read empty).

If you use scanf() again to read the pay string, then it will be smart enough to know to skip the newline character left on the standard input.

scanf("%s", pay);
commented Sep 20, 2021 by Lexus Guevara (1,010 points)
Thank you very much, but what if I type multiple words like when using scanf, it can't read the next word that I needed after the space.

else
    {
        printf("How would you like to be paid?");
        printf("\nPlease specify how would you like to be paid: ");
        scanf("%s", pay);
        printf("%s", pay);
    }
commented Sep 20, 2021 by Peter Minarik (84,720 points)
Mixing fgets() and and scanf() can lead to strange behaviours, as you can see. You can get rid of this unwanted behaviour by only using just one of them to read from the standard input (stdin).

E.g. you read a whole line for they "Y/N" selection into a buffer, and then check what's in the buffer later:

char buffer[255];
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, " %c", &answer);

This way, stdin is always handled by fgets().
commented Sep 21, 2021 by Lexus Guevara (1,010 points)
thank you very much, it helped me a lot.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...