[英]Can someone tell me why the functions are not returning a value? New to C
这段代码只是想得到收入,支出,然后在第三个function中做一些计算。我不确定问题出在哪里,当我运行它时,我一直得到值0.00。 这是我和一个朋友正在做的一个个人项目,目的是帮助获得一些 C 的经验。它可能与 switch 语句有关吗?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct expense
{
char name[50];
float amount;
};
struct income
{
char name[50];
float amount;
};
float getExpense(void);
float getIncome(void);
float budgetCalc(float sum_exp, float sum_inc);
int main()
{
float sum_exp, sum_inc;
int option;
printf("\t\t**************************************************************************\n");
printf("\t\t* Thank you for choosing Ford$ as your personal budget tracker! *\n");
printf("\t\t* *\n");
printf("\t\t* *\n");
printf("\t\t* The goal of this program is to allow you track your finances! You may *\n");
printf("\t\t* enter your specified expenses, income streams. The amount for each of *\n");
printf("\t\t* the expenses and income. Then, the results will be displayed. You will *\n");
printf("\t\t* be brought back to the selection-screen after you have entered your *\n");
printf("\t\t* data. From the selection-screen, if you are finished, you may quit the *\n");
printf("\t\t* program if you are finished by selecting 0. The first feature will *\n");
printf("\t\t* give you the option to enter your income. The second feature is the *\n");
printf("\t\t* expenses feature. The third will do some calculations with the given *\n");
printf("\t\t* details but only after you have entered your data from the first and *\n");
printf("\t\t* the second feature! *\n");
printf("\t\t* *\n");
printf("\t\t* Made by ****** ******* & ***** **** *\n");
printf("\t\t* *\n");
printf("\t\t* *\n");
printf("\t\t* *\n");
printf("\t\t* Please select a feature from below: *\n");
printf("\t\t* *\n");
START:printf("\t\t*********************************Features**********************************\n\n\t\t\t\t \t\t");
printf("(1) Track Income \n\t\t\t\t\t\t");
printf("(2) Track Expenses \n\t\t\t\t\t\t");
printf("(3) Calculator \n\t\t\t\t\t\t");
printf("(0) Exit \n\n\t\t\t\t\t\t");
printf("Enter: ");
scanf("%d", &option);
switch(option){
case 1:
getIncome();
goto START;
case 2:
getExpense();
goto START;
case 3:
budgetCalc(sum_exp, sum_inc);
goto START;
case 0:
printf("\n\t\t\t\t\t\tThank you for choosing Ford$!");
printf("\n\t\t\t\t\t\tNOW EXITING PROGRAM.....");
exit;
}
return 0;
}
这为我们提供了用户输入的费用。 使用 struct expense 来存储值和数组,这和 income 都可以正常工作。
float getExpense(){
struct expense arr_expense[1];
int i;
int num_exp;
float sum_exp=0;
printf("\t\t\t\t\t\tHow many expenses do you have? ");
scanf("%d", &num_exp);
for(i = 0; i < num_exp; i++ )
{
printf("\n\t\t\t\t\t\tEnter details of Expense %d\n\n", i+1);
printf("\t\t\t\t\t\tEnter name of the bill: ");
scanf("%s", arr_expense[i].name);
printf("\t\t\t\t\t\tEnter amount of payment: ");
scanf("%f", &arr_expense[i].amount);
}
printf("\n\t\t\t\t\t\tName:\t\tAmount:\t\n");
for(i = 0; i < num_exp; i++ )
{
printf("\t\t\t\t\t\t%s\t\t%.2f\n",
arr_expense[i].name, arr_expense[i].amount);
}
for(i = 0; i < num_exp; i++)
{
sum_exp = sum_exp + arr_expense[i].amount;
}
printf("\n\t\t\t\t\t\tTotal expenses are: $%.2f\n\n", sum_exp);
printf("\t\t\t\t\t\tNOW RETURNIG TO SELECTION-SCREEN.....\n");
return sum_exp;
}
这给了我们用户输入的收入。 使用 struct income 将收入存储在数组中,然后在最后显示结果。
float getIncome(){
struct income arr_income[1];
int i;
int num_inc;
float sum_inc=0;
printf("\t\t\t\t\t\tHow many forms of income do you have? ");
scanf("%d", &num_inc);
for(i = 0; i < num_inc; i++ )
{
printf("\n\t\t\t\t\t\tEnter details of Income %d\n\n", i+1);
printf("\t\t\t\t\t\tEnter name of the Income stream: ");
scanf("%s", arr_income[i].name);
printf("\t\t\t\t\t\tEnter the amount paid to you: ");
scanf("%f", &arr_income[i].amount);
}
printf("\n\t\t\t\t\t\tName:\t\tAmount:\t\n");
for(i = 0; i < num_inc; i++ )
{
printf("\t\t\t\t\t\t%s\t\t%.2f\n",
arr_income[i].name, arr_income[i].amount);
}
for(i = 0; i < num_inc; i++)
{
sum_inc = sum_inc + arr_income[i].amount;
}
printf("\n\t\t\t\t\t\tTotal Income is: $%.2f\n\n", sum_inc);
printf("\t\t\t\t\t\tNOW RETURNIG TO SELECTION-SCREEN.....\n");
return sum_inc;
}
这个 function 使用给定的数据运行一些计算。 目前,它只是应该显示收入的价值。 但这里唯一显示的是值 0.00 而不是实际值。 我认为它与功能和传递有关,但我看不出哪里出了问题。
float budgetCalc(float sum_inc, float sum_exp){
printf("\t\t\t\t\t\tYou earn $%.2f! ", sum_inc);
}
在getIncome
中,您创建一个大小为 1 的数组:
struct income arr_income[1];
然后尝试遍历num_inc
数组元素。 这会写入数组末尾,调用未定义的行为。
您应该在读取num_inc
的值后定义数组,然后使用该值作为数组的大小。
scanf("%d", &num_inc);
struct income arr_income[num_inc];
getExpense
function 也有同样的问题。
此外,您没有使用这两个函数的返回值。 您需要将它们的返回值分配给main
中的关联变量,以便它们具有有意义的值。
case 1:
sum_inc = getIncome();
goto START;
case 2:
sum_ext = getExpense();
goto START;
作为旁注,这不是使用goto
的好地方。 请改用循环。
这个数组: struct income arr_income[1];
用 1 个元素定义,稍后尝试访问超过 1 个元素,导致您的程序调用未定义的行为。
更改此:(此处和getExpense()
中的类似内容)
struct income arr_income[1];
int i;
int num_inc;
float sum_inc=0;
printf("\t\t\t\t\t\tHow many forms of income do you have? ");
scanf("%d", &num_inc);
为此:使用可变长度数组
int i;
int num_inc;
float sum_inc=0;
printf("\t\t\t\t\t\tHow many forms of income do you have? ");
scanf("%d", &num_inc);
struct income arr_income[num_inc] ={0};//correctly sized VLA (array) of struct.
“有人能告诉我为什么函数没有返回值吗?”
它们是,但您的代码没有读取返回值。
该段缺少左值:
switch(option){
case 1:
getIncome();// this function returns a value, but value is lost
goto START;
case 2:
getExpense();//ditto
goto START;
将其更改为:
switch(option){
case 1:
sum_inc = getIncome();//return value is captured in sum_inc
goto START;
case 2:
sum_exp = getExpense();//return value is captured in sum_exp
goto START;
最后,关于这个部分:
printf("\n\t\t\t\t\t\tEnter details of Expense %d\n\n", i+1);
printf("\t\t\t\t\t\tEnter name of the bill: ");
scanf("%s", arr_expense[i].name);
printf("\t\t\t\t\t\tEnter amount of payment: ");
scanf("%f", &arr_expense[i].amount);
这里有一些使用更灵活的格式化技术替换\t
字符的提示和建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.