0

I have this code for a school project. Our teacher just want a simple function which puts stars instead of text for a login menu.

So I wrote that:

void login_menu(){

    char nickname[15], password[15] = { '/0' };
    int check=0;

    while (check != 1){
        printf(" your nickname : ");
        int checknickname=0;
        while(checknickname!=1){
            scanf("%s",&nickname);
            if(strlen(nickname)>1){
                checknickname=1;
            }
            else{
                printf(" your nickname : ");
            }
        }
        int checkpw=0;
        printf(" your password : ");
        while(checkpw!=1){
            int i;
            for (i = 0; i < 10;i++) {
                password[i] = _getch();
                _putch('*');
                if (password[i] == 13){
                    password[i] = '\0';
                    break;
                }
            }
            if(strlen(password)>1){
                checkpw=1;
            }
            else{
                printf(" your password : ");
            }
        }
        printf("\n");
        if((strcmp(mdp,"admin")==0) ){
            printf("mdp");
        }
        if( (strcmp(pseudo, "admin")==0 ) && (strcmp(mdp, "admin")==0) ){
            system("cls");
            check = 1;
            user_menu();
        }
        else{
            printf("your logs are incorrect");
        }
    }
}

Its working fine but I have some warnings :

C:\Users\superneiluj\Desktop\Poly Music 1.0\main.c|434|warning: multi-character character constant [-Wmultichar]|
C:\Users\superneiluj\Desktop\Poly Music 1.0\main.c|434|warning: overflow in implicit constant conversion [-Woverflow]|

for the line

char nickname[15], password[15] = { '/0' };

and

C:\Users\superneiluj\Desktop\Poly Music 1.0\main.c|441|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[15]' [-Wformat=]|

for the line

scanf("%s",&nickname);
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
julien1h
  • 85
  • 7
  • 1
    `'/0'` is a multi byte character literal. You probably wanted `'\0'` instead which is just a single character with value 0 – Gerhardh Jun 13 '22 at 09:02
  • 1
    The identifier of an array already decays to a pointer in many cases. No need to add `&`. Either use `scanf("%s", nickname);` or `scanf("%s", &nickname[0]);` And to be on safe side you should always add a length limit: `"%14s"` to avoid buffer overflow – Gerhardh Jun 13 '22 at 09:05
  • 1
    I consider your first warning a typo, so I closed it with a question that deals with the second in pretty good detail – klutt Jun 13 '22 at 09:19
  • thank you @Gerhardh didnt see that ! – julien1h Jun 13 '22 at 12:10

0 Answers0