C中未声明的标识符

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

#include #include 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年版本。 其中一个限制是所有变量必须在任何其他语句之前声明。 因此,将所有变量声明移动到函数的顶部。

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

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

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

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

您阅读option将无效。 因为当您检查1您正在使用ASCII代码检查字符1.将option更改为int并使用%d读取。

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

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

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

 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检查也是case

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

最后,您可能想要删除您未使用的变量的代码,例如kbalancewithdrawdeposit

在视觉c的变量声明中,在块的开头处进行。

例如

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

试试这段代码:

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

移动这个:

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

switch语句之前。