-2

guys I have a C program question. My goal is to make a change operation working for taking a price and giving out the current amount remaining. I code in java, so this is my first time in C, Im not looking to have my entire code done for me. I can do the code, however im having trouble getting it to execute the way I want. My problem here is that when I use \n my code seems to work, but my output is really weird, i have to add constant spaces and repeat my lines. not sure why this is happening, Also my while loop does not seem to execute, which is making no sense to me. If anyone could help , I would apprectiate it. Thank you for reading, ps I reply isntantly

/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

/* Header comment that describes the purpose of the program
 * Name Karanvir Dhillon
 * Date Jan 12
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.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\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 \n", changeDue);
 }


 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 \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;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I have figured out \n however im still struggling trying to see if my while loop is even executing, it doesnt seem to do anything. Im not sure why this problem is even occurring – krgarh jagjbar Jan 13 '18 at 06:46
  • Could the problem be in C, you cant re assign a variable value ? – krgarh jagjbar Jan 13 '18 at 06:49
  • 3
    [Get a couple of good beginners books](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), read them and do their exercises. – Some programmer dude Jan 13 '18 at 06:51
  • 2
    [Didn't you ask the same question](https://stackoverflow.com/questions/48235713/program-stops-running-after-displaying-result) under a different user name earlier? – ad absurdum Jan 13 '18 at 06:54
  • You described your weird "solution" to your first problem but not the problem itself. As for the second you shouldn't be doing a direct comparison between two floats, especially where one is the result of some arithmetic operation. – George Jan 13 '18 at 06:59
  • You should be checking the value returned by functions which return meaningful values, such as `scanf()`. Also, it is almost always wrong to use trailing whitespace characters in `scanf()` format strings. This leads to many interactive input problems, and probably does not do what you think it does. Note that the only conversion specifiers which don't _automatically_ skip leading whitespace characters are `%c`, `%[]`, and `%n`. – ad absurdum Jan 13 '18 at 07:01
  • If I dont include the \n my code does no execute – krgarh jagjbar Jan 13 '18 at 07:05
  • any resources you can recommend? – krgarh jagjbar Jan 13 '18 at 07:05
  • I am going to give the answer away, i think i found my problem. which took some playing around to figure out. why is this throwing me a error f((changeDue>=50.00)&&(<=100)) – krgarh jagjbar Jan 13 '18 at 07:13
  • is this not the way to make if and statements? – krgarh jagjbar Jan 13 '18 at 07:14
  • nvm found it, someone delete this thread thanks. i am learning on my own – krgarh jagjbar Jan 13 '18 at 07:15

1 Answers1

-1

In most C compilers, including ours, the newline escape sequence '\n' yields an ASCII line feed character. The C escape sequence for a carriage return is '\r'.

Include a '\r' along with (or perhaps instead of) the '\n' in output strings. For example:

printf("Hello world\r\n");

Prasanta Bose
  • 674
  • 5
  • 13