# Capital Letters ...

• 01-10-2006
twomers
Capital Letters ...
I know this is a silly question, as with some extra code it is possible to do it, but is there anyway way to assure should someone access your menu function or whatever, and they enter 'A' instead of 'a' that the computer will bring them to the correct destination, or at least the one they wished to access?? I know I could do something like:

Code:

```switch (choice) { case 'a': case 'A':   //do whatever   break; case 'b': case 'B':   //do whatever B wishes you to do }```
but I was hoping there was a more eloquant way to do it.
• 01-10-2006
Daved
You can also use tolower to convert the input character and just check for 'a' (or toupper and check for 'A').
• 01-10-2006
twomers
Excellent, that's exactly what I wanted!! thanks a lot!!
• 01-10-2006
twomers
Does

Code:

```                int ivariable;         cout << "Enter a number\nNumber: ";         ivariable = getch();         cout<< ivariable                 << "\n\n";         char cvariable;         cout << "Enter a character\nCharacter: ";         cvariable = getch();         cout<< cvariable                 << "\n\n";```
make a cast from an int to a char??? This is a really simple example, if you put in 1 for both occasions, notice it prints 49 for the first and 1 for the second ....
• 01-10-2006
Daved
Every character has a numeric code associated with it. The character '1' has the code 49. So getch returns 49 when you type in '1'. It stores 49 in both ivariable and cvariable. The difference is that when you output an int, the int value is output, so when ivariable is output, 49 is printed. When you output a char variable, the character associated with the stored code is printed. That means that since the code 49 refers to the character '1', the second cout prints '1'.

The same thing would happen if you typed a instead of 1. '1' is a character just like 'a'. Only a different code would be printed.

If you wanted to read in the actual number you typed as a number, then either don't use getch(), since getch() reads in a character, or convert the character(s) you get from getch() into the equivalent integer.
• 01-10-2006
CornedBee
You should use cin instead of getch() anyway. Mixing C and C++ streams can sometimes, under some compilers, cause trouble.
• 01-10-2006
twomers
Quote:

Originally Posted by CornedBee
You should use cin instead of getch() anyway. Mixing C and C++ streams can sometimes, under some compilers, cause trouble.

But I wanted the nice feature of variable = getch(); in that you don't have to press return ... there probably is a C++ way to do it, does anyone know how??
• 01-10-2006
CornedBee
No C++ way to do it. The fact that you have to hit enter has to do with the console itself, not the language. getch() calls into the WinAPI to deactivate the console's waiting for enter before forwarding the keys to your program. But it's non-standard, and there's no standard way of doing this.

Come to think of it, that makes the whole non-portability issue of mixing streams a non-issue.
• 01-10-2006
twomers
Quote:

Originally Posted by CornedBee
Come to think of it, that makes the whole non-portability issue of mixing streams a non-issue.

....... sure it does ....... ?? what's that mean?
• 01-10-2006
CornedBee
If you use a function that only exists in Windows, it doesn't matter if the code would crash in some weird other OS.
• 01-10-2006
dwks
Quote:

Originally Posted by twomers
Does

Code:

```                int ivariable;         cout << "Enter a number\nNumber: ";         ivariable = getch();         cout<< ivariable                 << "\n\n";         char cvariable;         cout << "Enter a character\nCharacter: ";         cvariable = getch();         cout<< cvariable                 << "\n\n";```
make a cast from an int to a char??? This is a really simple example, if you put in 1 for both occasions, notice it prints 49 for the first and 1 for the second ....

Well, since cvariable is an int, printing it will print an int. If you want to print a character, cast it to char.

Unbuffered input: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
• 01-11-2006
twomers
Quote:

Originally Posted by dwks
Well, since cvariable is an int, printing it will print an int. If you want to print a character, cast it to char.

Unbuffered input: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

Well cvariable is a char ....

Code:

`char cvariable;`
right ... ? c for char, i for int ... I'm sure there's a naming system for that kind of naming isn't there? I dunno, it doesn't matter all that much anyways, only a minor difficulty.