-2

I'm brand new to programming in C (and in general) and I'm stuck on a part for my function. I'm trying to do error checking and I keep getting the error

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?

Pasted below is my full code

    #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;

}

I am also getting warning messages concerning all my ifstatements. It says warning: passing argument 1 of 'exit' makes integer from pointer without a cast [enabled by default] Should I be worried about this? Code::Blocks does not record it as an error. Thank you 8)

waljoedoh
  • 1
  • 2

2 Answers2

0

error: invalid operands to binary < (have 'float *' and 'double')

The error message says exactly what the problem is. startAmt is of type float * and .01 is of type double. You can't compare a pointer with a number, what you need to do is dereference the pointer, to get the value. Put an * before the variable to indicate you want to get the value that is being pointed at.

if (*startAmt < .01)

warning: passing argument 1 of 'exit' makes integer from pointer without a cast [enabled by default]

In this case, you are passing a pointer to an array of characters (e.g. "Starting amount must be at least one cent.\nExiting") to exit which is expecting an integer.

You probably want to do something like:

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

I just used 10 as an example, it will be the number returned to the operating system when your program finishes.

Note that exiting the program when you get incorrect input isn't very friendly.

The Dark
  • 8,453
  • 1
  • 16
  • 19
0
if (startAmt < .01)

is wrong because startAmt is a pointer, not a number. You need to use:

if (*startAmt < .01)

Use of

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

and similar calls to exit are wrong since the input argument needs to be of type int. The above call is equivalent to:

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

i.e. you are calling exit with a char const* instead of an int.

Use:

char const* message = "Starting amount must be at least one cent.\nExiting";
fprintf(stderr, "%s\n", message);
exit(1);
R Sahu
  • 204,454
  • 14
  • 159
  • 270