Writing a good old text based RPG!!

Hey guys,

I'm in the process of writing a text based RPG as a sort of.. coding challenge to myself. I'm trying to give the player the choice to enter his/her name but as soon as one enters his/her name, the entire program is sent into some infinite loop!

I'll put the code down here (either side of the name)!

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
39
40
41
42
43
44
45
46
genderChoice: //Gender loop
	cout<< "\n\nAre you male or female?\n\n 1: Male\n\n 2: Female\n\n";
	cin>> userInput;
	playerInfo[0] = userInput; //Gender

	if (playerInfo[0] == 1) {
		cout<< "\nWelcome, sir!\n\n"; //Male
	}

	else if (playerInfo[0] == 2) {
		cout<< "Welcome, ma'am!\n\n"; //Female
	}

	else {
		cout<< "Well.. you're either male or female....\n\n"; //Input error, takes back to gender loop start
		goto genderChoice;
	}

nameChoice: //Name loop
	cout<< "What is your name?\n\n";
	getline(cin, playerName, '\n');
	cin.get();

	cout<< "Hello, " << playerName << "!\n\n";

classChoice: //Class loop
	cout<< "What class are you?\n\n 1: Figher\n\n 2: Archer\n\n 3: Mage\n\n";
	cin>> userInput;
	playerInfo[1] = userInput; //Class

	if (playerInfo[1] == 1) {
		cout<< "\n\nAhh a fighter! Strong and hardy!\n\n"; //Fighter class
	}

	else if (playerInfo[1] == 2) {
		cout<< "\n\nAhh an archer! Cunning and deadly, from a distance!\n\n"; //Archer class
	}

	else if (playerInfo[1] == 3) {
		cout<< "\n\nAhh the wise mage! Fast witted and deadly with his learned arcane magics!\n\n"; //Mage class
	}

	else {
		cout<< "\n\nPlease choose an existing class!!\n"; //Input error, takes back to class loop start
		goto classChoice;
	}


Sorry for the big code chunk!

Thanks for the help,
Ed
if you are writing code in c++, operate at oop abstraction level - what you created is no different than basic sequential code.

Don't use GOTO in high level languages, but conditional/unconditional loops.

If you have problem with such code, maybe try to finish tutorial on this site.

ps. http://www.cplusplus.com/search.do?q=text+based+RPG

:)
Definitely DO NOT use goto anywhere, at all. Once your game becomes large, the gotos will become extremely confusing and start messing everything up. If I were doing a text based RPG, I would do this (note that these lists will not be exhaustive):

Declare a class which is the main character.
Declare a class for items.
Declare a base class for monsters.
Use inheritance from the base class monster to make all the monsters.

Then I would have functions for various things, such as:

Use item
Fight monster
Movement
View inventory
Maze generation (if you're using a maze).

Then in the main function, first of all call a function to set up the basic stats of the character. After this, give the user the options of movement, fighting (if applicable), viewing inventory etc and call the appropriate functions.

Writing an RPG game is actually quite involved and before you start, make sure you are very familiar with classes, inheritance, functions and various applicable aspects of mathematics.

Cheers for the replies,

I'll use all of this to improve :P At the moment it's literally.. a wall of if statements xD

Thanks again,
Ed
Topic archived. No new replies allowed.