Skip to main content
added 273 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you're not rounding the amount you get from the user.

    you shouldn't be reading the float like that

    scanf("%.2f\n", &change);
    
  2. you have logical errors in your code.

    you're not rounding the amount you get from the user.

  3. you shouldn't be printing the value of change.

    you have logical errors in your code.

  4. you shouldn't be printing the value of change.

How to fix these?

  1. you should rather be reading the float either like this

    change = GetInt(); // from the cs50 library
    

    or this

    scanf("%f", &change);
    
  2. use the round() function (declared in math.h) to round the amount that you receive from the user.

  3. given the following piece of code

    while (condition)
        printf("hello from inside the loop!\n");
    
    printf("hello from outside the loop!\n");
    

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  4. you should remove all printf() calls that print the value of change.

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you're not rounding the amount you get from the user.
  2. you have logical errors in your code.
  3. you shouldn't be printing the value of change.

How to fix these?

  1. use the round() function (declared in math.h) to round the amount that you receive from the user.

  2. given the following piece of code

    while (condition)
        printf("hello from inside the loop!\n");
    
    printf("hello from outside the loop!\n");
    

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  3. you should remove all printf() calls that print the value of change.

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you shouldn't be reading the float like that

    scanf("%.2f\n", &change);
    
  2. you're not rounding the amount you get from the user.

  3. you have logical errors in your code.

  4. you shouldn't be printing the value of change.

How to fix these?

  1. you should rather be reading the float either like this

    change = GetInt(); // from the cs50 library
    

    or this

    scanf("%f", &change);
    
  2. use the round() function (declared in math.h) to round the amount that you receive from the user.

  3. given the following piece of code

    while (condition)
        printf("hello from inside the loop!\n");
    
    printf("hello from outside the loop!\n");
    

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  4. you should remove all printf() calls that print the value of change.

added 12 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you're not rounding the amount you get from the user.
  2. you have logical errors in your code.
  3. you shouldn't be printing the value of change.

How to fix these?

  1. use the round() function (declared in math.h) to round the amount that you receive from the user.

  2. given the following piece of code

    while (condition) printf("hello from inside the loop!\n");

    printf("hello from outside the loop!\n");

    while (condition)
        printf("hello from inside the loop!\n");
    
    printf("hello from outside the loop!\n");
    

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  3. you should remove all printf() calls that print the value of change.

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you're not rounding the amount you get from the user.
  2. you have logical errors in your code.
  3. you shouldn't be printing the value of change.

How to fix these?

  1. use the round() function (declared in math.h) to round the amount that you receive from the user.

  2. given the following piece of code

    while (condition) printf("hello from inside the loop!\n");

    printf("hello from outside the loop!\n");

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  3. you should remove all printf() calls that print the value of change.

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you're not rounding the amount you get from the user.
  2. you have logical errors in your code.
  3. you shouldn't be printing the value of change.

How to fix these?

  1. use the round() function (declared in math.h) to round the amount that you receive from the user.

  2. given the following piece of code

    while (condition)
        printf("hello from inside the loop!\n");
    
    printf("hello from outside the loop!\n");
    

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  3. you should remove all printf() calls that print the value of change.

added 1198 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you're not rounding the amount you get from the user.
  2. you have logical errors in your code.
  3. you shouldn't be printing the value of change.

How to fix these?

  1. use the round() function (declared in math.h) to round the amount that you receive from the user.

  2. given the following piece of code

    while (condition) printf("hello from inside the loop!\n");

    printf("hello from outside the loop!\n");

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  3. you should remove all printf() calls that print the value of change.

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you're not rounding the amount you get from the user.
  2. you have logical errors in your code.
  3. you shouldn't be printing the value of change.

How to fix these?

  1. use the round() function (declared in math.h) to round the amount that you receive from the user.

  2. given the following piece of code

    while (condition) printf("hello from inside the loop!\n");

    printf("hello from outside the loop!\n");

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  3. you should remove all printf() calls that print the value of change.

Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100
Loading