dimanche 14 mars 2021

why is my switch statement passing over fgets input straight to my "please enter something" error message? [duplicate]

the code written in a different program without the switch statement doesnt skip right to print something am i doing something wrong?


int c1 = 0;
            
            scanf("%i", &c1);
            
            char name[20];
            
            switch(c1)
            {
                case 1:
                
                printf("Please enter a username: ");
                fgets(name, 20, stdin);
                
                printf("Hello: %s", name);
                
                if (name[0] == '\n')
                {
                    printf("Please enter something\n");
                }
                if (name[strlen(name) - 1] != '\n')
                {
                    printf("Please enter less than 20 characters\n");
                }
                
                break;

if more code is needed ill post it hope this is enough to see the problem

Aucun commentaire:

Enregistrer un commentaire