Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Money counter C program

I have generated code that counts the minimum number of 20's, 10's, 5's, 2's and 1's that will add up to a user defined amount of money. The user is only allowed to enter whole numbers i.e. no decimal values. I have two questions.

  1. If a denomination is not needed, the program outputs a random number instead of 0. How can I fix this?
  2. Is it possible to create a function that could replace all of the if statements and possibly the printf statements? I'm new to functions so am a little bit lost with them.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
 int pounds;
 int one, two, five, ten, twenty;

    printf("Enter a pounds amount with no decimals, max E999999: \n");
    scanf("%d", &pounds);
    printf("%d\n", pounds);

    if(pounds >= 20)
    {
        twenty = (pounds / 20);
        pounds = (pounds-(twenty * 20));
        printf("%d\n", pounds);
    }
    if(pounds >= 10)
    {
        ten = (pounds / 10);
        pounds = (pounds-(ten * 10));
        printf("%d\n", pounds);
    }
    if(pounds >= 5)
    {
        five = (pounds / 5);
        pounds = (pounds-(five * 5));
        printf("%d\n", pounds);
    }
    if(pounds >= 2)
    {
        two = (pounds / 2);
        pounds = (pounds-(two * 2));
        printf("%d\n", pounds);
    }
    if(pounds >= 1)
    {
        one = (pounds / 1);
        pounds = (pounds-(one * 1));
        printf("%d\n", pounds);
    }


 printf("The smallest amount of denominations you need are: \n");
 printf("20 x %d\n", twenty);
 printf("10 x %d\n", ten);
 printf("5 x %d\n", five);
 printf("2 x %d\n", two);
 printf("1 x %d\n", one);

return 0;
}
like image 556
adohertyd Avatar asked Jan 05 '12 20:01

adohertyd


3 Answers

This is a great example of why you should initialize your variables when you declare them.

If pounds<20, then twenty will never get initialized. In C, variables have a (basically) random value assigned to them until you replace it with something else.

You just need to do this:

int one = 0, two = 0, five = 0, ten = 0, twenty = 0;
like image 163
Jack Edmonds Avatar answered Oct 26 '22 00:10

Jack Edmonds


To output 0 just initialize all your variables to 0, otherwise they will be assigned "junk" values:

int one = 0, two = 0, five = 0, ten = 0, twenty = 0;
like image 21
Tudor Avatar answered Oct 26 '22 00:10

Tudor


It is always a good practice to initialize all your variables to 0 when you declare them. This way you wont get a random value if there are no denominations to be made. You can declare and initiate your variables at the same time by doing this :

int a = 0;

or if they are many:

int a = 0, b = 0, c = 0;

If you don't initialize your variables before you use them the data they have stored in them will be random things that were in your ram before you executed your program. This is why you get random numbers as answers.

like image 45
Eternal_Light Avatar answered Oct 26 '22 00:10

Eternal_Light