显示结果后程序停止运行

时间:2018-01-13 00:11:27

标签: c

我希望能够将某人的欠款作为价格,然后根据收到的金额进行一些数学计算并打印我的结果。

以下是我提出的代码。但是,我的程序在显示投标金额后没有运行。

有什么想法吗?

请注意,这是我第一次使用C语言进行编码,而且我是来自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;
}

我有该代码的替代版本,它将扫描和打印的第一部分更改为

 /* 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 ... */

我有第三个版本,第一个扫描和打印是这样的。

 /* 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 ... */

1 个答案:

答案 0 :(得分:0)

您有比较身份的浮点值的问题 (或者不相同,相同的问题):

while(changeDue!=0.00){

请参阅此处了解背景信息:
Is floating point math broken?

它创造了一个无限循环(至少如果你不是&#34;幸运&#34;),这会阻止所有进一步的打印。 (实际上&#34;幸运&#34;不是隐藏错误的好描述......)
为了验证此诊断,请在循环开始时插入printf。

while(changeDue!=0.00){
    printf("Making change...\n");

您将看到比预期更多的调试输出行。

为了解决无穷循环问题,请转到

while(changeDue>=0.01)

并解决了无限循环,目前可以防止在打印到期金额后发生任何可见的事情。

这并不一定能解决代码中的所有问题,但问题中描述的最突出问题已经解决。

请注意,其中一条评论建议使用int作为货币 我通常同意,但我已接受你的陈述,你必须使用float / double。

顺便说一下,使用代码的第一个版本 在第二个版本中,您打印的是某些内容的地址而不是值。即这里&错了:

printf("%d", &tendered);

在第三个版本中,您的类型和printf中的格式字符串不匹配。

相关问题