# Problems with a switch

## Recommended Posts

Carolina    176
Hi there! Well,I´m in trouble with college assigments again...I have to program something like a poor version of Excel,dealing with charts and cells containing values and formulae.Everything was going fine until I got to the point where I have to make a switch command,with various cases inside it.I have a char variable named Command that holds the value of any command the user inputs,resulting in chart modificatons. This variable named Command can be operators like +,-,* and /,and I´d like to make a case for when Command is one of these operators. I tried something like this: case '+': { code inside break; } But it didn´t work...However,case 'N',which is one of the possible cases too,works perfectly.what´s the secret here?=) Thanks in advance!

##### Share on other sites
GHertZ    122
i havent tried this out, but take a look at an ascii chart and put in the number for each sign that you want to use.

##### Share on other sites
There's no need to use an ASCII chart, that's just doing work the hard way that the compiler should do for you. When you use '+', it should subsitute the ASCII value of the + character automatically.

Truth be told, Carolina, you didn't give us enough information to help you with this one. switch / case works fine, and based on your wording there could be an infinite number of things going wrong for you.

The basic format should go something like this:

switch(character){case '+':    // code    break;case '-':    // code    break;case '/':    // code    break;default:    // code    break;}

If that's not working for you, it's simply something wrong in your code in a part you didn't describe here.

##### Share on other sites
NoMonkey    290
I suggest you put a break point in before your switch and check the value of Command each time a user presses the key. You may need to do what GHertZ suggested and look up the value you recieve in an ASCII table depending on how you are storing it. If Command is a char, then it should show you the actual letter otherwise, if it is an int, you will get the ASCII value.

Hope this helps!

##### Share on other sites
Carolina    176
Thanks for the help,I solved my problem... But now I have another: I am using a function named getche(),which is very useful because then I don´t need to press <enter> every time I input numbers or characters in my program.I use Dev-C++ compiler and I don´t even need to include libraries other than stdio.h and stdlib.h for it to work:it just does.
However,I need to submit the program to my professor and he uses gcc.Now I discovered that gcc doesn´t recognize getche()! Is there a way I can make gcc recognize it,like a library or something?Or is there a function that works similarly to getche()?

Thanks for everything,

Carol

##### Share on other sites
Zeikstraal    122
Have you tried adding #include <conio.h>?

##### Share on other sites
Carolina    176
Yeah,conio.h works in Dev-C++,but not in gcc...That´s why I´m thinking if maybe there isn´t some library in gcc that supports getche().I was searching at the Net and found there is one named ncurses tha supports getch(),but getch() and getche() aren´t the same...

##### Share on other sites
Zahlman    1682
Have you checked the behaviour of ncurses "getch"? If all it does is input one character and leave it on the screen, then you should be able to "fake it" by using the ncurses getch, then other ncurses functions to go back to the appropriate screen location and output a space. (Just outputting backspace followed by space might do it, too.)