0

I have written the following code and when I try to run it, I get the following warnings:

  • warning: variable 'money' is uninitialized when used here [-Wuninitialized] GetData (money, product);

  • note: initialize the variable 'money' to silence this warning int product, money, change;

  • warning: variable 'product' is uninitialized when used here [-Wuninitialized] GetData (money, product);

  • note: initialize the variable 'product' to silence this warning int product, money, change;

  • warning: variable 'change' is uninitialized when used here [-Wuninitialized] CalculateChange (money, product, change);

  • note: initialize the variable 'change' to silence this warning int product, money, change;

I have also typed an arrow to the relevant line. I have been trying to figure out how to fix this problem for an hour now. If you could please help, it would be much appreciated.

#include <stdio.h>
#include <ctype.h>

void GetData (int money, int product)
{
  // Asks user how much was the product and how much they paid the cashier. //
  printf("\nHow much was the product?\n");
  scanf("%d", &product);

  if((product < 5) || (product > 95) || (product %5 != 0))

  printf("\nInvalid number!\nNumber has to be a multiple of 5 and cannot be less than 5 or 
  more than 95.\n");

  else printf("How much did you give to the cashier? \n");
  scanf("%d", &money);

  if (money < product)
  printf("You did not give enough money to the cashier!");

  return;
}

void CalculateChange (int money, int product, int change)
{
  // Calculates the amount of change needed. //
  int fifty = 0;
  int twenty = 0;
  int ten = 0;
  int five = 0;

  change = (money - product);

  fifty += change / 50;
  change %= 50;

  twenty += change / 20;
  change %= 20;

  ten += change / 10;
  change %= 10;

  five += change / 5;
  change %= 5;

  return;
}

void PrintResults (int fifty, int twenty, int ten, int five)
{
  // Prints the amount of coins needed. //
  printf("\nThe number of fifty cents: %d", fifty);
  printf("\nThe number of twenty cents: %d", twenty);
  printf("\nThe number of ten cents: %d", ten);
  printf("\nThe number of five cents: %d", five);

return;
}

int main ()
{
  int product, money, change; <------
  int fifty = 0;
  int twenty = 0;
  int ten = 0;
  int five = 0;
  char response;

do {

  GetData (money, product); <-------

  CalculateChange (money, product, change);

  PrintResults (fifty, twenty, ten, five);

  printf("\nWould like to quit the program? Enter y or n\n");
  scanf("%c", &response );
  response = tolower (response);
  printf("--------------------------------------------------------------------------");

} while (response != 'y');
  printf("\nQuitting program...\n");

  return (0);
}
greg-449
  • 109,219
  • 232
  • 102
  • 145

2 Answers2

1

You have to pass reference to variables in GetData() else they are not modified by the function. I assume the compiler thinks that as they are not passed as references, they are inputs and thus should be initialized.

Same for change variabel in CalculateChange

Dharman
  • 30,962
  • 25
  • 85
  • 135
XL64
  • 66
  • 3
0

The money and product variables are indeed uninitialized when GetData is called. Passing their addresses will make the values passed have a useful value (i.e., their addresses which are, of course, initialized).

With respect to money and product, the GetData procedure modifies them but their values are lost after GetData returns. You should pass the address of money and product and modify GetData to properly reference and dereference them. That way, after GetData returns, the rest of the code can see the values written to them.

The new call in main:

GetData((&money, &product);

The new GetData signature:

void GetData (int *money, int product)

And then within GetData:

  • change product and money to *product and *money
  • change &product and &money to product and money

The same thing goes for change.

Jeff Holt
  • 2,940
  • 3
  • 22
  • 29