float cash=0; void income(float amount); void expend(float amount); void main() { float amount; int choice; while(1) { makechoice: printf("1-Income,2-Expend,3-Balance,4-Exit\n"); printf("Make a choice,please:"); scanf("%d",&choice); if(choice<0||choice>3) { printf("Select error,please re-enter!\n\n"); goto makechoice; } if(choice==0) { printf("Thanks!\n"); break; } if(choice==3) { printf("Current balance:%.2f\n\n",cash); goto makechoice; } printf("Enter the amount of cash:"); scanf("%f",&amount); if(choice==1) income(amount); else expend(amount); printf("\n"); } system("pause"); } void income(float amount) { cash=cash+amount; return cash; }修改成正确代码
时间: 2024-01-09 08:05:57 浏览: 102
请参考下面的代码,其中我对您原有的代码进行了修正:
```c
#include <stdio.h>
float cash = 0;
void income(float amount);
void expend(float amount);
int main() {
float amount;
int choice;
while (1) {
makechoice:
printf("1-Income,2-Expend,3-Balance,4-Exit\n");
printf("Make a choice,please:");
scanf("%d", &choice);
if (choice < 1 || choice > 4) {
printf("Select error,please re-enter!\n\n");
goto makechoice;
}
if (choice == 4) {
printf("Thanks!\n");
break;
}
if (choice == 3) {
printf("Current balance:%.2f\n\n", cash);
goto makechoice;
}
printf("Enter the amount of cash:");
scanf("%f", &amount);
if (choice == 1)
income(amount);
else
expend(amount);
printf("\n");
}
getchar();
return 0;
}
void income(float amount) {
cash += amount;
}
void expend(float amount) {
if (amount > cash) {
printf("Insufficient balance!\n");
} else {
cash -= amount;
}
}
```
在上面的代码中,我对 `main` 函数中的选择逻辑和循环进行了修正,并且修改了 `income` 函数和 `expend` 函数的返回类型和逻辑。需要注意的是,在 `main` 函数的末尾,我添加了一个 `getchar()` 语句,以等待用户在程序退出前按下任意键,以防止程序突然退出。
阅读全文