-2

I'm attempting to write a program in which:

  • The user inputs the cost of an item
  • The user inputs the amount they paid for the item
  • The program determines if the user is owed any change
  • The program calculates the amount of change owed
  • The program uses the modulus operator to break the amount of change down into coin denominations
  • The program stores the change and coin denominations into an array This is the first bit at which I'm getting stuck
  • The program displays the amount of change in coin denominations to the user

The purpose is to use an array to hold the values of the coins, so I "can write a general purpose change calculator that can be used for any coinage by changing the contents of the array".

Here is my code:

void vendingMachine()
{

    // Declarations
    #define ARRAY_LENGTH 6

    int itemCost;
    int amountEntered;
    int fifty, twenty, ten, five, two, one; 
    int remainder;

    // User input

    printf("Please enter the cost of the item in pence: ");
    scanf_s("%d", &itemCost);
    while (itemCost <= 0 || itemCost > 99)
    {
        printf("You've entered an invalid amount. Please enter an amount between 1p and 99p: ");
        scanf_s("%d", &itemCost);
    }

    printf("Please enter the amount entered into the machine in pence: ");
    scanf_s("%d", &amountEntered);
    while (amountEntered <= 0 || amountEntered > 100)
    {
        printf("You've entered an invalid amount. Please enter an amount between 1p and 100p: ");
        scanf_s("%d", &amountEntered);
    }

    while (amountEntered < itemCost)
    {
        printf("You've entered an invalid amount. Please enter an amount equal to or higher than the cost of the item: ");
        scanf_s("%d", &amountEntered);
    }

    // Program to determine if the customer is owed any change and, if so, how much is owed

    if (amountEntered == itemCost)
    {
        printf("No change is owed to the customer");
    }
    else if (amountEntered > itemCost)
    {
        int change = amountEntered - itemCost;
        printf("The amount of change owed to the customer is: %d pence, broken down as follows: \n", change);

        fifty = change / 50;
        remainder = change % 50;
        twenty = remainder / 20;
        remainder = remainder % 20;
        ten = remainder / 10;
        remainder = remainder % 10;
        five = remainder / 5;
        remainder = remainder % 5;
        two = remainder / 2;
        remainder = remainder % 2;
        one = remainder;

         // Program to store the change in an array

        int count[ARRAY_LENGTH];
        count[0] = fifty;
        count[1] = twenty;
        count[2] = ten;
        count[3] = five;
        count[4] = two;
        count[5] = one;

        for (int i = 0; i < ARRAY_LENGTH; i++)
        {
             count[i] = 0;
        }

        for (int i = 0; i < ARRAY_LENGTH; i++)
        {
            printf("The number of %d coins is: %d\n", //I don't know what to do here);
        }
    }
}
hailnolly
  • 33
  • 1
  • 10
  • What's the point of `count[i] = 0` which deletes all the assignments you did before? – Henning Koehler Nov 12 '18 at 11:17
  • In entire honesty, I got stuck here and was using code from the notes I was given. I don't think there is a point - thank you for pointing out what that code does. – hailnolly Nov 12 '18 at 11:26
  • "This is the first bit at which I'm getting stuck" --> How/why are you stuck? What is unclear about the functionality at `for (int i = 0; i < ARRAY_LENGTH; i++) { count[i] = 0; }`? – chux - Reinstate Monica Nov 12 '18 at 15:13

2 Answers2

0

Store the type of coins in an array as well, e.g.

const int coins[ARRAY_LENGTH] = { 50, 20, 10, 5, 2, 1 };

Then you can easily refer to them in your loop:

printf("The number of %d coins is: %d\n", coins[i], count[i]);

This also allows you to perform your modulo calculations in a loop.

Henning Koehler
  • 2,456
  • 1
  • 16
  • 20
0

I am not sure what you are trying to achieve here:

The following piece of (your) code sets the values of count from index 0 to index 5, starting from fifty to one..

int count[ARRAY_LENGTH];
count[0] = fifty;
count[1] = twenty;
count[2] = ten;
count[3] = five;
count[4] = two;
count[5] = one;

Then here, you are overwriting those with 0 in the for loop.

for (int i = 0; i < ARRAY_LENGTH; i++)
{
     count[i] = 0;
}

So above loop is not required, or at least must not be placed after you have assigned values of fifty, twenty, ten, five, two and one to count array indices.

I guess you are trying to print them? You don't have to use a loop here:

// Doing it the newbie-way:

printf("The number of coins of 50 are: %d\n", count[0]);
printf("The number of coins of 20 are: %d\n", count[1]);
printf("The number of coins of 10 are: %d\n", count[2]);
printf("The number of coins of 5 are: %d\n", count[3]);
printf("The number of coins of 2 are: %d\n", count[4]);
printf("The number of coins of 1 are: %d\n", count[5]);
WedaPashi
  • 3,561
  • 26
  • 42
  • Thanks! I really didn't realise (I'm new to this language, and programming in general) I was cancelling out my array, which is really embarrassing. Thank you for taking the time to help me. – hailnolly Nov 12 '18 at 16:46
  • @hailnolly: No, its not embarrassing at all. We all have been learning from out mistakes in programming :-) – WedaPashi Nov 13 '18 at 04:45