Noni A. Noni A. - 3 months ago 14
C Question

Need to return a value from a do/while and multiple while loops - C

I am trying to build a simple change calculator of sorts. The user inputs an amount of change owed, and then they press return. The value they entered is supposed to be multiplied by 100 first (so that when we round it, the digits are not truncated). The rounding is supposed to turn the float into an int, and then all the math operations (the while loops) should execute, with a single number printing out at end that represents how many coins they were given (i.e. how many quarters, dimes, etc...) The code compiles fine, it prompts the user to input a value, but then when you press return, nothing is executed, and the command line goes back to being blank.

Any ideas what I'm doing wrong? My guess is the values in the while loops are not getting transferred out of the loop, so they can be used in the next loop. But then I am a very early beginner in C language, and not sure the correct rules for the loops. I tried looking up while loop examples, but nothing really explains exactly how to get a value to carry from one while loop to another. If, in fact, that is the problem. Thanks for your help.

CODE REVISION:

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

int main (void) {

float change;
int cents;

int quarter_count = 0;
int dime_count = 0;
int nickel_count = 0;
int pennies = 0;
int total_count;

do
{
printf("Enter the amount of change you are owed: ");
change = GetFloat();
cents = round(change * 100);
}
while (change < 0);
return cents;

int quarter = 25;
while (cents >= quarter)
{
cents = cents - quarter;
quarter_count++;
}
return cents;

int dime = 10;
while (cents >= dime)
{
cents = cents - dime;
dime_count++;
}
return cents;

int nickel = 5;
while (cents >= nickel)
{
pennies = cents - nickel;
nickel_count++;
}
return pennies;

total_count = quarter_count + dime_count + nickel_count + pennies;

printf("%d\n", total_count);

}

Answer

After every while loop you put return statement so. Code after first while..loop meaning less.

Also for third while..loop you put condition like while (cents >= nickel) but in while loop you dont alter value of cents.So it will in infinine loop if cent value remain greater then when it reach upto third while..loop.

See my updated code.It will do basic functionality which may be you want

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

int main (void) {

    float change;
    int cents;

    int quarter_count = 0;
    int dime_count = 0;
    int nickel_count = 0;
    int pennies = 0;
    int total_count;

    do
    {
        printf("Enter the amount of change you are owed: ");
        change = GetFloat();
        cents = round(change * 100); 
    }
    while (change < 0);


    int quarter = 25;

    while (cents >= quarter)
    {
        cents = cents - quarter;
        quarter_count++;
    }

     if (cents <= 0)
      goto done;

    int dime = 10;
    while (cents >= dime)
    {
        cents = cents - dime;
        dime_count++;
    }

   if (cents <= 0)
      goto done;

    int nickel = 5;

    pennies=cents;

    while (pennies >= nickel)
    {
        pennies = pennies - nickel;
        nickel_count++;
    }

done:
    total_count = quarter_count + dime_count + nickel_count + pennies;

    printf("%d\n", total_count);

    return 0;

}