problem on switch

This is a discussion on problem on switch within the C Programming forums, part of the General Programming Boards category; i am writing a menu by using switch
however it is not successful
i typed the option 1; but it ...

problem on switch

i am writing a menu by using switch
however it is not successful
i typed the option 1; but it seems it could not recognize and output [Please enter again]. Then i can't input anything, the screen flashed away...

also is it a bit clumsy for the code?

besides, i have one more question
i've attached a printscreen,
i would like to know how can i make the words printed in center? [green circle]

You're reading options as an int and then comparing it to the character representations of numbers. This is wrong. Either read it as a char and compare it as you're doing, or leave the int as it is and change the comparison to compare to numbers. I would do the latter.

Hint: 1 is 1. '1' is the character representation of 1, which is 49 in ASCII.

however, i found that if i type in a character
it output " u choose 2"
why?

This is what we call "undefined behaviour". The switch statement branch to "case 2:" because the value of option was 2 at the time of the test. Why it was 2 ? Well it could have been anything else. If you try reading a number with scanf but the user enter something else, then nothing happens to your "storing variable".

If you want the user to be able only to enter numbers, then this little piece of code will help you:

This is what we call "undefined behaviour". The switch statement branch to "case 2:" because the value of option was 2 at the time of the test. Why it was 2 ? Well it could have been anything else. If you try reading a number with scanf but the user enter something else, then nothing happens to your "storing variable".

If you want the user to be able only to enter numbers, then this little piece of code will help you:

yes, i can really prevent the characters with your code
however if i type 7 (which is out of 1-6), it shows please enter again,
then no matter what i input, the exe. window close itself
it there something wrong with my switch ?

I also have some word of advice for you...
First, main should explicitly return int. main() is bad, int main() is correct.
Second, it's not recommended to mix tabs and spaces. It will mess up formatting when you copy it out of your editor/IDE. So use one and stick with it.
Work a little on the indentation. Each block should be indented once. So all the code inside each { and } should get another indent from the code outside.
And while we're at it, keep indentation consistent. Use only X number of tabs or X number of spaces everywhere to indent one level. 1 tab or 4 spaces are recommended.