tags:

views:

119

answers:

3

Hey guys, 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;
}
A: 

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;

}
DShook
This did have a "homework" tag, you know.
sbi
+4  A: 

It has nothing to do with your switch statement, but you will probably have to puzzle over the meaning of these four lines:

 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.

1800 INFORMATION
So by defining them as chars above, that doesn't help?
Chandler
the line `o = Ammonia` doesn't make sense. o expects a single char, and it can't make one out of "Ammonia." Those lines could be a good comment, though, to keep track of what's what.
thepocketwade
Okay so,by saying o = Ammonia <-- That's useless? Okay that makes sense now. I guess I don't understand why you don't have to declare any of the "case" characters
Chandler
The `case` statements don't need declaring. They test `x` against _values_ which are `'o'` (the literal character o), not against abstract ideas.
Chris Lutz
So no declaring is needed when I use values that are 'char'?
Chandler
Thank you, you guys have tought me better than my teacher!
Chandler
+3  A: 

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.

Also, on the switch statement, I'm pretty sure you need a break; after each case, else it will print each case's statement

thepocketwade
Wow, thank you did not notice that I forgot the breaks :XI think I am having trouble discerning the difference in coding between integers and characters
Chandler