0

Turbo C++ コンパイラでエラーが表示されないにもかかわらず、.exe プログラムが実行されない理由を誰か説明してもらえますか?

コンパイラはエラーを表示しません

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

私はcに比較的慣れていないため、何が間違いなのかわかりません

4

3 に答える 3

5

変数choiceは初期化されておらず、書き込まれることはありません。ユーザーに入力を求めた後、入力された値を実際にスキャンする必要があります。

于 2013-09-09T09:43:23.087 に答える
1

切り替える前に scanf を使用する必要があります。「choice」に値を割り当てていないため、機能しません。scanf を 1 つだけ使用し、switch の前に使用します。

于 2013-09-09T09:42:36.490 に答える
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);

于 2013-09-09T09:56:07.677 に答える