I don't know where I went wrong. I think there misplace or something but I not sure where is it. Its not my code but still I want to know where it goes wrong.
#include <stdio.h>
#include <stdlib.h>
calculateArea(float t, float p, float a);
checkStatus(float a);
int main() {
float t, p, a;
printf("Please enter the height in cm: ");
scanf("%f", &t);
printf("\nPlease enter the length in cm: ");
scanf("%f", &p);
calculateArea(t, p, a);
checkStatus(a);
return 0;
}
int calculateArea(float t, float p, float a) {
a = t * p;
printf("The area of the box is: %.2f cm^2\n", a);
return 0;
}
int checkStatus(float a) {
if (a > 50) {
printf("\nPass");
} else {
printf("\nDefect");
}
return 0;
}
CodePudding user response:
The problem is calculateArea function. It should takes only two parameter since your program only takes float t and float p from user as input. Your calculateArea should look like this.
float calculateArea(float t, float p) {
float area = t * p;
printf("The area of the box is: %.2f cm^2\n", area);
return area;
}
The reason you need to return area is because main function still has no idea about area in calculateArea function. Keep in mind that you also have to change your function declaration to match what you have changed. (In calculateArea function, I use area instead of a to avoid confusion)
Now since you return value from calculateArea(float t, float p), you need to assign this value to a. At this point, your function calling part in main should look like this.
a = calculateArea(t, p);
checkStatus(a);
Now you are good to go. Hope this helps.
Edit: Change from int calculateArea(float t, float p)
to float calculateArea(float t, float p)
. Thanks to chux - Reinstate Monica for correcting my answer.
CodePudding user response:
I guess the question is that it is just a temporary function. Its usable time is limited.When it runs over,all statitic will be free,so does a;A better way to solve it is by pointer.Have a try. Besides,the type of your data is defined kind of wrong,because it may cause the overflow of data.
CodePudding user response:
int calculateArea(float t, float p, float a);
int checkStatus(float a);
Add the "int" may solve this problem? I am a novice.
CodePudding user response:
There are two common ways to achieve your goal. Firstly, you can use function returns which should be float
instead of int
and you need to assign to the a
variable for calculating the area. So the function declaration of the calculating area must be like float calculateArea(float t, float p)
. But, on the other hand, the function should return the calculated value to a
. So the calculateArea process should be like this:
float calculateArea(float t, float p) {
float area = t * p;
printf("The area of the box is: %.2f cm^2\n", a);
return area;
}
According to calculateArea function update main
should be:
int main() {
float t, p, a;
printf("Please enter the height in cm: ");
scanf("%f", &t);
printf("\nPlease enter the length in cm: ");
scanf("%f", &p);
a = calculateArea(t, p);
checkStatus(a);
return 0;
}
Please be aware of how you declared function prototypes. The prototype should be updated too.