Question

I am new to C, know python well but I'm struggling with some basic things and it's really frustrating, because I can't seem to pinpoint where my code is not letting the while loop break, until it hits numbers that I'm assuming has to do with the buffer, and then it just hits every condition in my program, anyone have some insight?

here's the output I get when I insert 10:

Welcome to the Soda Vending Machine
All sodas cost $1.50 each
This machine accepts the following coins:
nickel = 5, dime = 10, quarter = 25, half-dollar = 50, dollar = 100
Please use the numeric value corresponding to each coin
10
Amount depositied so far: $ 32767
Amount depositied so far: $ 32777
Amount depositied so far: $ 32777
Amount depositied so far: $ 32777
Amount depositied so far: $ 32777
Invalid Coin
Dispensing soda ...
Your change is: Half dollar quarter dime nickel







int main(void)

int coin;
int totalcoin;
char *change = "Your change is:" ;
printf("Welcome to the Soda Vending Machine\n===================================\nAll sodas cost $1.50 each\nThis machine accepts the following coins:\nnickel = 5, dime = 10, quarter = 25, half-dollar = 50, dollar = 100\nPlease use the numeric value corresponding to each coin\n");
while (totalcoin < 150)
    printf("Deposit a Coin: ");
    scanf("%d", &coin);
    if ( coin == 5)
        totalcoin = totalcoin + 5;
        printf("Amount depositied so far: $ %d\n", totalcoin);
    if (coin == 10)
        totalcoin = totalcoin + 10;
        printf("Amount depositied so far: $ %d\n", totalcoin);
    if (coin == 25)
        totalcoin = totalcoin + 25;
        printf("Amount depositied so far: $ %d\n", totalcoin);
    if (coin == 50)
        totalcoin = totalcoin + 50;
        printf("Amount depositied so far: $ %d\n", totalcoin);
    if (coin == 100)
        totalcoin = totalcoin + 100;
        printf("Amount depositied so far: $ %d\n", totalcoin);
    if (coin != 5 || coin !=10 || coin !=25 || coin !=50 || coin !=100)
        printf("Invalid Coin\n");
printf("Dispensing soda ...\n");
printf("Your change is: ");
if (totalcoin > 50)
    totalcoin -= 50;
    printf("Half dollar");
if (totalcoin >= 25)
    totalcoin -= 25;
    printf(" quarter");
if (totalcoin >= 10)
    totalcoin -= 10;
    printf(" dime");
if (totalcoin >= 5)
    totalcoin -= 5;
    printf(" nickel");
Was it helpful?

Solution

if ( coin == 5)
    totalcoin = totalcoin + 5;
    printf("Amount depositied so far: $ %d\n", totalcoin);

The indentation does not show what it really does. It's actually equivalent to:

if ( coin == 5)
{
    totalcoin = totalcoin + 5;
}
printf("Amount depositied so far: $ %d\n", totalcoin);

which is not what you mean, you should use a block:

if ( coin == 5)
{
    totalcoin = totalcoin + 5;
    printf("Amount depositied so far: $ %d\n", totalcoin);
}

As a best practice, some people choose to always use a block after if, for, while, etc, even if there's only one statement in the block. It's less error prone, especially when you need to add more statements to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top