Can I convert a string to a char?

I'm trying to make several functions in a class. The point of the first function is to get a single character, ensure that the user only typed in one character as well as ensure that it's an allowed character before returning whatever they typed in as a char

This is the code so far:

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
class UserInputHandler {
  char GetSingleCharacter() {
    std::string str;
    char letter;
    int i;
    i = (int)str.length();
    const std::string invalid_char_input_("Failure: Too many characters.");
    const std::string char_not_allowed_("Failure: Invalid character.");
    std::getline(std::cin, str);
    if (std::string::npos == str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
      if (i > 1) {
        std::cout << invalid_char_input_;
        letter = invalid_char_input_;
      } else {
        std::cout << str;
        letter = str;
      }
    } else {
      std::cout << char_not_allowed_;
      letter = char_not_allowed_;
    }
    return letter;
  }
};

int main() {
  UserInputHandler();
  return 0;
}


And this is the compile error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ make
/usr/bin/g++ -Wall -Wextra -g assignment_2.cpp  -o assignment_2
assignment_2.cpp: In member function ‘char UserInputHandler::GetSingleCharacter()’:
assignment_2.cpp:30:16: error: cannot convert ‘const string {aka const std::basic_string<char>}’ to ‘char’ in assignment
         letter = invalid_char_input_;
                ^
assignment_2.cpp:33:16: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char’ in assignment
         letter = str;
                ^
assignment_2.cpp:37:14: error: cannot convert ‘const string {aka const std::basic_string<char>}’ to ‘char’ in assignment
       letter = char_not_allowed_;
              ^
makefile:10: recipe for target 'assignment_2' failed
make: *** [assignment_2] Error 1


I am required to return whatever they typed in as a char, but char is not compatible with ".length()" based on my past attempts, so I need a string. Is there any way I could convert it?
I don't understand why you are trying to perform the assignments on lines 13 and 20 (lines 30 and 37 in your error messages). You're trying to assign error messages to a single character. Huh???

As for line 16 (line 33) if you know the string is at least one character long you can just access str[0]
Last edited on
Well I can't return what they typed if it wasn't valid, but I need to return something. I tried making letter = "N/A", but that didn't work either.
Continue to prompt the user until you get a valid response.
Not allowed to do that. Is there a way to set the char to a blank space, maybe?
CNerd2121 wrote:
The point of the first function is to get a single character, ensure that the user only typed in one character as well as ensure that it's an allowed character before returning whatever they typed in as a char


And how do you want to ensure that? What if they type more than one? What do you want to return then? If you just want a blank space, you certainly could return that, but what's the reasoning behind not prompting until you get valid data here rather than in the code that is using this function?
Topic archived. No new replies allowed.