1

I tried to run this program but I cannot understand why my counter doesn't seem to increase. How I change it to make it work?

#include <stdio.h>

int main(void) {
    int amt, i = 0, j = 0, k = 0, l = 0;
    printf("Enter a dollar amount : ");
    scanf("%d", &amt);
    
    while (amt >= 20) // this is wrong idk why 
        amt -= 20;
        i = i + 1;
    while (amt >= 10 && amt <= 20)
        amt -= 10;
        j++;
    while (amt >= 5 && amt <= 10)
        amt -= 5;
        k++;
    while (amt > 0 && amt < 5)
        amt -= 1;
        l++;

    printf("$20 bills : %d\n", i);
    printf("$10 bills : %d\n", j);
    printf("$5 bills : %d\n", k);
    printf("$1 bills : %d\n", l);

    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
John Lim
  • 81
  • 5
  • 1
    Warnings. Use them. See [here](https://ideone.com/BjVjtA). See also [here](https://stackoverflow.com/questions/57842756). – n. m. could be an AI Nov 27 '20 at 08:34
  • 2
    Use braces after while loop and enclose the codes inside the block. Your counter like i++ is not place inside the block of while loop, so use curly braces and place the code inside. – nsky80 Nov 27 '20 at 08:37

3 Answers3

1

In your code

while (amt >= 20) // this is wrong idk why 
    amt -= 20;
    i = i + 1;

this seems to be wrong, because, you do not have an associated block to encompass all the statement you wish to execute as loop body. In your case, the code is same as

while (amt >= 20) {// this is wrong idk why 
    amt -= 20;
 }
 i = i + 1;

You need to enclose all the statements which you want to execute as loop body in a block, like

while (amt >= 20) {
    amt -= 20;
    i = i + 1;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

The problems lies here:

// What you see:       // What the compiler sees:

while (amt >= 20)      while (amt >= 20)
    amt -= 20;             amt -= 20;
    i = i + 1;         i = i + 1;

The result of that code will be to set i to one (assuming it's originally zero, which it is in your case) no matter what value is in amt.

By default, the body of a while (or if or for ...) is a single statement. You can give it a group of statements but you do that by enclosing them in braces:

while (amt >= 20) {
    amt -= 20;
    i = i + 1;
}

In addition, you upper bound checks are not necessary here. For example, the only way to reach:

while (amt >= 10 && amt <= 20)

is if amt is already less than 20. Otherwise it never would have left the previous loop. So you code can probably be better written as:

#include <stdio.h>

int main (void) {
    int amt, twenties = 0, tens = 0, fives = 0, ones = 0;
    printf("Enter a dollar amount : ");
    if (scanf("%d" , &amt) != 1) {
        fprintf(stderr, "Invalid input\n");
        return 1;
    }
    if (amt < 0) {
        fprintf(stderr, "Negative input\n");
        return 1;
    }
    
    while (amt >= 20) { amt -= 20; twenties++; }
    while (amt >= 10) { amt -= 10; tens++; }
    while (amt >=  5) { amt -=  5; fives++; }
    ones = amt;

    printf("$20 bills : %d\n", twenties);
    printf("$10 bills : %d\n", tens);
    printf(" $5 bills : %d\n", fives);
    printf(" $1 bills : %d\n", ones);

    return 0;
}

You'll notice some other changes such as more descriptive variable names, formatting, user input checking, and keeping code small. These are not technically necessary but they are the result of a great many years of experience in writing (and reading code).


As an aside, you can also do this without repeated subtraction, by using integer division and remainders:

#include <stdio.h>

int main (void) {
    int amt;
    printf("Enter a dollar amount : ");
    if (scanf("%d" , &amt) != 1) {
        fprintf(stderr, "Invalid input\n");
        return 1;
    }
    if (amt < 0) {
        fprintf(stderr, "Negative input\n");
        return 1;
    }

    int twenties = amt / 20; amt = amt % 20;
    int tens     = amt / 10; amt = amt % 10;
    int fives    = amt /  5; amt = amt %  5;
    int ones     = amt;

    printf("$20 bills : %d\n", twenties);
    printf("$10 bills : %d\n", tens);
    printf(" $5 bills : %d\n", fives);
    printf(" $1 bills : %d\n", ones);

    return 0;
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Unlike python, C does not infer program structure from indentation: your while statements only include the first subsequent statement, leaving i = i + 1; outside the while statement body and executing once, unconditionally.

Here is a corrected version:

#include <stdio.h>

int main(void) {
    int amt, i = 0, j = 0, k = 0, l = 0;

    printf("Enter a dollar amount : ");
    if (scanf("%d", &amt) != 1 || amt < 0) {
        printf("invalid input\n");
        return 1;
    }
    
    while (amt >= 20) {
        amt -= 20;
        i = i + 1;
    }
    while (amt >= 10) {
        amt -= 10;
        j++;
    }
    while (amt >= 5) {
        amt -= 5;
        k++;
    }
    while (amt > 0) {
        amt -= 1;
        l++;
    }
    printf("$20 bills : %d\n", i);
    printf("$10 bills : %d\n", j);
    printf("$5 bills : %d\n", k);
    printf("$1 bills : %d\n", l);

    return 0;
}

Here is a simpler version:

#include <stdio.h>

int main(void) {
    int amt;

    printf("Enter a dollar amount : ");
    if (scanf("%d", &amt) != 1 || amt < 0) {
        printf("invalid input\n");
        return 1;
    }

    printf("$20 bills : %d\n", amt / 20);
    printf("$10 bills : %d\n", amt % 20 / 10);
    printf(" $5 bills : %d\n", amt % 10 / 5);
    printf(" $1 bills : %d\n", amt % 5);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189