【问题标题】:Program stops running after displaying result显示结果后程序停止运行
【发布时间】:2018-01-13 00:11:27
【问题描述】:

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

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

有什么想法吗?

注意,这是我第一次用 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 ... */

【问题讨论】:

  • 货币不要使用float,而是使用int
  • 不幸的是,我现在需要使用 float。
  • 你看到我的逻辑在哪里失败了吗?
  • 您能告诉我们您对程序的输入吗?我们无法有效地猜出您尝试输入的内容导致它无法正常工作。
  • 议案附议。浮动和货币不混合。但你的问题可能是scanf()s。在那里做更多的调试。此外,可以 scanf() 浮点数,然后为应用程序的其余部分转换为整数。如果你的老师真的想让你在浮点数中做所有的数学,他就是个白痴,你应该找另一位老师。

标签: c


【解决方案1】:

您在比较浮点值的身份时遇到问题
(或者不一样,同样的问题)这里:

while(changeDue!=0.00){

查看这里了解一些背景:
Is floating point math broken?

它会创建一个无限循环(至少如果你不是“幸运”的话),这会阻止所有进一步的打印。 (其实“幸运”并不是隐藏bug的好描述……)
为了验证这个诊断,在循环开始处插入一个 printf。

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

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

为了解决这个死循环问题,改成

while(changeDue>=0.01)

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

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

请注意,其中一个 cmets 建议使用 int 作为货币。
我通常同意,但我接受了你必须使用 float/double 的声明。

顺便说一下,使用第一个版本的代码。
在第二个版本中,您打印的是某物的地址而不是值。 IE。 &amp; 在这里是错误的:

printf("%d", &tendered);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    相关资源
    最近更新 更多