Hi again,
Your code does look good for starters, though there is much to be changed. For instance:
Code:
void makechange(float cost, float payment, float *change,
int *twentiesInTill, int *tensInTill, int *fivesInTill
int *dollarsInTill, int *quartersInTill, int *dimesInTill
int *nickelsInTill, int *penniesInTill, int *twentiesInChange,
int *tensInChange, int *fivesInChange, int *dollarsInChange,
int *nickelsInChange, int *penniesInChange);
That in itself can be remedied into something simple.
I will display my revised code step by step to help you learn. Here comes the local variables. These are declared before doing any work:
Code:
int main() {
// The file
FILE *transactions;
// The numbers
float cost, payment, change;
// The remainder in depth
int penniesInTill = 0, nickelsInTill = 0, dimesInTill = 0, quartersInTill = 0;
int dollarsInTill = 0, fivesInTill = 0, tensInTill = 0, twentiesInTill = 0;
Quite simple. We declare our file called transactions, then we make three floating-point variables. This is to ensure we get precision. Like decimal points etc... Then we make our 8 integers to calculate how many quaters, tens, etc... All of these are initialized to 0 for a reason. Take an unintialized variable and add one to it, it wont equal 1.
Next part:
Code:
transactions = fopen ("transactions.dat", "r");
if (transactions == NULL) {
printf ("File, transactions, could not be opened\n");
}
Nothing major here. You did just fine
Next:
Code:
fscanf(transactions, "%f\n", &cost);
printf ("The cost is %g\n", cost);
fscanf(transactions, "%f\n", &payment);
printf("The customer's payment was %g\n", payment);
change = cost - payment;
printf("The change due is %g\n", change);
This scans the file for a number. That is why we have "%f\n" so the next time we scan we look at the next line. \n means new line. Here we do something close to:
Scan the file for cost
Print cost
Scan the file for payment
Print payment
Equate difference [change]
Print difference [change]
We know change is a float since we are taking the difference of two floats.
Next:
Code:
while (change >= 20.0f) {
change -= 20.0f;
twentiesInTill++;
}
while (change >= 10.0f) {
change -= 10.0f;
tensInTill++;
}
while (change >= 5.0f) {
change -= 5.0f;
fivesInTill++;
}
while (change >= 1.0f) {
change -= 1.0f;
dollarsInTill++;
}
while (change >= 0.25f) {
change -= 0.25f;
quartersInTill++;
}
while (change >= 0.10f) {
change -= 0.10f;
dimesInTill++;
}
while (change >= 0.05f) {
change -= 0.05f;
nickelsInTill++;
}
while (change >= 0.01f) {
change -= 0.01f;
penniesInTill++;
}
This has been modified. On the old code you had "if change > 1) change -= 5;" That personall would have taken your program off in the wrong direction to calculate. Also, instead of doing ">", it would be better to do ">=" because if we run into 0.01 at the end, it wont take that as a penny because 0.01 is not greater than 0.01. Though 0.01 is greater than or equal to 0.01.
Next, and Lastly:
Code:
printf("In Detail\n\nTwenties: %d\nTens: %d\nFives: %d\nOnes: %d\nQuarters: %d\nDimes: %d\nNickels: %d\nPennies: %d\n",
twentiesInTill, tensInTill, fivesInTill, dollarsInTill, quartersInTill, dimesInTill, nickelsInTill, penniesInTill);
return 0;
}
That is how your code ends. It should compile as long as you include stdio.
Also, my fscanf() calls look into a file and look for:Just for example. Since 83.45 will be the cost, and 75.20 is what you paid.
If you have questions please feel free to ask.
- Stack Overflow