Loop back to question instead of printing out "Try again"? (C)

Viewed 70

this is my first day coding and I'm trying to see how the code work, how can I change Y/N to Yes/No and loop back questions if the answer is not Yes instead of printing out "Try again?" or make it better?

int main(void)
{

    char answer;
    int name;
    printf("Enter Username: \n");
    scanf("%s",&name);
    printf("Is your username \"%s\"? Enter Y or N\n", &name);

    while (scanf(" %c", &answer) == 1 && answer == 'Y')
    {
        char answer2;
        Sleep(1000);
        printf("Username confirmed\n");
        Sleep(2000);
        int pass;
        printf("Enter your password: \n");
        scanf("%s",&pass);
        printf("Is your password \"%s\"? Enter Y or N\n", &pass);
        while (scanf(" %c", &answer2) == 1 && answer2 == 'Y')
        {
            printf("Success!");
            Sleep(1000);
            exit;
        }
        printf("Try again..\n");
        Sleep(1000);
        exit;
    }
    Sleep(1000);
    printf("Try again..\n");
    Sleep(1000);
    exit;
    return 0;
}
1 Answers

My best advice, as Weather Vane told you in the comment section, is to try to compile your code, before making changes: you'll find out that it won't even compile, since there are some errors (https://godbolt.org/z/MdahM4sT8).
You should debug and try to figure out why it doesn't work, fix it, then move to the new "features".


You probably want to change the while() condition and read a sequence of characters (array of char) instead of a single char.

char answer[4];

while (scanf("%3s", answer) > 0 && strcmp(answer, "Yes") == 0)

char answer[4]; creates an array of characters (which can be called a string, even though C doesn't have that abstraction) of size 4.
You might want to know why we need a size of 4, since the longer answer we can get is "Yes". Well, a string in C is a sequence of characters (char), ending with a special character, called terminating character '\0'.

Therefore, when you read a string with scanf(), you need to reserve some space for the terminating character aswell, and scanf() will add it when reading a space or carriage return after your input string. So if you enter "Yes" and then press the Return key, your answer[4] buffer will look like: {'Y', 'e', 's', '\0'}.
%3s tells scanf() to read only the first 3 characters of the string (prevents security issues like buffer overflow).

int scanf(const char *format, ...)

Description
Reads formatted input from stdin.

Return Value
On success, the function returns the number of items of the argument list successfully read. If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror) and, if either happens before any data could be successfully read, EOF is returned.

Once you've read that string, you can use strcmp(), a function defined in `<string.h> header, that takes in 2 string parameters, and returns:

  • a negative value if the first precedes alphabetically the second;
  • 0 if they're equal;
  • a positive integer if the second precedes alphabetically the second;

Therefore you can check if strcmp(answer, "Yes") == 0 to know if you've read Yes, and

Related