Search code examples
cstringinputscanffgets

getting string with spaces from input in a switch case or loop


I try this code which I found on this website (how-do-you-allow-spaces-to-be-entered-using-scanf)

           char name[15];         //or char*name=malloc(15);
           /* Ask user for name. */

            printf("What is your name? ");

            /* Get the name, with size limit. */

            fgets(name, MAX, stdin);

            /* Remove trailing newline, if there. */

            if ((strlen(name) > 0) && (name[strlen(name) - 1] == '\n'))
                name[strlen(name) - 1] = '\0';

            /* Say hello. */

            printf("Hello %s. Nice to meet you.\n", name);

When I run this code in main it works pretty well. Output is:

 What is your name? j k rowling
Hello j k rowling. Nice to meet you.

But when I put this code in a while loop or in a switch case:

        char name[15];
        switch (choice) {
            case 1:
            printf("What is your name? ");

            /* Get the name, with size limit. */

            fgets(name, MAX, stdin);

            /* Remove trailing newline, if there. */

            if ((strlen(name) > 0) && (name[strlen(name) - 1] == '\n'))
                name[strlen(name) - 1] = '\0';

            /* Say hello. */

            printf("Hello %s. Nice to meet you.\n", name);


            break;  }

The output is:

What is your name? Hello . Nice to meet you.

So, it doesn't wait to entering a string. maybe fgets doesn't work I don't know.

How can I make this code work? Or any alternative to get string from input with all spaces. I tried this either:

switch (choice) {
        case 1:
            printf("What is your name? ");
            scanf("%[^\n]s", name);
            printf("%s\n", name);  }

output is:

What is your name? ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠

What is wrong with these? I use visiual studio. And I always get a trouble. Is this about it?


Solution

  • The problem is that stdin is not getting flushed properly.

    You need to manually flush it. (fflush() function is available. But it is problematic.)

    Following is an example code to solve your issue. See it working here:

    #include <stdio.h>
    #define MAX 15
    
    int main(void) {
        // your code goes here
        char name[MAX];
        char c;
        int choice=1;
        while(choice>0 && choice<3)
        {
            printf("Enter your choice: ");
            scanf("%d", &choice);
    
            switch(choice)
            {
                case 1:
    
                    //Flush stdin
                    while ((c = getchar()) == '\n');
                    ungetc(c, stdin);
    
                    printf("What is your name? ");
                    fgets(name, MAX, stdin);
                    if ((strlen(name) > 0) && (name[strlen(name) - 1] == '\n'))
                    name[strlen(name) - 1] = '\0';
                    printf("Hello %s. Nice to meet you.\n", name);
                break;
    
                case 2:
                    printf("Your choice is case 2\n");
                break;
            }
        }
        return 0;
    }