gpt4 book ai didi

c - C中未声明的标识符

转载 作者:太空宇宙 更新时间:2023-11-04 05:28:01 25 4
gpt4 key购买 nike

我正在尝试在 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();
}

最佳答案

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

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

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

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

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

您对选项 的阅读将不起作用。因为当您检查 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();
}

关于c - C中未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17134586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com