0
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void) {
    double tendered;
    double changeDue;
    double price;
    int hundred;
    int fivty=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;
    double zeroValue=0.001;

 /* Statements to be executed */
 printf("Total purchase price and tendered amount\n");
scanf("%lf %lf ", &price, &tendered);
 printf(" %lf and %lf is \n", tendered,price);
 changeDue=tendered-price;
 printf("%lf \n", changeDue);

 if(tendered<price){
     printf("Not enough money recieved as payment \n");
 }

 if(tendered==price){
     printf("Exact payment, no change given \n");
 }

 if(tendered>price){
     printf("%lf Amount to be paid is ", changeDue);
 }


 while(changeDue<zeroValue){
     if(changeDue>=100.00){
         changeDue=changeDue-100.00;
         hundred=hundred+1;
     }
     if((changeDue>=50.00)&&(changeDue<100)){
         changeDue=changeDue-20.00;
         twenty=twenty+1;
     }

     if((changeDue>=20.00)&&(changeDue<50)){
         changeDue=changeDue-20.00;
         twenty=twenty+1;
     }
     if((changeDue>=10)&&(changeDue<20)){
         changeDue=changeDue-10.00;
         ten=ten+1;
     }
     if((changeDue>=5)&&(changeDue<10)){
         changeDue=changeDue-5.00;
         five=five+1;
     }
     if((changeDue>=2)&&(changeDue<5)){
         changeDue=changeDue-2.00;
         toonoe=toonoe+1;
     }
      if
      ((changeDue>=1)&&(changeDue<2)){
         changeDue=changeDue-1.00;
         loonie=loonie+1;
     }

      if((changeDue>=0.25)&&(changeDue<1)){
               changeDue=changeDue-0.25;
               quarter=quarter+1;
            }
     if((changeDue>=0.10)&&(changeDue<0.25)){
               changeDue=changeDue-0.10;
               dime=dime+1;
     }
      if((changeDue>=0.05)&&(changeDue<0.05)){
              changeDue=changeDue-0.05;
               nickle=nickle+1;
     }
      if((changeDue>=0.01)&&(changeDue<0.05)){
             changeDue=changeDue-0.01;
               penny=penny+1;
     }
         if((changeDue==0)||(changeDue==0.000000)){
        break;
     }

        }


 if(hundred!=0){
     printf("%d hundred$ bills given as change \n",hundred);
 }
  if(twenty!=0){
       printf("%d twenty$ bills given as change \n",twenty);
 }
  if(ten!=0){
     printf("%d ten$ bills given as change \n",ten);
 }
  if(five!=0){
     printf("%d five$ bills given as change \n",five);
 } 
 if(toonoe!=0){
       printf("%d toonie coins given as change \n",toonoe);
 }
  if(loonie!=0){
       printf("%d loonie coins given as change \n",loonie);
 }
  if(quarter!=0){
      printf("%d quarter coins given as change \n",quarter);
 }
  if(dime!=0){
      printf("%d dime coins given as change \n",dime);
 }
  if(nickle!=0){
       printf("%d nicke coins given as change \n",nickle);
 }
  if(penny!=0){
       printf("%d penny coins given as change \n",penny);
 }

 return 0;
}

I have a question about my while loop, it works for decimals if there are no corresponding values beside zero. My while loop looks like this. The change Due variable is initialized as a double.

I have added the following code for further clarification as a example. I'm still very confused.

while(changeDue!=0){

}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Please give a proper description of the issue you are facing – Tanuj Yadav Jan 14 '18 at 05:25
  • 0 is not a decimal, it is an integer (int) so that could be one issue. – Katianie Jan 14 '18 at 05:26
  • I can upload all my code, if you wish to see. Please keep in mind I have also tried 0.00001 and similar values, no decimals seem to work. – aegaewgawe aewggawegaw Jan 14 '18 at 05:28
  • I have uploaded the following, please examine if need be. – aegaewgawe aewggawegaw Jan 14 '18 at 05:30
  • 2
    this is not javascript, don't put js snippets. Please format your code and question properly. There's no variables called `change Due`, and [using floating-point math for currency is a bad idea](https://stackoverflow.com/q/588004/995714) – phuclv Jan 14 '18 at 05:37
  • Line 6 its declared and following down its been initialized, so yes the variable does exist – aegaewgawe aewggawegaw Jan 14 '18 at 05:39
  • and its changeDue <-- no spaces – aegaewgawe aewggawegaw Jan 14 '18 at 05:39
  • Shouldn't while(changeDuezeroValue)? – visibleman Jan 14 '18 at 05:44
  • 1
    Pretty sure the loop condition is backwards. It probably should be while (changeDue > zeroValue) { ... } – Quinn Mortimer Jan 14 '18 at 05:44
  • This code has been asked about twice before: [Program stops running after displaying result](https://stackoverflow.com/questions/48235713/program-stops-running-after-displaying-result) and [C program \n and while loop](https://stackoverflow.com/questions/48237563/c-program-n-and-while-loop). – Jonathan Leffler Jan 14 '18 at 06:16
  • The code `if((changeDue>=50.00)&&(changeDue<100)){ changeDue=changeDue-20.00; twenty=twenty+1; }` is confused; it should be dealing in $50 bills, not $20 (assuming the currency is dollars, of course; choose your currency symbol). – Jonathan Leffler Jan 14 '18 at 06:19
  • The whole loop structure is weird. Have you worked with arrays yet? What about structures? You should be looking to work with arrays that define the currency units. They also invented this technique called 'division'; it is rather useful for finding how many $100 bills are needed for $987 change. Also, and I believe this was pointed out before, there are many, many similar questions on SO. You should research other questions and their answers. – Jonathan Leffler Jan 14 '18 at 06:24
  • Fyi, `hundred=hundred+1;`, consider what value `hundred` is the first time that is executed (if it ever is). For unknown reasons, you chose not to initialize that variable like you did all the others. – WhozCraig Jan 14 '18 at 07:04
  • Always check return values of scanf functions, to detect parse errors. Debugging without being sure input is read is pointless. – hyde Jan 14 '18 at 14:40
  • You need to learn data types and loop.. just put your mind in your code after that definitely you will get your answer – Onic Team Jan 14 '18 at 14:43

1 Answers1

0

First of all

while(changeDue<zeroValue) should be while(changeDue>zeroValue)

Actually, you are probably better off skipping the outer loop and inner branches and instead do something like:

while(changeDue>=0.25){
    changeDue=changeDue-0.25;
    quarter=quarter+1;
}
while(changeDue>=0.10){
    changeDue=changeDue-0.10;
    dime=dime+1;
}

Otherwise, consider what will happen when changeDue is e.g. 51 cents.

And as said in the comments, of course, a problem like this should be solved as a division & remainder problem, but I assume that this was supposed to be an exercise in control structures.

visibleman
  • 3,175
  • 1
  • 14
  • 27