如何让C程序回复一封信并吐出其他东西?

时间:2009-09-30 03:37:59

标签: c

我好像无法编译这个程序。

我一直收到错误:

'Ammonia' undeclared 'Carbon_Monoxide' undeclared

等等。我是否使用了带开关的正确功能?

/*This program will report the content of a compressed-gas cylinder based on the first letter of the cylinder's color.*/


#include <stdio.h>
int main (void)
{ 
     int x; 
     char o, b, y, g;
     int pause;

     o = Ammonia;
     b = Carbon_Monoxide;
     y = Hydrogen;
     g = Oxygen;

     printf(" Enter a character representing the observed color of the cylinder \n" );
     scanf("%d", &x);
     switch (x)
     {

        case 'o': printf("The content is Ammonia\n");
        case 'b': printf("The content is Carbon Monoxide\n");
        case 'y': printf("The content is Hydrogen\n");
        case 'g': printf("The content is Oxygen\n");
        default: printf("Character out of range \n");
     }

     printf("After Switch \n");
     printf("Enter new character to continue \n");
     scanf("%d", &pause);

     return 0;
}

3 个答案:

答案 0 :(得分:4)

它与您的switch语句无关,但您可能不得不对这四行的含义进行拼图:

 o = Ammonia;
 b = Carbon_Monoxide;
 y = Hydrogen;
 g = Oxygen;

您不使用在任何地方定义的变量,并且未定义符号“Ammonia”,“Carbon_Monoxide”等 - 这是您所看到的错误的原因。

答案 1 :(得分:3)

由于这是作业,我不想直接给你答案,而是看看你对这些字符(o,b,y,g)做了什么,并问自己是否有意义。

另外,在switch语句中,我很确定你需要休息一下;在每个案例之后,否则它将打印每个案例的陈述

答案 2 :(得分:-3)

尝试:

#include <stdio.h>

int main (void)

{

    char x; 

    int pause;


    printf(" Enter a character representing the observed color of the cylinder \n" );
    scanf("%c", &x);


    while(x){
        switch (x)

        {

        case 'o': printf("The content is Ammonia\n"); break;
        case 'b': printf("The content is Carbon Monoxide\n"); break;
        case 'y': printf("The content is Hydrogen\n"); break;
        case 'g': printf("The content is Oxygen\n"); break;
        default: printf("Character out of range \n");

        }

        printf("After Switch \n");
        printf("Enter new character to continue \n");
        scanf(" %c", &x);

        if(x == 'q'){
            break;
        }
    }
    return 0;

}