Home > Software design >  C Visual Studio goto skipping scanf_s
C Visual Studio goto skipping scanf_s

Time:10-31

I'm coding in C on visual studio and can't seem to figure out a fix for scanf_s scanning only once.

#include <stdio.h>
#include <string.h>
int main() {
    int duration;
    int cost;
    printf("1 day = 50$\nFor students:\n2 days = 90$\n3 days = 120$\n");
    question_1:
    printf("What's your stay duration in days?\n");
    scanf_s("%d", &duration);
    if (duration == 1)
    {
        printf("That will be 50$.");
    }
    else
    {
        if (duration == 2 || duration == 3)
        {
        question_2:
            printf("Are you a student?\n");
            char answer[20];
            scanf_s("%s", &answer, sizeof(answer));
            if (strcmp(answer, "yes") == 0)
            {
                    if (duration == 2)
                    {
                        printf("That will be 90$.");
                    }
                    else
                    {
                        printf("That will be 120$.");
                    }
            }
            else
            {
                if (strcmp(answer, "no") == 0)
                {
                    cost = duration * 50;
                    printf("That will be %d$.", cost);
                }
                else
                {
                    goto question_2;
                }
            }
        }
        else
        {
            goto question_1;
        }
    }
}

When inputting a word instead of a number for "duration" the program repeatedly prints "What's your stay duration in days?" instead of scanning for another input, what should I change?

CodePudding user response:

You need to clear the input buffer after an invalid input. For example

printf("1 day = 50$\nFor students:\n2 days = 90$\n3 days = 120$\n");

question_1:;
int result;
do
{
    printf("What's your stay duration in days?\n");
    result = scanf_s("%d", &duration);

    if ( result != 1 )
    {
        while ( getchar() != '\n' );
    }
} while ( result != 1 );

Pay attention to that instead of goto statements and labels it is much better to use loops. Try to rewrite your program using loops.

Also pay attention to the semicolon after the label

question_1:;

The problem is that in C you may not place labels before declarations. So the null statement helps to resolve the problem.

CodePudding user response:

This version of the code works as intended by me, the poster and a C novice.

#include <stdio.h>
#include <string.h>
int main() {
    int duration;
    int cost;
    int check;
    printf("1 day = 50$\nFor students:\n2 days = 90$\n3 days = 120$\n");
    question_1:
    do
    {
        printf("What's your stay duration in days?\n");
        check = scanf_s("%d", &duration);

        if (check != 1)
        {
            while (getchar() != '\n');
        }
    } while (check != 1);
    if (duration == 1)
    {
        printf("That will be 50$.");
    }
    else
    {
        if (duration == 2 || duration == 3)
        {
        question_2:
            printf("Are you a student?\n");
            char answer[20];
            scanf_s("%s", &answer, sizeof(answer));
            if (strcmp(answer, "yes") == 0)
            {
                    if (duration == 2)
                    {
                        printf("That will be 90$.");
                    }
                    else
                    {
                        printf("That will be 120$.");
                    }
            }
            else
            {
                if (strcmp(answer, "no") == 0)
                {
                    cost = duration * 50;
                    printf("That will be %d$.", cost);
                }
                else
                {
                    goto question_2;
                }
            }
        }
        else
        {
            goto question_1;
        }
    }
}
  • Related