Converting a string input to char

I am writing a program that asks the user for their gender. I know that atoi converts arrays into integers, but I need the input from the user in the form of F or M into a char, and return it to the main function to be displayed at the end of the program.
string Employee::getgender(char gen)
{

cout << "Please enter your Gender: " << endl;
//atoi function would go here, what for char?
getline(cin,gen)
return gen;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>



std::string getgender(char gen)
{
	if (gen == 'f' || gen == 'F')
		return "Female";
	else if (gen == 'm' || gen == 'M')
		return "Male";
	else return "Sorry, but your selection does not fit our definition of a gender";
}


int main(void)
{
	char gender;
	std::cout << "Please enter your gender (m/f): ";
	std::cin >> gender;
	std::cout << getgender(gender) << "\n";

}
Last edited on
Topic archived. No new replies allowed.