简体   繁体   中英

why does my program not accept subtractions?

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM