-2

我对 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)

4

2 回答 2

0

错误:二进制 < 的操作数无效(有“float *”和“double”)

错误消息准确地说明了问题所在。startAmt是 类型float *并且.01是 类型double。您不能将指针与数字进行比较,您需要做的是取消引用指针以获取值。在变量之前放置一个*表示您想要获取所指向的值的变量。

if (*startAmt < .01)

警告:传递 'exit' 的参数 1 从没有强制转换的指针生成整数 [默认启用]

在这种情况下,您正在传递一个指向字符数组的指针(例如“起始金额必须至少为一美分。\n退出”),exit它需要一个整数。

您可能想要执行以下操作:

if (startAmt < .01) {
   printf("Starting amount must be at least one cent.\nExiting");
   exit(10);
}

我只是以 10 为例,它将是您的程序完成时返回给操作系统的数字。

请注意,当您输入错误时退出程序不是很友好。

于 2015-10-09T02:42:00.180 回答
0
if (startAmt < .01)

是错误的,因为startAmt是指针,而不是数字。你需要使用:

if (*startAmt < .01)

用于

exit("Starting amount must be at least one cent.\nExiting");

和类似的调用exit都是错误的,因为输入参数需要是 type int。上面的调用相当于:

char const* message = "Starting amount must be at least one cent.\nExiting";
exit(message);

即你exit用 achar const*而不是a 打电话int

利用:

char const* message = "Starting amount must be at least one cent.\nExiting";
fprintf(stderr, "%s\n", message);
exit(1);
于 2015-10-09T02:43:56.857 回答