加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C中帮助do / while和switch语句

发布时间:2020-12-16 10:29:34 所属栏目:百科 来源:网络整理
导读:尝试编译一个包含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
尝试编译一个包含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.0n");
        printf("2.Air                               331.5n");
        printf("3.Helium                            972.0n");
        printf("4.Hydrogen                          1270.0n");
        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,that the sound traveled in air: ");
                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,that the sound traveled in helium: ");
                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,that the sound traveled in hydrogen: ");
                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 Programn");
                break;
        }
    } while (choice != 5);

    return 0;
}

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读