PDA

View Full Version : Need help with a program in C


MMG1590
02-23-2005, 07:04 PM
98 char determineContents_if(char gasCode)

99 {

100 if (gasCode == 'b' || 'B')

101 printf ("\nBrown - Carbon Monoxide\n");

102 else if (gasCode == 'O' || 'o')

103 printf ("\n Orange - Ammonia\n");

104 else if (gasCode == 'G' || 'g')

105 printf ("\nGreen - Oxygen\n");

106 else if (gasCode == 'Y' || 'y')

107 printf ("\nYellow - Hydrogen\n");

108 else printf ("\nInvalid Character\n");

109 return (0);

no matter what i put in it always prints "Brown - Carbon Monoxide" - i could put in y, g, or afdaosiof;a and it will always just print that - can anyone tell me why? do i need to rearrange the if statements or something?

same thing with this:


125 char determineContents_switch(char gasCode)

126 {

127

128 switch (x)

129 {

130 case 'O':

131 case 'o':

132 printf ("\nOrange - Ammonia\n");

133 break;

134 case 'B':

135 case 'b':

136 printf ("\nBrown - Carbon Monoxide\n");

137 break;

138 case 'Y':

139 case 'y':

140 printf ("\nYellow - Hydrogen\n");

141 break;
142 case 'G':
143 case 'g':
144 printf ("/nGreen - Oxygen\n");
145 break;
146 default:
147 printf ("\nUnknown Contents\n");
148 }
149 return(0);
150 }
151 /* char determineContents_switch */


except instead of brown - ammonia it just prints "Unkown Contents" no matter whats put in for the code


the point of the program is to have a letter inputed (B b Y y O o G g) and then print the color/contents - if anything else is put in besides the specified letters youre supposed to get an error (like "Unknown COntents")

some other backround information about the program - theres 5 functions total - main, intro, getCode, determinecontents_if, determinecontents, switch - intro is just a introduction of the program, getCode has the user input the gascode and then return it to main, getcontents_if and getcontents_switch receive the code from main and then are supposed to print the corresponding color/contents - - any help would be greatly appreciated

(i know the spacing looks messed up but thats just how it copied from C - if you need to see more of the program i can copy and paste the whole thing, its just messy to read)

thanks

JonoF
02-23-2005, 08:56 PM
char determineContents_if(char gasCode)
{
if (gasCode == 'b' || gasCode == 'B')
printf ("\nBrown - Carbon Monoxide\n");
else if (gasCode == 'O' || gasCode == 'o')
printf ("\n Orange - Ammonia\n");
else if (gasCode == 'G' || gasCode == 'g')
printf ("\nGreen - Oxygen\n");
else if (gasCode == 'Y' || gasCode == 'y')
printf ("\nYellow - Hydrogen\n");
else printf ("\nInvalid Character\n");
return (0);
}

char determineContents_switch(char gasCode)
{
switch (gasCode)
{
case 'O':
case 'o':
printf ("\nOrange - Ammonia\n");
break;
case 'B':
case 'b':
printf ("\nBrown - Carbon Monoxide\n");
break;
case 'Y':
case 'y':
printf ("\nYellow - Hydrogen\n");
break;
case 'G':
case 'g':
printf ("\nGreen - Oxygen\n");
break;
default:
printf ("\nUnknown Contents\n");
break;
}
return(0);
}


Jonathon

DudeMiester
02-23-2005, 09:45 PM
Yeah the switch looks find, you just screwed up the if conditions in the first function. Remember your operator precendance. The == is evaulated before the ||. Even so it's still inherintly wrong, because you aren't comparing gasCode with the second condition in each case. Look at the fixed code to see exactly what I mean.

MMG1590
02-24-2005, 09:12 AM
Oh ok...i see where i made the mistake now, thanks for the help guys

Night Hacker
02-25-2005, 03:01 AM
You may also consider converting the character to uppercase before you test, this way you only have to test for a capital letter.

for example...

gasCode = toupper(gasCode);

if (gasCode == 'B')

(note: you need to: #include <ctype.h> to use this.)

There's also tolower() which converts to lowercase.

DudeMiester
02-25-2005, 08:56 PM
^^That would actually be more efficent then your current method, not like you'd actually notice it lol. Still it's good to keep in mind. While I don't massively optimise while I code, I do try to keep things nice and efficent.