int login_user()
{
wrong:
printf("-------------Login-------------\n");
int loop,dev;
char usernmcmpr[30],pswdcmpr[4],loginchoice,test[30];
printf("Username:");
scanf(" %s",&usernmcmpr);
printf("Password:");
scanf(" %s", &pswdcmpr);
printf("1. %s\n",username);
printf("2. %s",usernmcmpr);
if(strcmp(password,pswdcmpr) == 0 && strcmp(username,usernmcmpr) == 0)
{
system("cls");
printf("-------------Main Menu-------------\n");
printf("a.Calculate Cost\nb.Payment\nc.Recepit\nd.Exit\n");
scanf(" %c", &loginchoice);
if (loginchoice == 'a')
{
system("cls");
calculate_cost();
}
else if (loginchoice == 'b')
{
system("cls");
payment();
}
else if (loginchoice == 'c')
{
system("cls");
receipt();
}
else if (loginchoice == 'd')
{
keep_Looping = 1;
system("cls");
}
else
{
system("cls");
printf("-------------Error-------------\n");
printf("Invalid input please try again\n");
goto wrong;
clear();
}
}
else
{
goto wrong;
}
return 0;
}
It used to work and then all of a sudden it stopped, don't know why i didn't even change the code in this function nor did i change anything related to it. Really can't tell what is wrong.
CodePudding user response:
usernmcmpr is a char array which is basically a char* under the hood. Since usernmcmpr is already a pointer you want scanf(" %s", usernmcmpr); instead of scanf(" %s",&usernmcmpr);. Same for your other char arrays.
Furthermore, you should be more cautious about buffer overflow problems as currently the user can enter more than your arrays can hold. See How to prevent scanf causing a buffer overflow in C?
