Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 23rd, 2005, 7:42 PM   #1
mmg1590
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 mmg1590 is on a distinguished road
Need help with a program...

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
mmg1590 is offline   Reply With Quote
Old Feb 23rd, 2005, 7:49 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
I'm not sure if this would do anything, but have you tried doing this:
 if(gasCode == 'B' || gasCode =='b')
 {
 	 // code
 }
 // same with all the others add the || gasCode == 'letter'

If you posted the entire code, we could help you better, as we could compile it and take a look at exactly what's going on.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 23rd, 2005, 8:30 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I didn't read the entire thread.... but i did notice in your comparisons..

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

those should actually be along the lines of this

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

you could also use the toupper or tolower function, or a double cased switch statement to do this.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Feb 23rd, 2005, 9:32 PM   #4
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
no curly brackets after the if statements, so it produces the first output, which is the brown-carbon monoxide. Since it fulfills the if statement, the else if's are disregarded and the app terminates (or goes to another function in main, determining on what your program does).
gardon is offline   Reply With Quote
Old Feb 23rd, 2005, 9:46 PM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
use #include <ctype.h>

and do...

gasType = tolower(gasType);

and it'll convert it to lowercase making it easier to evaluate
__________________

tempest is offline   Reply With Quote
Old Feb 23rd, 2005, 9:47 PM   #6
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Quote:
Originally Posted by gardon
no curly brackets after the if statements, so it produces the first output, which is the brown-carbon monoxide. Since it fulfills the if statement, the else if's are disregarded and the app terminates (or goes to another function in main, determining on what your program does).
If it's only one statement after the if statement, then it shouldn't require curly-brackets.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 23rd, 2005, 9:49 PM   #7
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
At the top of your program add this header...
#include <ctype.h>

char determineContents_if(char gasCode) {
    gasCode = toupper(gasCode); // Convert to uppercase

    if (gasCode == 'B')
        printf ("\nBrown - Carbon Monoxide\n");
    else if (gasCode == 'O')
        printf ("\n Orange - Ammonia\n");
    else if (gasCode == 'G')
        printf ("\nGreen - Oxygen\n");
    else if (gasCode == 'Y')
        printf ("\nYellow - Hydrogen\n");
    else
        printf ("\nInvalid Character\n");

    return 0;
}
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC