Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SkippyThorson

macrumors 68000
Original poster
Jul 22, 2007
1,705
1,010
Utica, NY
I tried to stay away as long as possible, but to no avail. :rolleyes:

This time, it's a problem where a user enters a letter (b,g,o,y) and is given a return statement depending on the letter (char) entered.

Code:
#include <stdio.h>
int main(void)

{
	//variables
	char answer = 'x';
	
	//input
	printf ("Welcome.\n");
	printf("Enter the first letter of the cylinder's color: \n");
	scanf("%d", &answer);
	
	//output based on the users input
	//outcomes depend on the letter entered
	switch(answer)
	{
		case 'b': case 'B': printf("The content is carbon monoxide.\n");
			break;
		case 'g': case 'G': printf("The content is oxygen.\n");
			break;
		case 'o': case 'O': printf("The content is ammonia.\n");
			break;
		case 'y': case 'Y': printf("The content is hydrogen.\n");
			break;
		default: printf("The content(s) of the cylinder is unknown.\n");
			break;
	}
	
	//end
	printf("Thank you. \n");
	return 0;
}

The user is prompted for a letter, and the program takes in to consideration a lower or upper case. If the letter entered does not match any case, an unknown is returned - a match returns it's corresponding statement.

My problem today is that no matter what, it returns the default - unless I change the 'x' to one of the 4 letters. Any hints are appreciated. :) Thank you.
 
Why are you using %d as your scanf format? %d is for an integer as a signed decimal number. You should use the correct format for a character (consult the documentation).
 
One way to debug and help see what is going on in situations like this is to add some code to print out the values, particularly in the "unknown" case.

So, instead of simply printing "The content of the cylinder is unknown", you should print out your variable "answer" as well. So it might print "No match for Q. The content of the cylinder is unknown".
 
Really!? Since the class began, I don't believe she's mentioned anything other than %d. I'll read up. :) Hard to believe she missed it, I must have.

-- and that's a really good idea I'll keep in mind. However, our instructions include using just what is in the code, so I'm gonna play it safe on this one.

Thanks again folks! :)
 
One minute makes all the difference. Everyone seems to troll around trying to one up each other solving all the problems :p

Sometimes it's easier to lurk, waiting for the wrong answers...

-- and that's a really good idea I'll keep in mind. However, our instructions include using just what is in the code, so I'm gonna play it safe on this one.

You don't have to hand in every speck of code you write. You can add something to help you understand or debug it, then take it out in order to meet the specification, er, I mean assignment.
 
You don't have to hand in every speck of code you write. You can add something to help you understand or debug it, then take it out in order to meet the specification, er, I mean assignment.

I do that - I turn in the program, and jot down everything else in documentation at the bottom after. If I didn't, it would be put in a box in the back of my mind, and then lost in a closet. Or something close.
 
That's good. Don't be afraid to play around, try different techniques, or print extra things out so you get an idea of what's going on. When it's time to hand it in, stick to what your assignment dictates and use the methods suggested by the teacher, but on your own time, you'll learn a lot by playing around just for fun (and conversely, not being afraid to rip things apart when you are debugging).

At work I have become largely responsible for a program that contains about half a million lines of code. Hundreds of classes, dozens of processes running concurrently across 3 CPU boards. Even with the tools I have available sometimes I still need to get in there and print "the value of X is ___" so I can keep track of what's going on. Obviously those statements get removed once I find the problem and fix it!
 
I had to laugh when I read "not being afraid to rip things apart" - that's my big thrill with the things that worked. I love taking something that's just fine, absolutely demolishing it, and then rebuilding it.

Regarding the 500,000 lines... ~kneel/bow~

Regarding the format specifiers, I found this handy site that covered some basics, and includes a table with them. My gears; they spin.
 
I had to laugh when I read "not being afraid to rip things apart" - that's my big thrill with the things that worked. I love taking something that's just fine, absolutely demolishing it, and then rebuilding it.

Regarding the 500,000 lines... ~kneel/bow~

Regarding the format specifiers, I found this handy site that covered some basics, and includes a table with them. My gears; they spin.

Do you have a programming book? It should have a reference section at the back.
 
The problem with using "%d" and a char is that "%d" tells scanf that the buffer provided is 32 bits, but a char is only 8 bits. Therefore, you have a potentially stack-smashing buffer overflow. Furthermore, it tells scanf to expect an integer, which letters are not, so it moves on and doesn't change the input buffer (which is probably part of why your program doesn't crash and is affected most by the initial value).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.