A program that consist of asking the user to input a letter

Hello , just asking if anyone knows how to
"Indicates whether the character was a vowel, consonant, number, or other/punctuation " in which the user inputs a letter , causing for one of the classification. I am using the ( if and else if) #include iostream , namespace std. I don't know how to set everything up.
http://www.cplusplus.com/reference/cctype/

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
27
28
29
30
bool is_vowel( char c )
{
    // ...
}

char c{};
std::cin >> c;

// letter
if( std::isalpha( c ) ) {
    if( is_vowel( c ) ) {
        // ...
    }
    // consonant
    else {
        // ..
    }
}
// number
else if( std::isdigit( c ) ) {
    // ...
}
// punctuation
else if( std::ispunct( c ) ) {

}
// other
else {

}
Last edited on
define 4 sets: (a) vowels, (b) consonants, (c) numbers, (d) other punctuation
capture user input in a char variable
check which of these 4 sets the input char belongs to - one way could be using std::find, I'm sure there are several others - google it

edit: you'd also have to make case insensitive the vowels and consonants with tolower() or toupper()
Last edited on
sometimes, setup saves a ton of work. An alternative approach:

int x;
unsigned char tbl[256] = {0};
for(x = 'a'; x <= 'z'; x++)
tbl[x] = 'C';

for(x = 'A'; x <= 'Z'; x++)
tbl[x] = 'C';

for(x = '0'; x <= '9'; x++)
tbl[x] = 'N';

tbl['a'] = 'V';tbl['A'] = 'V';
tbl['e'] = 'V';tbl['E'] = 'V';
tbl['i'] = 'V';tbl['I'] = 'V';
tbl['o'] = 'V';tbl['O'] = 'V';
tbl['u'] = 'V';tbl['U'] = 'V';

....
unsigned char unknownletter;
...
if(tbl[unknownletter] == 'V') cout << "vowel"
... repeat for C (consonant), N (number) and else (other)

This is very, very, very fast if you do it right. We use some code like this to patternize data from a *large* database for data analysis and profiling. It has more categories, but this is the guts of it.

I am short on explains for a bit, see if you can understand what I did. If not, Ill explain it tomorrow.




Last edited on
I'm so sorry , I thought I get notifications from my email that I got a reply in my topic. I really appreciate that you help me jonnin , gunnerfunner, and integralfx. Thank you
I thought I get notifications from my email that I got a reply in my topic

for that you'd have to turn on the tracking options from the drop-down box just underneath
Topic archived. No new replies allowed.