Question!

How do I declare str=testCharacters?
Line 17
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
#include <iostream>
using namespace std;

void askCharacter(char c);
void testCharacter(char c);

int main(){


char c;

//ask the user for a single character
askCharacter(c);

//identify if the character is a vowel, a consonant, a digit, a symbol
//and return the string "is a vowel", "is a consonant", "is a digit", or "is a symbol"
str = testCharacter(c);

cout << c << str << endl;
return 0;
}

void askCharacter(char c){
	cout << "Type a single character: ";
	cin >> c;
}

void testCharacter(char c){
	if(c == 1 && c == 2 && c == 3 && c == 4 && c == 5 && c == 6 &&
	 c == 7 && c == 8 && c == 9 && c == 0)
	 cout << c << " is a digit";
}
Last edited on
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.
}
Last edited on
ok thank you its working now!
Topic archived. No new replies allowed.