0
    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.

ks1322
  • 33,961
  • 14
  • 109
  • 164
Foros
  • 15
  • 4
  • 1
    Unless you password is three characters or less, this has little hope of "working" no matter what. `pswdcmpr[4]` only leaves room for a 3-char entry (or shorter) accounting or the terminating nullchar. Anything longer and you breach your array and invoke undefined behavior, since your `%s` has no length limit. And fyi, *all* of those string `scanf` arguments are wrong. The leading `&` on each of them is erroneous. – WhozCraig Jan 15 '22 at 12:48
  • 1
    Not your problem, but someone appears to have given you an overzealous rule in an attempt to work around `scanf`'s idiosyncrasies. Yes, you do want an extra space in `" %c"`. But you don't need it in `"%s"` or most of the other format specifiers. – Steve Summit Jan 15 '22 at 13:11
  • Only for `%c` or `%[]` or `%n` unless you intend to read the whitespace character (which is why they are an exception to the auto-filtering of other format specifiers). – Weather Vane Jan 15 '22 at 13:57
  • Always a bug: not checking the return value from scanf. – Jens Jan 15 '22 at 20:00

1 Answers1

0

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?

Eitan
  • 101
  • 3