0

I'm stuck trying to understand what's wrong with my code... When I check it I get this error message: :( input of 1.6 yields output of 7 expected exit code 0, not 1

But getting 7 coins for 1.6$ seems correct to me...Can anybody help? Thanks in advance!

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

int main(void)

{
//getting a float type variable from the user and defining integer variables for later use
   float change;
   int q, d, n, p, cents;
// prompting user to only give me positive values by using a do-while loop
   do
   {
       change = get_float("Change owed: \n");
   }
   while (change <=0);


//expressing value in cents to avoid problems with decimals

    cents = round(change * 100);

//calculating number of coins for each value in order from highest to lowest
    q = cents / 25;
    cents = cents % 25;

    d = cents / 10;
    cents = cents % 10;

    n = cents / 5;
    cents = cents % 5;

    p = cents;

//calculating and then printing total number of coins
    int c = q + d + n + p;
    printf("%i\n", c);

    return change;
}
1

The problem is this line: return change; For the main() function you're supposed to return 1 if there's been an error, or 0 if succeed.

| improve this answer | |
1

You need to read the report more carefully. Yes, 7 coins for $1.60 would be correct. But the problem is the return value that the program is sending.

The spec says to print the number of coins, not to return it when the program completes. The normal practice is that a program should return 0; when a program completes successfully, and to return a non-zero value as an error code when something goes wrong.

The code is returning the number of coins after printing that value, not 0. That's why the test is failing (assuming the number of coins is correct.)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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