C中未声明的标识符 [英] undeclared identifier in C

查看:72
本文介绍了C中未声明的标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 "syntax error: missing ';' before 'type'".Please tell me the correct syntax.Thank you.

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

推荐答案

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

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.

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

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);

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

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

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

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

您对option 的阅读不起作用.因为当您检查 1 时,您正在检查 ASCII 码为 1 的字符.将 option 更改为 int 并使用 读取%d.

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

这篇关于C中未声明的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆