Capital Letters ...

This is a discussion on Capital Letters ... within the C++ Programming forums, part of the General Programming Boards category; I know this is a silly question, as with some extra code it is possible to do it, but is ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262

    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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can also use tolower to convert the input character and just check for 'a' (or toupper and check for 'A').

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Excellent, that's exactly what I wanted!! thanks a lot!!

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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 ....

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You should use cin instead of getch() anyway. Mixing C and C++ streams can sometimes, under some compilers, cause trouble.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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??

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    If you use a function that only exists in Windows, it doesn't matter if the code would crash in some weird other OS.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    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
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capital Letters?
    By MiroMage in forum C Programming
    Replies: 8
    Last Post: 11-05-2008, 03:32 PM
  2. strcmp, a problem with capital letters
    By RichardH in forum C Programming
    Replies: 11
    Last Post: 04-23-2007, 08:49 AM
  3. Replies: 2
    Last Post: 11-16-2006, 12:43 PM
  4. How To Make Capital Letter To Small Capital Letter??
    By jason07 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2005, 04:37 PM
  5. check for capital letters
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2002, 09:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21