1

So I'm trying to make a code for one of my classes, and for some reason when I try to compile it, it simply won't accept the subtraction sections. Does anyone know what's going on?

Here's the code:

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

int main(void) {
    float value;
    
    do {
        value = get_float("enter value: ");
    } while (value >= 0);

    value * 100;
    
    int quarter = 25;
    int dime = 10;
    int nickel = 5;
    int penny = 1;

    if (value >= quarter) {
        while (value >= quarter) {
            quarter - value;
        }
        int quarters = 0;
        quarters++;
    } else {
        while (value >= dime) {
            dime - value;
        }
        int dimes = 0;
        dimes++;
    } else {
        while (value >= nickel) {
            nickel - value;
        }
        int nickels = 0;
        nickels++;
    } else {
        while (value >= penny) {
            penny - value;
        }
        int pennies = 0;
        pennies++;
    }
    printf ("%i pennies, %i nickels, %i dimes, %i quarters\n", pennies, nickels, dimes, quarters);
}

and whenever I try to compile it, it says cash.c:25:20: error: expression result unused [-Werror,-Wunused-value] quarter - value; or any of the other math expressions. I haven't been able to compile it so, if this doesn't work, I would like a heads up. thanks, guys.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • What is `get_float()`? And, assuming it reads a `float` from the user, what is your user input here? What is your expected output and what is your actual output? – scohe001 Jan 15 '21 at 20:15
  • What do you think this does `quarter - value;`? I'm guessing you meant to subtract value from quarter, but that would be `quarter -= value;`. – john Jan 15 '21 at 20:20
  • You need to put the subtraction somewhere where a rvalue is allowed, e.g. on the right hand side of an assignment: `value = value - quarter;` (or shorter `value -= quarter;`) – fabian Jan 15 '21 at 20:21
  • Same error here `value*100;` which should be `value *= 100;`. Need to learn how to assign to variables. – john Jan 15 '21 at 20:22
  • so instead of simply subtracting, I need to add the equal sign? – trollergutz Jan 15 '21 at 20:22
  • @trollergutz_playz If you want to change the quarter variable you need to use `-=`. – john Jan 15 '21 at 20:23
  • @trollergutz_playz Actaully maybe you need to do `value -= quarter;` The variable you are subtracting from should go on the lhs of `-=`. – john Jan 15 '21 at 20:24
  • @trollergutz_playz Also all these decalrations are misplced `int nickels = 0; nickels++;`. The way you have written it that variable only exists in the while loop, which isn;t what you wanted. – john Jan 15 '21 at 20:26
  • oh ok. I also seem to have a problem with the else statements. It's expecting something else. here's the problem `cash.c:39:5: error: expected expression 'else'` – trollergutz Jan 15 '21 at 20:26
  • @trollergutz_playz Yep that's wrong, but I can';t work out what you wanted instead. It should be clear that you cannot have multiple elses for only one if. – john Jan 15 '21 at 20:26
  • so I need to have multiple if statements? – trollergutz Jan 15 '21 at 20:27
  • @trollergutz_playz Looking more closely I can't see why you have any if statements – john Jan 15 '21 at 20:28
  • Your error rate seems to be around one serious error every three or so lines, that's a clear indication that you've written far too much code with no testing. It's hard to fix code when it has multiple errors. You problems are only going to get worse when you get the code compiled because then you will hit the logic errors and they're harder to figure out. – john Jan 15 '21 at 20:29
  • what do you mean? Am I supposed to have them or is there an easier way? – trollergutz Jan 15 '21 at 20:29
  • What you should do is start again, and this time only write four or five lines of code, and get those lines working before you write another four or five lines of code. That is the best way to make progress. You're heading for a whole heap of pain at the momemt. – john Jan 15 '21 at 20:30
  • Unrelated: `value = get_float ("enter value: ");` isn't user friendly since you'll reject all values that are not negative. Make the prompt "_`enter a negative value:`_". – Ted Lyngmo Jan 15 '21 at 20:31
  • @trollergutz_playz Well it's your code, but I can only see a need for while loops, not if statements. – john Jan 15 '21 at 20:31
  • ok, thanks man. I'll try to stick to that. – trollergutz Jan 15 '21 at 20:31
  • @TedLyngmo what im trying to do is find out how much change will be given out with a quantity of money. why would I want it to be negative? – trollergutz Jan 15 '21 at 20:33
  • @trollergutz_playz Your condition `while(value >= 0);` says that you want it to be negative. – Ted Lyngmo Jan 15 '21 at 20:33
  • 1
    @trollergutz_playz See what I mean about writing a few lines of code and getting those working before you go any further? the logic of your very first do while loop is the wrong way round (as Ted says). – john Jan 15 '21 at 20:34
  • 1
    oh, didn't notice that. thanks man. – trollergutz Jan 15 '21 at 20:35
  • 1
    well, thanks for the help everyone, I guess I'll just start over. see ya. – trollergutz Jan 15 '21 at 20:36
  • Keep the code in a backup file somewhere until you're done rewriting. You may still be able to use portions of it. – user4581301 Jan 15 '21 at 20:42
  • 1
    BTW don't work with `float` but integer, for example `int cents = (int)round(value*100);` Please read [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Weather Vane Jan 15 '21 at 20:57
  • @WeatherVane Yes! I'd like to get a `std::ratio`-like support into the library to aid currency calculations, like `chrono` uses the compile time `std::ratio`. ... and metric to non-metric scales etc. It should be obvious that one looses precision by forcing a cast. Wonderful! – Ted Lyngmo Jan 15 '21 at 23:31
  • 1
    @TedLyngmo the cast is specifically to silence the compiler (againt all wisdom) and the precision that is *necessary* is retained by the multiplication and rounding. By losing the `float`'s inexactness we are *gaining* precision. – Weather Vane Jan 16 '21 at 09:29

1 Answers1

1

There are multiple issues in you code:

  • the do / while loop continues as long as the entered value is >= 0. This is the exact opposite of what you want. You should continue if the value is negative.

  • the statements value * 100; and dime - value;... do not have any side effects, as diagnosed by the compiler. You should write value *= 100; or value = value * 100; etc.

  • multiplying the float value by 100 might not produce an integer because of the limited precision of the floating type and its inability to represent multiples of 0.01 exactly. You should round the value to the nearest integer with value = round(value * 100);

  • the series of else clauses prevent proper computation of the change to give. As coded, you can only give one kind of coin, and only one of that.

  • the variables quarters, dimes, nickels and pennies are defined with a local scope that ends before the final printf statement, so they are undefined there and the compiler produces an error.

You can compute the number of quarters with int quarters = floor(value / 25); etc.

Here is a modified version:

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

int main(void) {
    float value;
    
    do {
        value = get_float("enter value: ");
    } while (value < 0);

    value = round(value * 100);
    
    int quarters = floor(value / 25);
    value -= 25 * quarters;
    int dimes = floor(value / 10);
    value -= 10 * dimes;
    int nickels = floor(value / 5);
    value -= 5 * nickels;
    int pennies = value;

    printf ("%i pennies, %i nickels, %i dimes, %i quarters\n", pennies, nickels, dimes, quarters);
    return 0;
}

Using integer arithmetic to split value into coins allows for simpler code, using the modulo operator %:

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

int main(void) {
    float value;
    
    do {
        value = get_float("enter value: ");
    } while (value < 0);

    int cents = round(value * 100);
    
    int quarters = cents / 25;  cents %= 25;
    int dimes    = cents / 10;  cents %= 10;
    int nickels  = cents / 5;   cents %= 5;
    int pennies  = cents;

    printf ("%i pennies, %i nickels, %i dimes, %i quarters\n", pennies, nickels, dimes, quarters);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189