If you want to get a value from
testCharacter, you'll need to give it a return type of something other than
void. You also haven't declared
str. Your
askCharacter function won't work either - again, you're best off giving a return type.
Also, you're probably better off using the functions in
<cctype> to test the character type - its less typing, and you are more likely to avoid the errors you have there (
&& instead of
||). Another error you had was you were checking if the character was a number, rather than a character that just happened to be a number (there is normally a difference).
Here is an example (untested), just ask if you have any problems.
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 31 32 33 34
|
#include <iostream>
#include <string>
#include <cctype>
char askCharacter();
std::string testCharacter(char c); // use std::string over char* - its easier to use
int main() {
char c = askCharacter();
std::string str = testCharacter(c);
std::cout << c << str << std::endl;
return 0;
}
char askCharacter() {
char c;
std::cout << "Type a single character: ";
std::cin >> c;
return c;
}
std::string testCharacter(char c) {
if (isdigit(c))
return " is a digit";
else if (isalpha(c)) {
c = tolower(c); // make it lowercase, so we have less values to check
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return " is a vowel";
else
return " is a consonant";
} else
return " is a symbol"; // catch-all case for anything else.
}
|