返回前正确答案,退货后不正确
我一直在谷歌搜索这几个小时,但没有运气。
我正在使用标准c,调用一个非常简单的方法,并返回正确的值,但返回后该值完全错误。
呼叫:
//declare the gross and ficaTax variables double gross; double ficaTax; //calculate the gross and the ficaTax gross = calcGross(payRate, hours); printf("%f\n", gross); //DELETE
方法:
double calcGross(double rate, double hours){ double gross; //if the person didn't work more than 40 hours if(hours <= 40.0){ gross = hours * rate; } //if the person did work more than 40 hours else{ gross = 40.0 * rate + ((hours - 40.0) * rate * 1.5); } printf("%f \t", gross); return gross; }
我正在打印出方法中的值以尝试解决它,但我无法弄明白。 这是输出:
(正确)(不正确,退货后)
529.600000,-858993459.000000
1371.522500,171798692.000000
100.000000,0.000000
1515.710000,171798692.000000
977.255000,1030792150.000000
5631.360000,687194767.000000
7502.400000,1717986918.000000
4335.106000,584115553.000000
1924.181500,-618475291.000000
683.084000,137438953.000000
1348.424000,755914245.000000
1369.200000,-858993460.000000
529.600000,-858993459.000000
4441.522500,-1030792152.000000
100.000000,0.000000
1882.710000,171798692.000000
我唯一的猜测要么是我的双重回复太长而不适合双倍的长度,但我将它存储在双预回复中并正确打印。 如果是这种情况,我无法弄清楚如何解决它。 我的另一个猜测是我在返回后将其打印错误,但我以同样的方式打印它。
如果重要的话,我正在使用Linux和gcc编译器。 任何帮助,将不胜感激。 我一直试图解决这个看似简单的问题几个小时。
问题是你在第一个例子中调用之前没有为calcgross()
函数提供声明或原型。
在没有看到函数的声明/原型的情况下,C编译器会假设它返回一个int
而不是double
,所以事情非常糟糕。
在调用函数之前将以下行放在某处(理想情况下,在您包含的标题中):
double calcGross(double rate, double hours);
使用-Wall
编译器选项会给你以下警告:
test.c:73:5: warning: implicit declaration of function 'calcGross' [-Wimplicit-function-declaration]
最可能的问题是你在使用之前没有声明calcGross
,在这种情况下编译器将采用默认的返回类型( int
)。 尝试在使用之前放置calcGross()
定义。
愚蠢,微妙的错误。 在两个地方使用“printf(”%lf“,粗略)”我敢打赌问题会消失:)