undeclared identifier in C

前端 未结 5 1501
忘掉有多难
忘掉有多难 2021-01-26 04:29

I am trying to compile a small bank program in C in visual studio 2012 express. It shows me this error \"undeclared identifier\" for almost all variables and this one too \"synt

5条回答
  •  既然无缘
    2021-01-26 04:59

    The Microsoft C compiler only supports a 25 year old version of the language. And one of the limitations is that all variables must be declared before any other statements. So move all your variable declarations to the top of the function.

    The next error I can see is the use of scanf_s with the %c format string. You must pass a pointer to the variable, and pass the number of characters to read.

    scanf_s("%c", &option, 1);
    

    And likewise you need to pass an address for the read of balance.

    You also need to change the switch statement so that it just contains cases. Move the bare instructions outside.

    Your reading of option won't work. Because when you check for 1 you are checking for the character with ASCII code 1. Change option to be an int and read using %d.

    Perhaps you are looking for something like this:

    #include
    #include
    
    int main(void)
    {
        int deposit,withdraw,kbalance;
        int option;
        int decash,wicash;
        int balance;
        int wibal;
    
        printf("Welcome to skybank\n");
        printf("Press 1 to deposit cash\n");
        printf("Press 2 to Withdraw Cash\n");
        printf("Press 3 to Know Your Balance\n");
        scanf_s("%d", &option);
        printf("Enter your current Balance\n");
        scanf_s("%d", &balance);
        switch(option)
        {
            case 1:
                printf("Enter the amount you want to deposit\n");
                scanf_s("%d", &decash);
                printf("Thank You\n");
                printf("%d have been deposited in your account\n", decash);
                break;
            case 2:
                printf("Enter the amount you want to withdraw\n");
                scanf_s("%d", &wicash);
                wibal=balance-wicash;
                printf("Thank You\n");
                printf("%d have been withdrawed from your account\n", wicash);
                printf("Your balance is %d\n", wibal);
                break;
            case 3:
                printf("Your balance is Rs.%d\n", balance);
                break;
            default:
                printf("Invalid Input\n");
                break;
        }
        getchar();
    }
    

提交回复
热议问题