我创建了一个程序来读取用户的 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 在您的代码中发现了两个错误。
- 删除
while( counter < 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/