I can't seem to figure out whats wrong with my code, but I'm receiving incorrect values for simple inputs like 1 or 2 but correct inputs for .41. If someone could help me out It'd be greatly appreciated!
This is my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
//Establish Variables
float amount_owed;
int c = 0;
//Get Valid Input from User
do
{
amount_owed = get_float ("Change Owed: ");
} while (amount_owed <= 0);
//Check for quarters, mark # of quarters that can be used, subtract value from original amount_owed
do
{
(c++);
(amount_owed = amount_owed - .25);
} while (amount_owed >= .25);
//Check for dimes, mark # of dimes that can be used, subtract value from original amount_owed
do
{
(c++);
(amount_owed = amount_owed - .1);
} while ((amount_owed >= .1) && (amount_owed < .25));
//Check for Nickels, mark $ of nickels that can be used, subtract value from original amount_owed
do
{
(c++);
(amount_owed = amount_owed - .05);
} while ((amount_owed >= .05) && (amount_owed < .1));
//Check for Pennies, mark # of pennis that can be used, subtract value from original amount_owed
do
{
(c++);
(amount_owed = amount_owed - .01);
} while ((amount_owed >= .01) && (amount_owed < .05));
//Print Number of Minimum number of coins that can be used
{
if (amount_owed == 0)
;
printf("%d\n", c);
}
}
转载于:https://stackoverflow.com/questions/53147814/cash-c-expected-18-n-not-22-n
To start with you should never use float for somethin that need to be accurate. But let's get back to that later as there is another issue with your program.
For now just assume that float is actually precise.
When you write:
do
{
c++;
...
} while(...);
the code in the body will always be executed at least once.
So with your code if the input is 1.0
the first loop will be executed 4 times and amount_owed
will be 0.0
.
When for the next 3 do-while
you will still go into the body once and do the c++
(even though amount_owed
is zero). Consequently you result will be 7 instead of 4 (that 4 from the first loop + 1 from each of the three following loop).
The solution is to use regular while
instead of do-while
. Like:
#include <stdio.h>
int main(void) {
float amount_owed = 1.0;
int c = 0;
while (amount_owed >= 0.25)
{
c++;
amount_owed = amount_owed - 0.25;
}
while ((amount_owed - 0.1) >= 0)
{
c++;
amount_owed = amount_owed - 0.1;
}
while (amount_owed >= .05)
{
c++;
amount_owed = amount_owed - .05;
}
while (amount_owed >= .01)
{
c++;
amount_owed = amount_owed - .01;
}
printf("%d\n", c);
return 0;
}
Back to using float : floats can't represent every number 100% accurate. So when doing calculations involving floats, you will likely see some rounding errors. So for any calculation that need an accurate result, you should try to do it using integers.
For a task like this the "trick" is to consider amount_owed
as being in units of the lowest coins you have. Typically that means 100 times the "normal" way of thinking about it. For instance, instead of 1.17
you use 117
.
Then your code could be more like:
#include <stdio.h>
int main(void) {
unsigned amount_owed = 100; // note 100 instead of 1.0
int c = 0;
while (amount_owed >= 25) // note 25 instead of .25
{
c++;
amount_owed = amount_owed - 25;
}
while ((amount_owed - 10) >= 0)
{
c++;
amount_owed = amount_owed - 10;
}
. . .