Switch statement returns

I am working on a school project, and I currently am almost finished the project, but I am having some problems getting an address from the input of a switch statement to another function.

this is the switch statement function:
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
 void Character::getRace() const{

	int choice = 0;
	cout << "Choose your race:" << endl;
	cout << "[1] Human" << endl;
	cout << "[2] Elf" << endl;
	cout << "[3] Dwarf" << endl;
	cout << "[4] Orc" << endl;
	cout << "[5] Halfling" << endl;
	cin >> choice;
	cout << endl;
	
	switch (choice) {
	case HUMAN:
		cout << "Human" << endl;
		break;
	case ELF:
		cout << "Elf" << endl << endl;
		break;
	case DWARF:
		cout << "Dwarf" << endl << endl;
		break;
	case ORC:
		cout << "Orc" << endl << endl;
		break;
	case HALFLING:
		cout << "Halfling" << endl << endl;
		break;
	default:
		return;
	}
}


this is the code for where I want to call the players choice:
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
void traits() {
	string name = "";
	cout << "What is your character's name?" << endl;
	cout << "> ";
	if (!(cin >> name)) {
		cin.clear();
		cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		cout << "Invalid option." << endl;
		return;
	}
	
	system("CLS");

	Character *races = new Character();
	races->getRace();

	Character * job = new Character();
	job->getJob();
	Character * strength = new Character();
	Character * dexterity = new Character();
	Character * constitution = new Character();
	Character * intelligence = new Character();
	Character * willpower = new Character();
	Character * charisma = new Character();
	cout << "Here are your results!" << endl;

	cout << "Name: " << name << endl;
	cout << "Race: " << races << endl;
	cout << "Class: " << job << endl;
	cout << "===========" << endl;
	cout << strength->getStrength() << endl;
	cout << dexterity->getDexterity() << endl;
	cout << constitution->getConstitution() << endl;
	cout << intelligence->getIntelligence() << endl;
	cout << willpower->getWillpower() << endl;
	cout << charisma->getCharisma() << endl;

}


Any idea on how to get the Race that was selected in the cout << "Race: " <<races<< endl;

When it's all said and done it's supposed to read out:

Name: John
Race: Human
Class: Warrior
Strength:
Dexterity:
etc..
Last edited on
Hello SirSkunks,

Welcome to the forum,

In the first bit of code I see nothing wrong at the moment except for HUMAN, ELF etc. I hope that this come from an enum in the public area of the class. If not it will not work because a switch works on an "int" or "char", a type of "int", type.

In the "Traits" function I question if the if statement would even work. Since the "cin" is to a std::string it is hard to make "cin" fail as the string will take anything as far as I know. This works better when "cin" is to a numeric variable and you try to enter something other than a number.

"system("anything")" should be avoided as it does not work on all operating systems and can leave your program vulnerable to attack. See http://www.cplusplus.com/forum/beginner/1988/ for some ideas to pause the program. I like using this: std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread" to put a timed pause in a program.

I am guessing that "Character" is a class? Lines 14 - 24 create several pointers of the class with different names for these pointers, but you never store any information into these pointers before you use them. As an example line 28 the variable "races" is a pointer to the class. If anything would print it would be the address of the pointer. I think what you want here is something like cout << "Race: " << races->getRaces() << endl;. Just guessing at the function name here. Even with that I do not see where you have stored any information under the pointer "races". The same is partially true for the rest of the "cout" statements.

Without the class definition and maybe some more code a lot of this is just guessing based on what I can see.

Hope that is of some help,

Andy
Hi Andy,

Yeah, the Character is a Class, I didn't think it would overly be necessary to include ALL of the code written, but I can do that if it would help you. When I use the cout <<"Race: " <<races->getRace << endl; , it gives me an error under the "<<" before races-> stating that it is 'no operator "<<" matches these operands, operand types are std::basic_ostream<char,std::char_traits<char> <<void.' I'm going to assume that maybe it's because the traits function is a void function. I am mostly just having problems understanding Classes and Pointers, I know how useful they can be but can't seem to wrap my head around it, I've read the Classes and pointers tutorials here but they didn't really make too much sense to me.
Hello SirSkunks,

This is true many times it is not necessary to include all the code, but I do not have the experience that some people he have, so I like to be able to load the code in my IDE and see what error messages I get and be able to run the program to see what happens. For me most times it makes it easier to figure it out.

Your error message looks like it is referring to the "<<" part of a cout statement. Generally when I have this error it is the rhs of "<<" that is the problem and usually means that the header file "<string>" was not included in the file. It may be in some other file, but not in this file. if "races->getRace" returns a string check the header files and make sure string is among them. If not I need to see the class anf the function to see what is there.

Sorry it is late for me and I hope this makes sense,

Hope that helps,

Andy
Topic archived. No new replies allowed.