0

the way my code works is to reduce c as we count out coin-types. But I'm getting errors.

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

    int main(void)
    {
    float c;

    do
    {
        float c = get_float("Change: ");
    }
    while (c<.01);

    int ones=0;
    while (c>1.00)
    {
        ones++;
        c - 1.00;
    }
    int quarters=0;
    while (c>0.25)
    {
        quarters++;
        c - 0.25;
    }
    int dimes=0;
    while (c>0.10)
    {
        dimes++;
        c - 0.10;
    }
    int nickels=0;
    while (c>0.05)
    {
        nickels++;
        c - 0.05;
    }
    int pennies=0;
    while (c>0.01)
    {
        nickels++;
        c - 0.01;
    }
    int coins = ones + quarters + dimes + nickels + pennies;
    }

Error:

    cash.c:10:11: error: declaration shadows a local variable [-Werror,-Wshadow]
        float c = get_float("Change: ");
              ^
    cash.c:6:7: note: previous declaration is here
    float c;
          ^
    cash.c:10:11: error: unused variable 'c' [-Werror,-Wunused-variable]
        float c = get_float("Change: ");
              ^
    cash.c:18:7: error: expression result unused [-Werror,-Wunused-value]
        c - 1.00;
        ~ ^ ~~~~

(The last one for every coin value.)

1

About the first error in line 10 , you cant declare a vatiable twice , you can only assign it more than once , so you need to delete the float type declaration which is inside the do while loop.

When you want to reduce a number you need to assign it then substract not just substracting the variable or you could use the increment syntax c -= 1.00 and its equivalent to c = c - 1.00

| improve this answer | |
  • Thanks K. that is helpful! I applied the same reasoning to the ones, quarters, dimes, etc. But my program doesn't return anything -- one reason that has occurred to me is that I declare the variables outside the { } and increment them inside the curly brackets. Maybe they are not incrementing globally? e.g. int pennies=0; while (c>0.01) { pennies = pennies + 1; c = c - 0.01; } int coins = ones + quarters + dimes + nickels + pennies; return coins; } – EB Gold Sep 9 '18 at 22:35
  • You are welcome ! Try using printf to print the result , and you can use it for debugging too. Just as the specification said " Use get_float from the CS50 Library to get the user’s input and printf from the Standard I/O library to output your answer. ". – K. Mamdouh Sep 10 '18 at 3:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .