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

标签 c loops runtime-error

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

#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++;

   }
}

非常感谢您的帮助!

最佳答案

BLUEPIXY 在您的代码中发现了两个错误。

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

    scanf("%c %d", 糖果, Quant ); 所以它看起来像这样

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

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

关于c - 如何修复 C 中的致命运行时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27131655/

相关文章:

c - 确定 shell 扩展是作为上下文菜单处理程序还是拖放处理程序调用

c - 在 x86_64/linux 上比 glibc 更快的数学库?

r - r中的季度增长计算

java - 从较短的字符串打印字符后,程序停止从两个字符串一次打印一个字符

c++ - 代码在 Linux 上编译和运行,在 Windows 上因堆损坏而崩溃

asp.net - 为什么我在此 aspx 页面中看不到详细的错误消息?

c - 如何用 git 处理这个补丁问题?

c - 为什么我的 Perl TCP 服务器脚本在许多 TCP 连接时挂起?

c++ - 无法将数组中的负值转换为正值?

安卓模拟器崩溃: "Dx bad class file magic"/ClassNotFoundException on startup?