I want to be able to take someones amount owed as the price, and then do some math from the amount received and print my result.
Below is the code I came up with. However, my program does not run after showing the amount tendered.
Any thoughts?
Note, it is my first time coding in C, and I'm from Java..
#include <stdio.h>
int main (void) {
double tendered;
double changeDue;
double price;
int hundred=0;
int twenty=0;
int ten=0;
int five=0;
int toonoe=0;
int loonie=0;
int quarter=0;
int dime=0;
int nickle=0;
int penny=0;
/* Statements to be executed */
printf("Total purchase price and tendered amount");
scanf("%lf%lf", &price, &tendered);
printf("The sum of %lf and %lf is ", tendered,price);
changeDue=tendered-price;
while(changeDue!=0.00){
if(changeDue<=100.00){
changeDue=changeDue-100.00;
hundred=hundred+1;
}
if(changeDue<=20.00){
changeDue=changeDue-20.00;
twenty=twenty+1;
}
if(changeDue<=10){
changeDue=changeDue-10.00;
ten=ten+1;
}
if(changeDue<=5){
changeDue=changeDue-5.00;
five=five+1;
}
if(changeDue<=2){
changeDue=changeDue-2.00;
toonoe=toonoe+1;
}
if(changeDue<=1){
changeDue=changeDue-1.00;
loonie=loonie+1;
}
if(changeDue>1){
for(int i=0;i<changeDue;i++){
if(i==0.25&&changeDue>=0.25){
changeDue=changeDue-0.25;
quarter=quarter+1;
}
if(i==0.10&&changeDue>=0.10){
changeDue=changeDue-0.10;
dime=dime+1;
}
if(i==0.05&&changeDue>=0.05){
changeDue=changeDue-0.05;
nickle=nickle+1;
}
if(i==0.01&&changeDue<0.05){
changeDue=changeDue-0.01;
penny=penny+1;
}
}
}
}
if(hundred!=0){
printf("%d hundred$ bills given as change",hundred);
}
if(twenty!=0){
printf("%d twenty$ bills given as change",twenty);
}
if(ten!=0){
printf("%d ten$ bills given as change",ten);
}
if(five!=0){
printf("%d five$ bills given as change",five);
}
if(toonoe!=0){
printf("%d toonie coins given as change",toonoe);
}
if(loonie!=0){
printf("%d loonie coins given as change",loonie);
}
if(quarter!=0){
printf("%d quarter coins given as change",quarter);
}
if(dime!=0){
printf("%d dime coins given as change",dime);
}
if(nickle!=0){
printf("%d nicke coins given as change",nickle);
}
if(penny!=0){
printf("%d penny coins given as change",penny);
}
return 0;
}
I have an alternative version of that code, which changes the first part of scanning and printing to
/* identical to start of first version ... */
/* Statements to be executed */
printf("Total purchase price");
scanf("%d", &price);
printf("Enter amount recieved by customer ");
scanf("%d", &tendered);
printf("%d", &tendered);
printf("%d",&tendered);
changeDue=tendered-price;
/* identical to end of first version ... */
And I have a third version, where first scanning and printing is like this.
/* identical to start of first version ... */
float tendered;
float changeDue;
float price;
int hundred=0;
int twenty=0;
int ten=0;
int five=0;
float toonoe=0;
float loonie=0;
float quarter=0;
float dime=0;
float nickle=0;
float penny=0;
/* Statements to be executed */
printf("Total purchase price");
scanf("%f", &price);
printf("Enter amount recieved by customer ");
scanf("%f", &tendered);
printf("%f tendered", tendered);
changeDue=tendered-price;
/* identical to end of first version ... */
You have the problem of comparing a floating point value for identity
(or for not being identical, same problem) here:
See here for some background:
Is floating point math broken?
It creates an endless loop (at least if you are not "lucky"), which prevents all further printing. (Actually "lucky" is not a good description for hiding a bug...)
In order to verify this diagnose, insert a printf at the start of the loop.
You will see many more of that debug output line than you expect.
In order to solve that endless-loop problem, change to
and the endless loop is solved, which currently prevents anything visible happen after printing the due amount.
This does not necessarily fix all the problems in your code, but the most prominent one described in your question is solved.
Note that one of the comments recommends to use
int
for currency.I normally agree, but I have accepted your statement that you have to use float/double.
By the way, use the first version of the code.
In the second version you are printing the address of something instead of the value. I.e. the
&
is wrong here:In the third version your types and the format string in printf do not match.