C中未声明的标识符

时间:2013-06-16 14:59:14

标签: c visual-studio-2012

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

#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();
}

5 个答案:

答案 0 :(得分:5)

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

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

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

同样,你需要传递一个地址来读取balance

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

您对option的阅读不起作用。因为当您检查1时,您正在使用ASCII代码检查字符1.将option更改为int并使用%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();
}

答案 1 :(得分:1)

在视觉c变量声明的块的开头做。

E.g。

int main()
{
    int deposit,withdraw,kbalance;
    char option;
    int decash,wicash
    int balance;
    int wibal;
...

答案 2 :(得分: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,因为它是char,而不是您想要的int

其他case项检查相同。

关于scanf_s问题,尝试编译警告,编译器应该指出它。

最后,您可能希望删除代码中没有使用的变量,例如kbalancewithdrawdeposit

答案 3 :(得分: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();
}

答案 4 :(得分:0)

移动它:

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

switch陈述之前。