Reputation: 35
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define CONST 267
void getInput(int *length, int *width, int *height);
void calcoutput(int length, int width, int height, int *squareFootage,int *paintNeeded);
int getSquareFootage(int length,int width, int height);
double getPaintNeeded(int squareFootage);
int main(void)
{
int length;
int width;
int height;
int squareFootage;
double paintNeeded;
getInput(&length, &width, &height);
calcoutput(length, width, height,&squareFootage,&paintNeeded);
return 0;
} //end main
void getInput(int *plength, int *pwidth, int *pheight)
{
printf("Enter the length of the room (whole number only): ");
scanf("%d", plength);
printf("Enter the width of the room (whole number only): ");
scanf("%d", pwidth);
printf("Enter the height of the room (whole number only): ");
scanf("%d", pheight);
} //end getInput
void calcoutput(int length, int width, int height, int *squareFootage,int *paintNeeded){
*squareFootage = getSquareFootage(length,width, height);
*paintNeeded = getPaintNeeded(squareFootage);
}
int getSquareFootage(int length,int width, int height){
int i;
i = 2*(length* height) + 2*(width*height) + (length* width);
return i;
}
double getPaintNeeded(int squareFootage)
{
double i = double (squareFootage / CONST);
return i;
}
i'm writing this code to calculate the area of the room and number of gallons of paint needed to paint the room, however, I'm not very familiar with pointers in C, there seems to be some errors and warning like this
C:\Users\khoavo\Desktop\hw2b.c||In function 'main':|
C:\Users\khoavo\Desktop\hw2b.c|23|warning: passing argument 5 of 'calcoutput' from incompatible pointer type|
C:\Users\khoavo\Desktop\hw2b.c|8|note: expected 'int *' but argument is of type 'double *'|
C:\Users\khoavo\Desktop\hw2b.c||In function 'calcoutput':|
C:\Users\khoavo\Desktop\hw2b.c|41|warning: passing argument 1 of 'getPaintNeeded' makes integer from pointer without a cast|
C:\Users\khoavo\Desktop\hw2b.c|10|note: expected 'int' but argument is of type 'int *'|
C:\Users\khoavo\Desktop\hw2b.c||In function 'getPaintNeeded':|
C:\Users\khoavo\Desktop\hw2b.c|52|error: expected expression before 'double'|
||=== Build finished: 1 errors, 2 warnings ===|
how will I be able to fix these errors and warning ? thank you in advance.
Upvotes: 0
Views: 604
Reputation: 6668
The error messages say it all:
calcoutput takes an int* as its fifth argument but you're passing it a double*. Change the fifth parameter to take a double*.
getPaintNeeded takes an int but you're passing it an int*. I think what you want in this case is getPaintNeeded(*squareFootage)
.
The last error is about the cast. You're using a function-style cast which supported in C++ but not in C, and you're compiling as C. Either compile as C++ (change the file extension to .cpp), or change the line to:
double i = (double)(squareFootage / CONST);
Actually you don't need a cast at all, the result can implicitly be converted to a double.
Upvotes: 2
Reputation: 13974
paintNeeded is declared a double but you're passing it's location as a pointer to an int. The function you have passed it to will see the value as in integer instead of a double, which would make your program run incorrectly.
You should consider converting the int* in calcoutput into a double* so that passing in paintNeeded would behave correctly.
Upvotes: 0
Reputation: 41170
Change
void calcoutput(int length, int width, int height, int *squareFootage,int *paintNeeded);
to
void calcoutput(int length, int width, int height, int *squareFootage,double *paintNeeded);
Upvotes: 0