Help with question, I cant get the program working

Hey,

Can you please help me with this question, I'm a little lost

Write a C++ program with a case structure. The program reads in a character from the user, then display its translation according to the following rules:
characters A, E, I, O, U will be translated to characters a, e, i, o, u respectively;
the space character ' ' will be translated to underscore '_'
digits 0, 1, 2, .., 9 will all be translated to '#'
all other characters remain unchanged


Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream.h>
#include <conio.h>

int main()
{
  char ch;
  while (true) {
    cout << "enter a character: ";
    ch=getche();
    cout << endl;
    switch (ch) {
      case 'A': case 'E': case 'I': case 'O': case 'U':
        ch+=32;
        break;
      case ' ':
        ch='_';
        break;
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        ch='#';
        break;
    }
    cout << "translation: " << ch << endl;
  }
  return 0;
}


In the part that handles vowels, you may notice the line "ch+=32;" . What this does is adds 32 to the ASCII value of the character, which happens to be the amount needed to switch an alphabet letter from uppercase to lowercase.
Another was would be to use the "tolower()" function as demonstrated in this link: http://www.cplusplus.com/reference/clibrary/cctype/tolower.html

~psault
Cheers, thanks 4 that
Make sure you have a default case in the switch. E,g, add this at the end:
1
2
default:
  break; // leave ch unchanged 
BTW: This is depricated MS-DOS code:
1
2
#include <iostream.h>
#include <conio.h> 


You should use only <iostream> (without the .h), and replace getche() with:
1
2
3
4
int ch = cin.get();
// or
char ch;
cin >> ch;

See http://cplusplus.com/reference/iostream/istream/
Topic archived. No new replies allowed.