![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 1
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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).
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
use #include <ctype.h>
and do... gasType = tolower(gasType); and it'll convert it to lowercase making it easier to evaluate ![]()
__________________
|
|
|
|
|
|
#6 | |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Quote:
__________________
"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 |
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
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;
}
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|