有关 C 语言中 do/while 和 switch 语句的帮助

发布于 2024-11-18 14:25:44 字数 4902 浏览 3 评论 0原文

尝试编译一个有 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;
}

Trying to compile a simple switch statement with 5 choices. 1-4 produce calculations and output while #5 exits the program. I made a do/while loop so if choice 5 is entered the program will end. I get an error:

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.)

I don't know why it is saying its undeclared, because I declared it in the beginning. What did I do wrong? Thanx. Here is my code:

    /* 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空名 2024-11-25 14:25:44

您已在 do { } while 循环中声明了 choice;这意味着只能在这两个大括号内访问它。但是,在您的 while(choice != 5) 条件中,您在大括号之外再次引用它;这是一个错误。解决方案是将 choice 上移一级,并在 main 的范围内声明它。

You have declared choice inside your do { } while loop; this means that it can only be accessed within those two braces. However, in your while(choice != 5) condition, you reference it again, outside the braces; this is an error. The solution is to move choice up one level, and declare it within the scope for main.

無處可尋 2024-11-25 14:25:44

choice 的声明移到循环之外。就在do之前。

choice 的范围仅限于循环内部。 while() 子句位于循环外部,因此它无法访问循环大括号内声明的内容。

Move the declaration of choice outside of the loop. Right before the do.

choice is only scoped inside the loop. The while() clause is outside of the loop, so it can't access things that were declared inside the loop's curly braces itself.

颜漓半夏 2024-11-25 14:25:44

只需在 do 语句之前声明 choice 即可。这是因为当您在 do 循环中声明它时,它在 while 条件中不可见,因为作用域是本地的并且仅在循环内

Just declare choice before the do statement. This is because when you declare it within the do loop, its not visible in the while condition as the scope is local and within the loop only

屌丝范 2024-11-25 14:25:44

我知道现在对此发表评论已经太晚了。但是,如果将来有人遇到这个问题并发现这个问题。您需要在括号之外声明变量。这是一个例子。

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

I know it is super late to comment on this. However, if anyone runs into this problem in the future and finds this question. You need to declare variables outside of the brackets. Here is an example.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文