简体   繁体   中英

This is Cs50 problem set1 cash. I don't know what's wrong with this code can someone help me

This is Cs50 problem set1 cash. I don't know what's wrong with this code can someone help me. I just added the quotes because stackoverflow was not letting me post it. Your program should behave per the examples below.

$ ./cash
Change owed: 0.41
4
#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
    int i = 0;
    int a = 0;
    int b = 0;
    int c = 0;
    float change_owed;
    do
    {
        change_owed = get_float("change owed: ");
    }
    while (change_owed <=0); 

    change_owed=round(change_owed*100);
    printf("%f\n", change_owed);

    if (change_owed>25)
    ;
    {
    while (change_owed>=25);
    ;
    {
        change_owed=change_owed-25;
        i++;
    }
    }
    if (change_owed>=10)
    ;
    {
    while (change_owed>=10);
    ;
    {
        change_owed=change_owed-10;
        a++;
    }
    }
    if (change_owed>=5)
    ;
    {
    while (change_owed>=5;c++;)
    ;
    {
        change_owed=change_owed-5;
    }
    }
    if (change_owed>=1)
    ;
    {
    while(change_owed>=1)
    ;
    {
        change_owed=change_owed-1;
        b++;
    }
    }
    int final = i+a+b+c;
    printf("%d + %d + %d+ %d = %d", i, a, b, c, final);
    printf("\n");"
   
}

}

This is Cs50 problem set1 cash. I don't know what's wrong with this code can someone help me

The program does not crash , it loops for ever in while (change_owed>=25);

There is a fundamental misunderstanding regarding the syntax of basic and control statements. ; ends a statement, including the empty statement. So while (change_owed>=25); is parsed as

    while (change_owed >= 25)
        /* empty statement */;

Since change_owned does not change in the loop body, the loop runs for ever is change_owed was >= 25 at the beginning.

Similarly

if (change_owed>=1)
;

Is parsed as

if (change_owed >= 1)
    /* do nothing */;

which has no effect.

Here is modified version:

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

int main(void) {
    int i = 0;
    int a = 0;
    int b = 0;
    int c = 0;
    float change_owed;
    do {
        change_owed = get_float("change owed: ");
    } while (change_owed <= 0); 

    change_owed = round(change_owed * 100);
    printf("%f\n", change_owed);

    if (change_owed > 25) {
        while (change_owed >= 25) {
            change_owed = change_owed - 25;
            i++;
        }
    }
    if (change_owed >= 10) {
        while (change_owed >= 10) {
            change_owed = change_owed - 10;
            a++;
        }
    }
    if (change_owed >= 5) {
        while (change_owed >= 5) {
            change_owed = change_owed - 5;
            b++;
        }
    }
    if (change_owed >= 1) {
        while (change_owed >= 1) {
            change_owed = change_owed - 1;
            c++;
        }
    }
    int final = i + a + b + c;
    printf("%d + %d + %d+ %d = %d", i, a, b, c, final);
    printf("\n");
    return 0;
}

Note that the if statements are redundant, as well as the last loop, and the code would be more readable with more explicit variable names:

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

int main(void) {
    int quarters = 0;
    int dimes = 0;
    int nickels = 0;
    int pennies = 0;
    float change_owed;
    do {
        change_owed = get_float("change owed: ");
    } while (change_owed <= 0); 

    int cents = round(change_owed * 100);
    printf("%d\n", cents);

    while (cents >= 25) {
        cents -= 25;
        quaters++;
    }
    while (cents >= 10) {
        cents -= 10;
        dimes++;
    }
    while (cents >= 5) {
        cents -= 5;
        nickels++;
    }
    pennies = cents;
    int coins = quarters + dimes + nickels + pennies;
    printf("%d + %d + %d + %d = %d\n", quarters, dimes, nickels, pennies, coins);
    return 0;
}

Finally, instead of loops, you can use integer division:

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

int main(void) {
    float change_owed;
    do {
        change_owed = get_float("change owed: ");
    } while (change_owed <= 0); 

    int cents = round(change_owed * 100);
    printf("%d\n", cents);

    int quarters = cents / 25;
    cents -= quarters * 25;  // or cents %= 25;
    int dimes = cents / 10;
    cents -= dimes * 10;     // or cents %= 10;
    int nickels = cents / 5;
    int pennies = cents % 5;
    int total = quarters + dimes + nickels + pennies;
    printf("%d + %d + %d + %d = %d\n", quarters, dimes, nickels, pennies, total);
    return 0;
}

Or even simpler:

Finally, instead of loops, you can use integer division:

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

int main(void) {
    float change_owed;
    do {
        change_owed = get_float("change owed: ");
    } while (change_owed <= 0); 

    int cents = round(change_owed * 100);
    printf("%d\n", cents);

    int quarters = cents / 25;
    int dimes = cents % 25 / 10;
    int nickels = cents % 25 % 10 / 5;
    int pennies = cents % 5;
    int total = quarters + dimes + nickels + pennies;
    printf("%d + %d + %d + %d = %d\n", quarters, dimes, nickels, pennies, total);
    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