我对 C 编程(以及一般情况)是全新的,并且我被困在我的函数的一部分上。我正在尝试进行错误检查,但不断收到错误
error: invalid operands to binary < (have 'float *' and 'double')
in line 97 and 100. Does this have to do with the kind of number I am using?
下面粘贴的是我的完整代码
#include <stdio.h>
#include <stdlib.h>
// Function Declarations
void getData (float* startAmt, float* intRate, int* numYears, int* startYear);
void calcTaxes (float startAmt, float intRate, int numYears, int startYear, float* endAmt, float* intEarned, float* percentGained, int* finalYear);
void printResults (float startAmy, float intRate, int numYears, int startYear, float endAmt, float intEarned, float percentGained, int finalYear);
int main (void)
{
// Local Declarations
float startAmt;
float intRate;
int numYears;
int startYear;
float endAmt;
float intEarned;
float percentGained;
int finalYear;
// Statements
getData (&startAmt, &intRate, &numYears, &startYear);
calcTaxes ( startAmt, intRate, numYears, startYear, &endAmt, &intEarned, &percentGained, &finalYear);
printResults ( startAmt, intRate, numYears, startYear, endAmt, intEarned, percentGained, finalYear);
return 0;
}
//~~~~~~~~~~~~~~~ getData ~~~~~~~~~~~~~~~~~~~~
/*
* Function Name: getData
*
* Input Parameters: startAmt, intRate, numYears, startYear
*
* Description: This function reads compound interest data from the keyboard and stores it in the parameters using pointers
*
* Return Value: None
*/
void getData (float* startAmt, float* intRate, int* numYears, int* startYear)
{
// Statements
printf("\nCOP 2220 Project 2: Walter Doherty\n");
printf("\nEnter a Starting amount (dollars and cents): \n");
scanf ("%f", startAmt);
printf("Enter an Interest rate (ex. 2.5 for 2.5%): \n");
scanf ("%f", intRate);
printf("Enter the Number of years (integer number): \n");
scanf ("%d", numYears);
printf("Enter the Starting year (four digits): \n");
scanf ("%d", startYear);
// Validations
if (startAmt < .01)
exit("Starting amount must be at least one cent.\nExiting");
if (intRate < .001)
exit("Interest rate must be at least .1%.\nExiting");
if (numYears < 1)
exit("Number of years must be at least 1.\nExiting");
if (startYear < 999 ^ startYear > 10000)
exit("Year must be four digits\nExiting");
return;
}
我还收到有关我的所有 if
的警告消息声明。它说warning: passing argument 1 of 'exit' makes integer from pointer without a cast [enabled by default]
我应该担心这个吗? Code::Blocks 不会将其记录为错误。
谢谢8)
最佳答案
error: invalid operands to binary < (have 'float *' and 'double')
错误消息准确说明了问题所在。 startAmt
的类型为 float *
,.01
的类型为 double
。您不能将指针与数字进行比较,您需要做的是取消引用指针以获取值。在变量前放置一个 *
表示您想要获取所指向的值。
if (*startAmt < .01)
warning: passing argument 1 of 'exit' makes integer from pointer without a cast [enabled by default]
在这种情况下,您将一个指向字符数组的指针(例如“起始金额必须至少为一美分。\n退出”)传递给需要整数的 exit
。
您可能想做类似的事情:
if (startAmt < .01) {
printf("Starting amount must be at least one cent.\nExiting");
exit(10);
}
我仅以 10 为例,它将是程序完成时返回给操作系统的数字。
请注意,当输入不正确时退出程序不太友好。
关于c - 错误: invalid operands to binary < (have 'float *' and 'double' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33028980/