如何修复C中的致命运行时错误?

时间:2014-11-25 16:17:46

标签: c loops runtime-error

我创建了一个程序来读取用户的5个输入:金额,选择1,数量1,选择2,数量2。 该程序旨在计算每个数量的糖果价值并将其从总金额中扣除,需要使用某种迭代。 我检查了代码并且找不到任何明显的错误,但每次运行我收到的程序时都会发现错误"错误:致命运行时错误"没有显示其他错误代码。 我的程序代码如下,我编写导致错误的代码的方式有什么问题吗?

#include <stdio.h>
#include <string.h>

int main( void )
{
   int quant, pence, cost, counter = 0;
   double money;
   char candy;
   char output[60] = {0};

   scanf( "%lf %c %d", &money, &candy, &quant );
   pence = (int)(money * 100);

   while( counter < 2 );
    {
    switch (candy)
      {
      case 'a' :
         cost = quant * 55;
         if( pence < cost)
         {
            printf( "You selected %d Mars bars, but do not have enough money", quant );
         }
         else
         {
            pence = pence - cost;
            strcpy( output, "Mars bars" );
         }
         break;
      case 'b' :
         cost = quant * 55;
         if( pence < cost)
         {
            printf( "You selected %d Snickers bars, but do not have enough money", quant );
         }
         else 
         {
            pence = pence - cost;
            strcpy( output, "Snickers bars" );
         }
         break;
      case 'c' :
         cost = quant * 55;
         if( pence < cost)
         {
            printf( "You selected %d Bounty Bars, but do not have enough money", quant );
         }
         else
         {
            pence = pence - cost;
            strcpy( output, "Bounty bars" );
         }
         break;
      case 'd' :
         cost = quant * 85;
         if( pence < cost)
         {
            printf( "You selected %d Peanut M&M bags, but do not have enough money", quant );
         }
         else 
         {
            pence = pence - cost;
            strcpy( output, "Peanut M&M bags" );
         }
         break;
      case 'e' :
         cost = quant * 85;
         if( pence < cost)
         {
            printf( "You selected %d Chocolate M&M bags, but do not have enough money", quant );
         }
         else
         {
            pence = pence - cost;
            strcpy( output, "Chocolate M&M bags" );
         }
         break;
      case 'f' :
         cost = quant * 65;
         if( pence < cost)
         {
            printf( "You selected %d Aero Bubbles bars, but do not have enough money", quant );
         }
         else
         {
            pence = pence - cost;
            strcpy( output, "Aero Bubbles bars" );
         }
         break;
      case 'g' :
         cost = quant * 55;
         if( pence < cost)
         {
            printf( "You selected %d Fruit Pastilles rolls, but do not have enough money", quant );
         }
         else
         {
            pence = pence - cost;
            strcpy( output, "Fruit Pastilles rolls" );
         }
         break;
      case 'h' :
         cost = quant * 55;
         if( pence < cost)
         {
            printf( "You selected %d Wine Gums rolls, but do not have enough money", quant );
         }
         else
         {
            pence = pence - cost;
            strcpy( output, "Wine Gums rolls" );
         }
         break;
      case 'i' :
         cost = quant * 45;
         if( pence < cost)
         {
            printf( "You selected %d Polo Mints rolls, but do not have enough money", quant );
         }
         else
         {
            pence = pence - cost;
            strcpy( output, "Polo Mints rolls" );
         }
         break;
      case 'j' :
         cost = quant * 95;
         if( pence < cost)
         {
            printf( "You selected %d Haribo Gold Bears, but do not have enough money", quant );
         }
         else
         {
            pence = pence - cost;
            strcpy( output, "Haribo Gold Bears bags" );
         }
         break;
      default :
         printf( "%c is an invalid candy selection", candy );
         exit(1);
         break;
      }      
   money = (double)(pence / 100.00);
    printf( "You bought %d %s", quant, output );
   printf( "Change remaining is %0.2lf", money );

   if( counter < 1 )
   {    
      scanf( "%c %d", candy, quant );
   }
   counter++;

   }
}

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您的代码中存在两个错误,因为BLUEPIXY已在您的代码中发现。

  1. 删除while( counter < 2 );中的分号以避免无限循环。
  2. 中添加&符号

    scanf(“%c%d”,糖果,定量); 所以它看起来像这样

    scanf(“%c%d”,&amp; candy,&amp; quant); //还要注意%c之前的空格 这样做是因为scanf期望变量的地址而不是其值来存储用户输入。

  3. 同时删除break;之后的exit(1)(在default情况下),因为它永远不会执行。您需要包含stdlib.h才能使用exit