Reputation: 7
I was just wondering what I did wrong here, because when the program is executed, it runs just fine. I'm a newbie at programming and am very lost. Any advise would be great!
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
printf("Days in Month:\n");
int days = GetInt();
for (; days < 28 || days > 31; days = GetInt())
{
printf("No month has that amount of days! Please try again.\n");
}
printf("Pennies on first day:\n");
int pennies = GetInt();
for (; pennies < 0; pennies = GetInt())
{
printf("You can't have negative pennies! Please try again.\n");
}
if (pennies == 1)
printf("$%.2f",pow ((pennies + 1), (days - 1)));
else
printf("$%.2f",pow (pennies, days));
}
Check50 yields:
~/workspace/chapter1/ $ check50 1617.chapter1.pennies pennies.c
:) pennies.c exists
:) pennies.c compiles
:( 28 days, 1 penny on first day
\ expected output, but not "$134217728.00"
:( 31 days, 1 penny on first day
\ expected output, but not "$1073741824.00"
:( 29 days, 2 pennies on first day
\ expected output, but not "$536870912.00"
:( 30 days, 30 pennies on first day
\ expected output, but not "$20589113209464899002378237447530552256..."
:) rejects days not 28, 29, 30, or 31
:( rejects non-positive pennies
\ expected prompt for input, not exit code of 0
:) rejects a non-numeric days input of "foo"
:) rejects a non-numeric penny input of "foo"
:) rejects a non-numeric days input of ""
:) rejects a non-numeric penny input of ""
As you can see it says that it is accepting negative inputs and is not yielding the correct amount of money. Is this a bug in check50, or did I do something wrong?
Upvotes: 0
Views: 836
Reputation: 165318
Assuming it's this assignment, your math is wrong.
The assignment is what happens if your initial amount of pennies doubles every day for a month. If you start with 5 pennies, that means 5 then 10 then 20 then 40... 5 * 2 * 2 * 2 ... or 5 * 2N where N
is the number of days.
But you have pow(pennies, days)
. If pennies = 5
and days = 30
that's 530 which is something entirely different and very large.
What you need is pennies * pow(2, days)
. And that's the number of pennies, so divide by 100 to get the dollar amount.
This isn't 100% correct, but since this is an exercise I leave the rest of the details up to you.
As to the "exit 0" problem, it's this:
for (; pennies < 0; pennies = GetInt())
{
printf("You can't have negative pennies! Please try again.\n");
}
You're allowing 0 pennies. You're supposed to reject that.
If the user does not input a positive integer for the first day’s number of pennies, the program should prompt the user to retry.
As to why it says it didn't get a prompt, it gave you 0 pennies and expected to be prompted again. Whoever wrote the checking code didn't think to see if it got output.
Upvotes: 1