转换为克

时间:2013-09-09 09:40:21

标签: c

有人可以解释为什么.exe程序没有运行,尽管turbo c ++编译器没有显示任何错误?

编译器不会显示任何错误

#include <stdio.h>
#include <conio.h>

int main ()
{
int choice;
float num, result;
printf("Select your choice\n");
printf("Press 1 for conversion from milligrams to grams\n");
printf("Press 2 for conversion from decigrams to grams\n");
printf("Press 3 for conversion from centigrams to grams\n");
printf("Press 4 for conversion from kilograms to grams\n");
printf("Press 5 for conversion from ounce to grams\n");
printf("Press 6 for conversion from pounds to grams\n");
printf("Press 7 for conversion from ton to grams\n");

switch(choice) //i thought the switch statement would be appropriate
{
case 1:
        printf("Enter the weight in milligrams for conversion\n");
        scanf("&#37;f", &num);

        result=num*0.001;

        printf("The weight in after conversion is %f g", result);
        getch();
        getch();
        break;

case 2:
        printf("Enter the weight in decigrams for conversion\n");
        scanf("&#37;f", &num);

        result=num*0.1;

        printf("The weight in after conversion is %f g", result);
        getch();
        getch();
        break;

case 3:
        printf("Enter the weight in centigrams for conversion\n");
        scanf("&#37;f", &num);

        result=num*0.01;

        printf("The weight in after conversion is %f g", result);
        getch();
        getch();
        break;

case 4:
        printf("Enter the weight in kilograms for conversion\n");
        scanf("&#37;f", &num);

        result=num*1000.0;

        printf("The weight in after conversion is %f g", result);
        getch();
        getch();
        break;

case 5:
        printf("Enter the weight in ounce for conversion\n");
        scanf("&#37;f", &num);

        result=num*28.3495;

        printf("The weight in after conversion is %f g", result);
        getch();
        getch();
        break;

case 6:
        printf("Enter the weight in pounds for conversion\n");
        scanf("&#37;f", &num);

        result=num*453.592;

        printf("The weight in after conversion is %f g", result);
        getch();
        getch();
        break;

case 7:
        printf("Enter the weight in ton for conversion\n");
        scanf("&#37;f", &num);

        result=num*907185.00;

        printf("The weight in after conversion is %f g", result);
        getch();
        getch();
        break;

default:
        printf("invalid choice\n");
        break;
        }
        return 0;
        }

我比较新,我不知道我的错误是什么

3 个答案:

答案 0 :(得分:5)

您的变量choice未初始化且从未写入。提示用户输入后,您需要实际扫描输入的值。

答案 1 :(得分:1)

您应该在切换之前使用scanf。 它不起作用,因为你没有为'choice'赋值。 只使用一个scanf并在切换之前使用它。

答案 2 :(得分:1)

首先获取用户选择的输入,然后在switch语句中进行检查。希望这有帮助

#include <stdio.h>
#include <conio.h>

int main ()
{
int choice;
float num, result;
printf("Select your choice\n");
printf("Press 1 for conversion from milligrams to grams\n");
printf("Press 2 for conversion from decigrams to grams\n");
printf("Press 3 for conversion from centigrams to grams\n");
printf("Press 4 for conversion from kilograms to grams\n");
printf("Press 5 for conversion from ounce to grams\n");
printf("Press 6 for conversion from pounds to grams\n");
printf("Press 7 for conversion from ton to grams\n");

scanf("%d",&choice);        //Added this line

switch(choice) //i thought the switch statement would be appropriate
{
case 1:
        printf("Enter the weight in milligrams for conversion\n");
        scanf("&#37;f", &num);

        result=num*0.001;

        printf("The weight in after conversion is %f g", result);
        getch();
        break;

case 2:
        printf("Enter the weight in decigrams for conversion\n");
        scanf("&#37;f", &num);

        result=num*0.1;

        printf("The weight in after conversion is %f g", result);
        getch();
        break;

case 3:
        printf("Enter the weight in centigrams for conversion\n");
        scanf("&#37;f", &num);

        result=num*0.01;

        printf("The weight in after conversion is %f g", result);
        getch();
        break;

case 4:
        printf("Enter the weight in kilograms for conversion\n");
        scanf("&#37;f", &num);

        result=num*1000.0;

        printf("The weight in after conversion is %f g", result);
        getch();
        break;

case 5:
        printf("Enter the weight in ounce for conversion\n");
        scanf("&#37;f", &num);

        result=num*28.3495;

        printf("The weight in after conversion is %f g", result);
        getch();
        break;

case 6:
        printf("Enter the weight in pounds for conversion\n");
        scanf("&#37;f", &num);

        result=num*453.592;

        printf("The weight in after conversion is %f g", result);
        getch();
        break;

case 7:
        printf("Enter the weight in ton for conversion\n");
        scanf("&#37;f", &num);

        result=num*907185.00;

        printf("The weight in after conversion is %f g", result);
        getch();
        break;

default:
        printf("invalid choice\n");
        break;
       }
        return 0;
  }

另外,我不理解您使用的格式说明符scanf("&#37;f", &num);,我使用了scanf("%f",&num);