0

I'm having a compiling issue with my code and I can not figure it out. It is saying that my variables are not initialized in a few areas. Can someone help me figure out why this may be? I've tried writing it several other ways but I must be missing something. Thank you in advance.

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
int pennies; 
int quarters; 
int nickels;
int dimes;
int total;
int coins;
int change;

printf("How much change is owed?: ")
scanf("%i", &change);

coins = round(change * 100);

do
{
while (coins >=25) {
coins = coins - 25; quarters++; total++;
}


while (coins >=10) {
coins = coins - 10; dimes++; total++;
}


while (coins >=5) {
coins = coins - 5; nickels++; total++;
}


while (coins >=1) {
coins = coins - 1; pennies++; total++;
}
}
while (coins !=0); {
printf("\n");
}
printf("quarters: %i \n dimes: %i \n nickels: %i \n pennies: %i \n total: %i \n");
return 0;
}

enter image description here

1 Answer 1

0

when you try to increment total the first time, it's not initialized. Yes it's DECLARED but not INITIALIZED. So it contains garbage value. Change int total; to int total = 0; and similar for other variables.

1
  • Oh wow, not sure how I skipped that step. Thank you for the help Hussain!
    – vslateart
    Commented Sep 22, 2015 at 19:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .