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 20:05:57 浏览: 140
在 `income` 函数中,您在函数签名上声明了返回类型 `void`,但在函数体中却使用了 `return cash;` 语句,这是不允许的。如果您想要返回 `cash` 的值,应该将函数签名修改为 `float income(float amount)`,并在函数体中使用 `return cash;` 语句。另外,在 `expend` 函数中也存在类似的问题。
另外,您在选择错误时使用了无限循环 `while(1)`,但并没有在退出程序时使用 `break` 语句来跳出循环,这可能导致程序一直运行下去。建议在 `choice==4` 的时候添加 `break;` 语句来退出循环,或者在 `while(1)` 的外层添加一个 `if` 条件判断来控制程序的退出。
相关问题
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; }修改成正确代码
请参考下面的代码,其中我对您原有的代码进行了修正:
```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()` 语句,以等待用户在程序退出前按下任意键,以防止程序突然退出。
任务是;全局变量与现金收支账 这个任务中我们将实现一个现金收入、支出登记及现金余额查询的程序。与以往所有程序不同的地方在于,这个程序中用到了所谓的“全局变量”来保存现金,这个变量的名称是cash。 程序由main、income、expend三个函数构成。income函数用于登记现金收入(cash增加),expend用于登记现金支出(cash减少),main函数显示一个菜单让用户选择要执行的功能:当选择现金收入、现金支出时,要求用户输入现金额,并分别调用income、expend函数增加、减少全局变量cash中的值。 为了将注意力集中在全局函数的运用上,我们提供了这个程序中main函数的代码,请根据程序的功能要求在程序中添加income、expend两个函数的代码,以最终实现现金收入、支出的登记。请根据这个将下列函数修正#include<stdio.h> void income(float); void expend(float); float cash=0; void main() { int choice; float amount; while(1) { makechoice; printf("1-Income,2-Expenf,3-Balance,0-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("Thinks!\n"); break; } if(choice == 3) { printf("Current balance:%.2f\n\n",cash) goto makechoice; } } }
经过分析,我们需要在程序中添加income、expend两个函数的代码,以最终实现现金收入、支出的登记。根据题目要求,我们可以编写income和expend函数如下:
```
void income(float amount)
{
cash += amount;
printf("Income successfully recorded!\n\n");
}
void expend(float amount)
{
if(amount > cash)
{
printf("Not enough cash to expend!\n\n");
}
else
{
cash -= amount;
printf("Expend successfully recorded!\n\n");
}
}
```
在income函数中,我们将传入的amount参数加到全局变量cash中,并输出收入成功的提示信息;在expend函数中,我们首先判断全局变量cash是否足够支出,如果不足则输出提示信息,否则将传入的amount参数减去全局变量cash并输出支出成功的提示信息。
接下来,我们需要在main函数中添加代码来调用income、expend函数,以实现现金收入、支出的登记。修改后的main函数如下:
```
void main()
{
int choice;
float amount;
while(1)
{
makechoice:
printf("1-Income,2-Expend,3-Balance,0-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;
}
if(choice == 1)
{
printf("Please input the income amount:");
scanf("%f", &amount);
income(amount);
}
if(choice == 2)
{
printf("Please input the expend amount:");
scanf("%f", &amount);
expend(amount);
}
}
}
```
在修改后的main函数中,我们添加了两个if语句分别处理现金收入、支出的情况。当用户选择1时,提示用户输入收入金额,然后调用income函数;当用户选择2时,提示用户输入支出金额,然后调用expend函数。
修改后的程序完整代码如下:
```
#include<stdio.h>
void income(float);
void expend(float);
float cash = 0;
void main()
{
int choice;
float amount;
while(1)
{
makechoice:
printf("1-Income,2-Expend,3-Balance,0-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;
}
if(choice == 1)
{
printf("Please input the income amount:");
scanf("%f", &amount);
income(amount);
}
if(choice == 2)
{
printf("Please input the expend amount:");
scanf("%f", &amount);
expend(amount);
}
}
}
void income(float amount)
{
cash += amount;
printf("Income successfully recorded!\n\n");
}
void expend(float amount)
{
if(amount > cash)
{
printf("Not enough cash to expend!\n\n");
}
else
{
cash -= amount;
printf("Expend successfully recorded!\n\n");
}
}
```
阅读全文