简体   繁体   中英

Output returns wrong result

#include <stdio.h>

#define GA_OF_PA_NEED 267.0

int getSquareFootage(int squareFootage);
double calcestpaint(int squareFootage);
double printEstPaint(double gallonsOfPaint);

int main(void)

{
  //Declaration
  int squareFootage = 0;
  double gallonsOfPaint = 0;


  //Statements
  getSquareFootage(squareFootage);
  gallonsOfPaint = calcestpaint(squareFootage);
  gallonsOfPaint = printEstPaint(gallonsOfPaint);
  system("PAUSE");
  return 0;
}

int getSquareFootage(int squareFootage)
{
  printf("Enter the square footage of the surface: ");
  scanf("%d", &squareFootage);
  return squareFootage;
}

double calcestpaint( int squareFootage)
{
  return (double) (squareFootage * GA_OF_PA_NEED);    
}
double printEstPaint(double gallonsOfPaint)
{

  printf("The estimate paint is: %lf\n",gallonsOfPaint);
  return gallonsOfPaint;
}

Why does my output show gallonsOfPaint as 0.0, there was no error and everything seems to be logically right. It seems like something is wrong with the calculate statement in calc function.

You need to assign the result of getSquareFootage(squareFootage); :

squareFootage = getSquareFootage(squareFootage);

As squareFootage is passed by value and not by reference or in other words, it doesn't matter how much you change it in the function, it will have no effect outside the function. alternatively, you could pass it by reference:

void getSquareFootage(int * squareFootage)
{
     printf("Enter the square footage of the surface: ");
     scanf("%d", squareFootage);
}

which will be called like this:

getSquareFootage(&squareFootage);

Correct as this squareFootage=getSquareFootage();

No need to pass a parameter.

you are not updating the variable squareFootage. When you call calcestpaint(squareFootage) you are passing the value 0 as argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM