Char to a string? Need advice.

Hi, i'm working on this assignment, and I can't figure out how to turn the char 'm' to a string called 'Male', of course i would need to do this for Female as well. Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define CHILD_H
class Child
{

public:
	Child();
	Child(string, int, char);
	string getName()const;
	void setName(string);
	int getAge()const;
	void setAge(int);
	char getGender();
	void setGender(char);
	bool getInSchool();
	void setInSchool(int);

private:
	string newName;
	int newAge;
	char newGender;
	bool inSchool;
};
#endif 

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
35
36
37
38
#include "Child.h"
Child::Child()
{
}
Child::Child(string name, int age, char gender)
{
	newName = name;
	newAge = age;
	newGender = gender;
}
string Child::getName() const
{
	return newName;
}
int Child::getAge() const
{
	return newAge;
}
char Child::getGender()
{
	return newGender;
}
bool Child::getInSchool()
{
	return inSchool;
}
void Child::setName(string name)
{
	newName = name;
}
void Child::setAge(int age)
{
	newAge = age;
}
void Child::setGender(char gender)
{
	newGender = gender;
}

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>
#include<string>
#include "Child.h"
using namespace std;
	
int main()
{
	string name;
	int age;
	char gender;

	cout << "Enter the child's Name: ";
	cin >> name;
	cout << "Enter the child's Age: ";
	cin >> age;
	cout << "Enter the child's gender: ";
	cin >> gender;


	Child Info_1;

	Info_1.setName(name);
	Info_1.setAge(age);
	Info_1.setGender(gender);

	cout << Info_1.getName() << " is a " << Info_1.getAge() << " year-old " << Info_1.getGender() 
	<< " who is in school" << endl;

	
	
	system("pause");
	return 0;
}
23
24
25
26
27
28
29
30
31
32
33
34
35
switch(gender){
   case 'm':
   case 'M':
      Info_1.setGender("Male");
      break;
   case 'f':
   case 'F':
      Info_1.setGender("Female");
      break;
   default:
      cout << "Invalid gender." << endl;
      break;
};


Why can't you have the user enter in "male" or "female" instead of just a char?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
switch(gender){
   case 'm':
   case 'M':
      Info_1.setGender("Male");
      break;
   case 'f':
   case 'F':
      Info_1.setGender("Female");
      break;
   default:
      cout << "Invalid gender." << endl;
      break;
};


Thanks for the response. But it's giving me an error. "const char is incompatible with char"
Last edited on
Oh, woops. Forget my last post. I think I misinterpreted where you needed to convert char to string.

You wanted to do the conversion for the output, right?
In that case, you can change the switch-case to output the string instead of setting the gender.
Oh, woops. Forget my last post. I think I misinterpreted where you needed to convert char to string.

You wanted to do the conversion for the output, right?
In that case, you can change the switch-case to output the string instead of setting the gender.


Hi, I tried it this way too. Do you see anything wrong? I am just wondering if my way would work too.
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>
#include<string>
#include "Child.h"
using namespace std;
	
int main()
{
	string name;
	int age;
	char gender;

	cout << "Enter the child's Name: ";
	cin >> name;
	cout << "Enter the child's Age: ";
	cin >> age;
	cout << "Enter the child's gender: ";
	cin >> gender;

	Child Info_1;

	Info_1.setName(name);
	Info_1.setAge(age);
	Info_1.setGender(gender);
	
	if (Info_1.getGender() == 'm')
	string genderString  = "Male";
	
	cout << Info_1.getName() << " is a " << Info_1.getAge() << " year-old " << Info_1.getGender() 
	<< " who is in school" << endl;

	system("pause");
	return 0;
}
Only problem I see there is your genderString only exists within that if statement. You won't be able to use it anywhere else.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (Info_1.getGender() == 'm')
   string genderString  = "Male";
//Is equivalent to
if (Info_1.getGender() == 'm'){
   string genderString  = "Male";
}

//Alternative
string genderString("");
if (Info_1.getGender() == 'm')
   genderString  = "Male";
else genderString = "Female";

//Alternative with the ternary operator
string genderString(Info_1.getGender() == 'm' ? "Male" : "Female");
Topic archived. No new replies allowed.