This code is completely wrong for what you are attempting. You are reading input as an int
eger instead of as a char
acter. You are not initializing the vowel
and consonant
variables correctly, and not comparing var
to them correctly. You are not checking for input errors. You are not handling upper-case letters.
Try something more like this instead instead:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
cout << "input a single letter";
char var;
if (cin >> var) {
var = (char) ::tolower( (unsigned char)var );
if ((var >= 'a') && (var <= 'z')) {
if ((var == 'a') || (var == 'e') || (var == 'i') || (var == 'o') || (var == 'u')) {
cout << "vowel";
} else {
cout << "consonant";
}
/* alternatively:
switch (var) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << "vowel";
break;
default:
cout << "consonant";
break;
}
*/
}
else {
cout << "Error";
}
}
else {
cout << "Input Error";
}
return 0;
}
Alternatively:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
const string vowels = "aeiou";
const string consonants = "bcdfghjklmnpqrstvwxyz";
int main() {
cout << "input a single letter";
char var;
if (cin >> var) {
var = (char) ::tolower( (unsigned char)var );
if (vowels.find(var) != string::npos) {
cout << "vowel";
} else if (consonant.find(var) != string::npos) {
cout << "consonant";
} else {
cout << "Error";
}
}
else {
cout << "Input Error";
}
return 0;
}
solved Initializing multiple variables in one line in C++