简体   繁体   English

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

[英]How to have a C program respond to a letter and spit out something else?

I can't seem to get this program to compile. 我似乎无法编译这个程序。

I keep getting the error: 我一直收到错误:

'Ammonia' undeclared 'Carbon_Monoxide' undeclared

and so on. 等等。 Am I using the right function with switch? 我使用开关的正确功能吗?

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

It has nothing to do with your switch statement, but you will probably have to puzzle over the meaning of these four lines: 它与您的switch语句无关,但您可能不得不对这四行的含义进行拼图:

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

You don't use the variables thus defined anywhere, and the symbols "Ammonia", "Carbon_Monoxide" and so on are not defined - this is the cause of the error you are seeing. 您不使用在任何地方定义的变量,并且未定义符号“Ammonia”,“Carbon_Monoxide”等 - 这是您看到的错误的原因。

Since this is homework, I don't want to give you the answer straight, but look at what you're doing with those chars (o, b, y, g) and ask yourself if it makes sense. 由于这是家庭作业,我不想直接给你答案,但看看你对这些字符(o,b,y,g)做了什么,并问自己是否有意义。

Also, on the switch statement, I'm pretty sure you need a break; 另外,在switch语句中,我很确定你需要休息一下; after each case, else it will print each case's statement 在每个案例之后,否则它将打印每个案例的陈述

try: 尝试:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM