C程序\ n和while循环

时间:2018-01-13 06:45:47

标签: c

伙计我有一个C程序问题。我的目标是让变更操作工作以获取价格并给出剩余的当前金额。我在java中编码,所以这是我第一次使用C语言,我不希望为我完成整个代码。我可以做代码,但是我无法让它按照我想要的方式执行。我的问题是,当我使用\ n我的代码似乎工作,但我的输出真的很奇怪,我必须添加常量空格并重复我的行。不知道为什么会这样,我的while循环似乎没有执行,这对我没有意义。如果有人可以提供帮助,我会很感激。谢谢你的阅读,ps我的答复是

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

                            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;
}

1 个答案:

答案 0 :(得分:-1)

在大多数C编译器中,包括我们的编译器,换行符转义序列&#39; \ n&#39;产生ASCII换行符。回车的C转义序列是&#39; \ r&#39;。

加入&#39; \ r&#39;以及(或许代替)&#39; \ n&#39;在输出字符串中。例如:

printf(&#34; Hello world \ r \ n&#34;);