头文件创建未声明的值

时间:2013-05-06 23:55:40

标签: c header structure

我似乎无法绕过这个。我有其他程序的相同设置,这是一个堆栈,推/弹,它的工作完美。我在函数错误中首次使用未声明的值。任何帮助,将不胜感激。

头文件

#include <stdio.h>
#include <string.h>

int money();
void amortization();

typedef struct{
int principle;
int rate;
int payments;
} loan_t;

功能代码

int money(loan_t)
{
 printf("Please input the amount borrowed:");
 scanf("%d", &principle);
 printf("\nPlease input the Annual Interest Rate:");
 scanf("%d", &rate);
 printf("\nPlease input the number of monthly payments:\n");
 scanf("%d", &payments);
 return (principle,rate,payments);
 }
谢谢你!

2 个答案:

答案 0 :(得分:1)

尝试:

 int money(LOAN *var){
     printf("Please input the amount borrowed:");
     scanf("%lf", var->principle);
     printf("\nPlease input the Annual Interest Rate:");
     scanf("%lf", var->rate);
     printf("\nPlease input the number of monthly payments:\n");
     scanf("%lf", var->payments);
     return 0;
 }

将标题更改为:

#include <stdio.h>
#include <string.h>

typedef struct loan{
    double principle;
    double rate;
    double payments;
}LOAN;

int money(LOAN *var);
void amortization();

这意味着您将在

中返回具有所需值的loan_t

编辑:编辑以最适合您,而不是Jonathan Leffler推荐的最佳实践

LOAN variable;内声明main(),然后致电money(&variable);

答案 1 :(得分:0)

根据您提供的内容,principle中的ratepaymentsmoney似乎都未声明。您还在money中提供了一个没有参数名称的参数作为参数。

编辑:正如另一位回答者所指出的那样,回归也很可疑。

我现在无法自己测试,但请尝试:

loan_t money(loan_t loan)
{
 printf("Please input the amount borrowed:");
 scanf("%lf", &(loan.principle));
 printf("\nPlease input the Annual Interest Rate:");
 scanf("%lf", &(loan.rate));
 printf("\nPlease input the number of monthly payments:\n");
 scanf("%lf", &(loan.payments));
 return loan;
 }