why don't read after the name when the option is 'No"? only readed "name" and finish the program

+1 vote
asked Jun 25 by Bruno Rodrigues (190 points)
#include<stdio.h>

#include<stdlib.h>

int main ()

{

int name,email,password,account,lastname;

printf("\n_____________________________");

printf(" |   Welcome to AlphaComics  |");

printf(" _______________________________");

printf("\nDo you have a account?");

printf("\nType it 1 for Yes\nType it 2 for No");

scanf("%d",&account);

if (account == 1)

{

printf("\nNext");

}

else

    if(account == 2)

    {

    printf("\nEnter with your name:");

    scanf("%d",&name);

    printf("\nEntre with your lastname:");

    scanf("%d",&lastname);

    printf("\nEnter with your email:");

    scanf("%d",&email);

    printf("\nEntre with your password:");

    scanf("%d",&password);

}

}

2 Answers

0 votes
answered Jun 25 by Peter Minarik (12,220 points)

Should we use scanf?

Scanf can be clumsy in many cases. Others have faced similar difficulties as you: https://stackoverflow.com/questions/26478931/why-cant-i-use-scanf-on-the-same-variable-again-when-the-first-trail-fails

The main problem is that if scanf fails, it will try to read the same input (console) again. Since all of them fail, it just skips everything.

Your code fails as you try to read a string (e.g. a name) into an integral number (int). That's not gonna work. scanf fails.

What you need is to use the proper types for variables. In C strings are stored in char * (char pointer) or char[] (char array/buffer).

Now you can call code like

char name[20];
scanf("%s", name);

The problem with this is that name is limited to be 20 characters long maximum (including the terminating '\0').

You can look at this topic and see how others have solved the problem of getting an input:https://stackoverflow.com/questions/4023895/how-do-i-read-a-string-entered-by-the-user-in-c/4023921#4023921

So to give an answer: scanf can be used, but one should be aware of its limitations. It's always good idea to check the documentation too: http://www.cplusplus.com/reference/cstdio/scanf/

A Possible Fix

Now, with this knowledge I would fix your code like below:

#include <stdio.h>

int main ()
{
    // You may need to change the buffer sizez to match your needs.
    const size_t bufferSize = 100;
    const size_t firstNameSize = 30;
    const size_t lastNameSize = 30;
    const size_t emailSize = 100;
    const size_t passwordSize = 20;

    char buffer[bufferSize];
    char firstName[firstNameSize];
    char lastName[lastNameSize];
    char email[emailSize];
    char password[passwordSize];

    int choice = -1; // Set choice to be invalid

    printf("_____________________________ |   Welcome to AlphaComics  | _____________________________\n");
    printf("Do you have a account? (0 for No, 1 for Yes)\n");
    
    do
    {
        printf("? ");
        fgets(buffer, bufferSize, stdin);
        sscanf(buffer, "%d", &choice);
    } while (choice != 0 && choice != 1);
    

    if (choice == 0)
    {
        printf("Next\n");
    }
    else if (choice == 1)
    {
        printf("Enter your first name: ");
        scanf("%s", firstName);
        printf("Entre your lastname: ");
        scanf("%s", lastName);
        printf("Enter your email: ");
        scanf("%s", email);
        printf("Entre your password: ");
        scanf("%s", password);
        
        printf("Hi %s %s.\n", firstName, lastName);
        printf("Your email is: %s\n", email);
        printf("Your password is: %s\n", password);
    }
    
    return 0;
}
0 votes
answered Jun 26 by LiOS (5,280 points)
Further to Peter's answer, the issue that causes all the problems is that you are attempting to write a string e.g. name to a integer variables. When you attempt to write characters to integer variables, it will often simply fail and skip the rest of the program, especially in this case.

The use of scanf is temperamental and often should use other similar functions such as fgets or sscanf but not gets since it's depracated and dangerous (just like how strncpy is safe while strcpy isn't since no bounds check).

Just to note, in Peter's solution, if the user types '1' to represent they have an account, it will ask for details while it should be 0, they don't.
commented Jun 26 by Peter Minarik (12,220 points)
Yeah, you're right. Probably details should be asked when there is no account, not when there is one. :)

I just copied the original behaviour without too much attention for the logic (except that in logic expressions zero means NO and non-zero means YES, opposed to 1 and 2).
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.
...