Forum Archive

Go Back   3D Realms Forums > General Topics > Programming Forum
Blogs FAQ Members List Social Groups Calendar Mark Forums Read


Thread Tools
Old 02-23-2005, 07:04 PM   #1
Need help with a program in C
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 {


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)

MMG1590 is offline  
Old 02-23-2005, 08:56 PM   #2

JonoF's Avatar
Re: Need help with a program in C
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");
        case 'B':
        case 'b':
            printf ("\nBrown - Carbon Monoxide\n");
        case 'Y':
        case 'y':
            printf ("\nYellow - Hydrogen\n");
        case 'G':
        case 'g':
            printf ("\nGreen - Oxygen\n");
            printf ("\nUnknown Contents\n");
JonoF is offline  
Old 02-23-2005, 09:45 PM   #3

DudeMiester's Avatar
Re: Need help with a program in C
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.
Forums are the Opiate of the Masses
Higher Game -- "Animal fur is renewable, unlike the oil used to make plastic!"
DudeMiester is offline  
Old 02-24-2005, 09:12 AM   #4
Re: Need help with a program in C
Oh ok...i see where i made the mistake now, thanks for the help guys
MMG1590 is offline  
Old 02-25-2005, 03:01 AM   #5
Night Hacker

Night Hacker's Avatar
Re: Need help with a program in C
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.
"My general feeling is that computers are doing more harm than good." - Jay Miner ("father" of the Amiga)
Night Hacker is offline  
Old 02-25-2005, 08:56 PM   #6

DudeMiester's Avatar
Re: Need help with a program in C
^^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.
Forums are the Opiate of the Masses
Higher Game -- "Animal fur is renewable, unlike the oil used to make plastic!"
DudeMiester is offline  


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -6. The time now is 04:15 AM.

Page generated in 0.12098289 seconds (100.00% PHP - 0% MySQL) with 16 queries

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.

Website is 1987-2014 Apogee Software, Ltd.
Ideas and messages posted here become property of Apogee Software Ltd.