This is the code that I am trying to test.
I have tried multiple times to change the type of loop that I used or to even change where I placed it (either in the "int get_cents" function or where it is now).
Could it be that because I am using a do while loop - the code doesn't initially reject the negative inputs and that is the reason why when I run check50 - I get this result:
running ./cash_test 0... sending input -10... checking that input was rejected...
#include <cs50.h>
#include <stdio.h>
int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);
int main(void)
{
// Ask how many cents the customer is owed
int cents;
do
{
cents = get_cents();
}
while (cents < 1);
{
// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;
// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;
// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;
// Sum coins
int coins = quarters dimes nickels pennies;
// Print total number of coins to give the customer
printf("%i\n", coins);
}
}
int get_cents(void)
{
// TODO
return get_int("how many cents? ");
}
int calculate_quarters(int cents)
{
// TODO
return cents/25;
}
int calculate_dimes(int cents)
{
// TODO
return cents/10;
}
int calculate_nickels(int cents)
{
// TODO
return cents/5;
}
int calculate_pennies(int cents)
{
// TODO
return cents/1;
}
Why is my code working well only when I test it manually?
CodePudding user response:
From the narrative of the error you get from the checker application, it appears that the requirement of the test is that the program is supposed to immediately indicate that an invalid negative value was entered and the program is supposed to end. Instead of a "while" loop where a retry is allowed, you might try the following code snippet.
// Ask how many cents the customer is owed
int cents;
cents = get_cents();
if (cents < 0)
{
printf("A negative value was entered and rejected\n");
return -1;
}
This would result in an output as follows if a value of "-10" were entered.
@Una:~/C_Programs/Console/cash_test/bin/Release$ ./cash_test
how many cents? -10
A negative value was entered and rejected
@Una:~/C_Programs/Console/cash_test/bin/Release$
You might give that a try.
CodePudding user response:
The get_cents
function here will not reprompt if a negative number is entered because that test is in main. It is explicit in the spec that the reprompt should be within the get_cents
function:
Implement
get_cents
in such a way that the function prompts the user for a number of cents usingget_int
and then returns that number as anint
. If the user inputs a negativeint
, your code should prompt the user again.