0

我正在尝试在 Visual Studio 2012 Express 中用 C 语言编译一个小型银行程序。它向我显示了几乎所有变量的这个错误“未声明的标识符”,而这个也显示了“语法错误:缺少';' 在'类型'之前“。请告诉我正确的语法。谢谢。

#include<stdio.h>
#include<conio.h>
int main()
{
printf("Welcome to skybank\n");
int deposit,withdraw,kbalance;
char option;
printf("Press 1 to deposit cash\n");
printf("Press 2 to Withdraw Cash\n");
printf("Press 3 to Know Your Balance\n");
scanf_s("%c",option);
int decash,wicash;
switch(option)
{
int balance;
printf("Enter your current Balance\n");
scanf_s("%d",&balance);
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);
    int wibal;
    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();
}
4

5 回答 5

5

Microsoft C 编译器仅支持该语言的 25 年旧版本。限制之一是所有变量必须在任何其他语句之前声明。因此,将所有变量声明移至函数顶部。

我可以看到的下一个错误是使用scanf_s格式%c字符串。您必须传递一个指向变量的指针,并传递要读取的字符数。

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

同样,您需要传递一个地址以读取balance.

您还需要更改 switch 语句,使其只包含案例。将裸指令移到外面。

你的阅读是option行不通的。因为当您检查时,1您正在检查带有 ASCII 代码 1 的字符。更改option为 anint并使用%d.

也许您正在寻找这样的东西:

#include<stdio.h>
#include<conio.h>

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();
}
于 2013-06-16T15:11:00.363 回答
1

do 在 Visual c 的变量声明中块的开头。

例如

int main()
{
    int deposit,withdraw,kbalance;
    char option;
    int decash,wicash
    int balance;
    int wibal;
...
于 2013-06-16T15:06:03.567 回答
1

关于未识别的变量,请尝试将所有变量声明放在主块的顶部,例如:

int main()
{
    int deposit, withdraw, kbalance, decash, wicash, wibal;
    char option;
    printf("Welcome to skybank\n");

将变量声明与代码混合使用的 C 的旧变体皱眉。据我所知,微软 C 实现的 C 标准是 C99 之前的,所以这可能是问题所在。

您应该研究的其他一些问题:

scanf_s("%c",option);-option应该是&option因为您正在使用指向该变量的指针。

也在这里:case 1:

您想要'1'(如case '1')而不是普通1的,因为它是 a char,而不是int您想要的。

其他case检查也一样。

关于scanf_s的问题,尝试编译时有警告,应该已经被编译器指出了。

最后,您可能希望在代码中删除不使用的变量,例如kbalance,withdrawdeposit.

于 2013-06-16T15:07:49.513 回答
0

试试这个代码:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    printf("Welcome to skybank\n");
    int deposit,withdraw,kbalance;
    char option;
    printf("Press 1 to deposit cash\n");
    printf("Press 2 to Withdraw Cash\n");
    printf("Press 3 to Know Your Balance\n");
    scanf("%c",&option);
    int decash,wicash;
    switch(option)
    {
    int balance;
    printf("Enter your current Balance\n");
    scanf("%d",&balance);
    case 1:
        printf("Enter the amount you want to deposit\n");
        scanf("%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("%d",&wicash);
        int wibal;
        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();
}
于 2013-06-16T15:07:03.350 回答
0

移动这个:

int balance;
printf("Enter your current Balance\n");
scanf_s("%d",&balance);

switch声明之前。

于 2013-06-16T15:13:37.897 回答