我是C语言编程的新手(一般而言),而且我的功能部分都停留在一部分上。我正在尝试进行错误检查,并且一直收到错误
error: invalid operands to binary < (have 'float *' and 'double')
第97行和第100行。这与我使用的那种数字有关吗?
下面粘贴的是我的完整代码
#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)
答案 0 :(得分:0)
错误:无效操作数到二进制&lt; (有'浮动*'和'双倍')
错误消息确切地说明了问题所在。 startAmt
的类型为float *
,.01
的类型为double
。您无法将指针与数字进行比较,您需要做的是取消引用指针,以获取值。在变量前加上*
表示您想获得指向的值。
if (*startAmt < .01)
警告:传递'exit'的参数1会使指针中的整数不带强制转换[默认情况下启用]
在这种情况下,您将指向一个字符数组(例如“起始金额必须至少为1美分。\ nExiting”)传递给期望整数的exit
。
你可能想做类似的事情:
if (startAmt < .01) {
printf("Starting amount must be at least one cent.\nExiting");
exit(10);
}
我只使用10作为示例,它将是程序完成后返回操作系统的编号。
请注意,输入错误时退出程序不是很友好。
答案 1 :(得分:0)
if (startAmt < .01)
是错误的,因为startAmt
是指针,而不是数字。你需要使用:
if (*startAmt < .01)
使用
exit("Starting amount must be at least one cent.\nExiting");
和对exit
的类似调用是错误的,因为输入参数必须是int
类型。上述调用相当于:
char const* message = "Starting amount must be at least one cent.\nExiting";
exit(message);
即。您使用exit
而不是char const*
来呼叫int
。
使用:
char const* message = "Starting amount must be at least one cent.\nExiting";
fprintf(stderr, "%s\n", message);
exit(1);