3

5つの選択肢がある単純なswitchステートメントをコンパイルしようとしています。1-4は計算を生成し、#5がプログラムを終了する間に出力します。do / whileループを作成したので、選択肢5を入力すると、プログラムは終了します。エラーが発生します:

4_19.c: In function ‘main’:
4_19.c:95: error: ‘choice’ undeclared (first use in this function)
4_19.c:95: error: (Each undeclared identifier is reported only once
4_19.c:95: error: for each function it appears in.)

最初に宣言したので、なぜ宣言されていないのかわかりません。私は何を間違えましたか?ありがとう。これが私のコードです:

    /* C++ book. 4_19 The speed of sound in gases. 
Create a menu to choose between 4 gases. User then enters number of seconds
it took to travel to destination. The program will calculate how far the source was (from speed that is unique to gas density). Validate input of seconds from 0 to 30 seconds only. 
*/

#include <stdio.h>
int main(void)
{
    do
    {
        // Declare variables
        int choice;
        float speed, seconds = 0, distance;

        //Display program details and menu choice
        printf("\n");
        printf("Choose a gas that you would like to analyze.\n");
        printf("Medium                              Speed(m/s)\n");
        printf("1.Carbon Dioxide                    258.0\n");
        printf("2.Air                               331.5\n");
        printf("3.Helium                            972.0\n");
        printf("4.Hydrogen                          1270.0\n");
        printf("5.Quit Program");
        printf("Enter a choice 1-5: ");
        scanf("%i",&choice);
        while (choice < 1 || choice > 5)        // Validate choice input. 
        {   
            printf("You entered an invalid number. Choose 1,2,3, or 4 only.\n");
            printf("Enter a choice 1-5: ");
            scanf("%i",&choice);
        }

        // Switch statements to execute different choices
        switch(choice)
        {
            case 1:     // Carbon Dioxide
                printf("Enter number of seconds, from 0 to 30, that the sound traveled in carbon dioxide: ");
                scanf("%f", &seconds);
                while (seconds < 0 || seconds > 30)      // Validate time entered
                {
                    printf("The range of input for seconds is only from 0 to 30 seconds.\n");
                    printf("Please enter a valid number for number of seconds: ");
                    scanf("%f", &seconds);
                }
                speed = 258.0;
                distance = speed * seconds;
                printf("The distance from the source of the sound is %.2f meters in carbon dioxide.\n", distance);
                break;

            case 2:     // Air
                printf("Enter number of seconds, from 0 to 30, that the sound traveled in air: ");
                scanf("%f", &seconds);
                while (seconds < 0 || seconds > 30)      // Validate time entered
                {
                    printf("The range of input for seconds is only from 0 to 30 seconds.\n");
                    printf("Please enter a valid number for number of seconds: ");
                    scanf("%f", &seconds);
                }
                speed = 331.5;
                distance = speed * seconds;
                printf("The distance from the source of the sound is %.2f meters in air.\n", distance);
                break;

            case 3:     // Helium
                printf("Enter number of seconds, from 0 to 30, that the sound traveled in helium: ");
                scanf("%f", &seconds);
                while (seconds < 0 || seconds > 30)      // Validate time entered
                {
                    printf("The range of input for seconds is only from 0 to 30 seconds.\n");
                    printf("Please enter a valid number for number of seconds: ");
                    scanf("%f", &seconds);
                }
                speed = 972.0;
                distance = speed * seconds;
                printf("The distance from the source of the sound is %.2f meters in helium.\n", distance);
                break;

            case 4:     // Hydrogen
                printf("Enter number of seconds, from 0 to 30, that the sound traveled in hydrogen: ");
                scanf("%f", &seconds);
                while (seconds < 0 || seconds > 30)      // Validate time entered
                {
                    printf("The range of input for seconds is only from 0 to 30 seconds.\n");
                    printf("Please enter a valid number for number of seconds: ");
                    scanf("%f", &seconds);
                }
                speed = 1270.0;
                distance = speed * seconds;
                printf("The distance from the source of the sound is %.2f meters in hydrogen.\n", distance);
                break;

            case 5:
                printf("End of Program\n");
                break;
        }
    } while (choice != 5);

    return 0;
}
4

4 に答える 4

8

ループchoice内で宣言しました。do { } whileこれは、これら2つの中括弧内でのみアクセスできることを意味します。ただし、あなたのwhile(choice != 5)状態では、中括弧の外側で再度参照します。これはエラーです。解決策は、1レベル上に移動choiceし、のスコープ内で宣言することですmain

于 2011-07-05T03:39:37.980 に答える
3

の宣言をchoiceループの外側に移動します。の直前do

choiceループでのみスコープされます。このwhile()句はループの外側にあるため、ループの中括弧自体の内側で宣言されたものにはアクセスできません。

于 2011-07-05T03:41:40.127 に答える
2

ステートメントchoiceの前に宣言するだけです。doこれは、ループ内で宣言すると、スコープがローカルでループ内のみであるためdo、条件に表示されないためです。while

于 2011-07-05T03:40:52.317 に答える
1

これについてコメントするのはとても遅いことを私は知っています。ただし、将来誰かがこの問題に遭遇し、この質問を見つけた場合。括弧の外で変数を宣言する必要があります。これが例です。

int main()
{
    char choice;  // Variables defined outside loop. With in the scope of main.
    double a, x, res;
    do
    {


        cout << "Enter a: ";
        cin >> a;
        cout << "Enter x: ";
        cin >> x;

        res = 5.00 * tanh(a, x) + 4.00 * cos(a, x);

        cout << "F = " << res << endl;
        cout << "Would you like to continue? ";
        cin >> choice;

    } while (choice == 'Y' || choice == 'y');

    return 0;
}
于 2020-04-30T18:37:39.290 に答える