Problem with Text Based RPG

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <stdlib.h>

using namespace std;

int input_int, attack, defense, intelligence, level, xp, max_health, health, enemy_int, enemy_level, damage;
string input_string, username, userclass, enemy_string;


int char_create()
{
	cout << "Hello! What's your name?" << endl;
	cout << ">";
	getline(cin, username);
	cout << "Hello " << username << endl;
	cout << "What class are you?" << endl;
	cout << "1: Warrior (High attack and defense and health)" << endl;
	cout << "2: Archer (Well rounded)" << endl;
	cout << "3: Mage (High intelligence, low health)" << endl;
	cout << ">";
	cin >> input_int;
		switch(input_int)
		{
			case 1:
				userclass = "Warrior";
				attack = 10;
				defense = 10;
				intelligence = 1;
				max_health = 150;
				break;
			case 2:
				userclass = "Archer";
				attack = 7;
				defense = 7;
				intelligence = 2;
				max_health = 125;
				break;
			case 3:
				userclass = "Mage";
				attack = 5;
				defense = 5;
				intelligence = 3;
				max_health = 100;
				break;
		}
		level = 1;
		xp = 0;
		health = max_health;
		cout << "Level: " << level << endl;
		cout << "Health: " << max_health << "/" << health << endl;
		cout << "Attack: " << attack << endl;
		cout << "Defense: " << defense << endl;
		cout << "Intelligence: " << intelligence << endl;
		cin.ignore();
	ofstream character_data((username + ".txt").c_str());
	character_data << username << endl;
	character_data << "Level " << level << " ";
	character_data << userclass << endl;
	character_data << "Health: " << health << "/" << max_health << endl;
	character_data << "Attack: " << attack << endl;
	character_data << "Defense: " << defense << endl;
	character_data << "Intelligence: " << intelligence << endl;
	return 0;
}
int show_menu()
{
	cout << "--Your Turn!--" << endl;
	cout << "1: Go to War!" << endl;

	return 0;
}
int game_save()
{
	ofstream character_data((username + ".txt").c_str());
	character_data << username << endl;
	character_data << "Level " << level << " ";
	character_data << userclass << endl;
	character_data << "Health: " << health << "/" << max_health << endl;
	character_data << "Attack: " << attack << endl;
	character_data << "Defense: " << defense << endl;
	character_data << "Intelligence: " << intelligence << endl;
	return 0;
}
int goto_war()
{
	srand(unsigned(time(NULL)));
	enemy_int = rand() % 5 + 1;
	
	switch (enemy_int)
	{
	case 1:
		enemy_string = "Imp";
		enemy_level = 1;
	case 2:
		enemy_string = "Goblin";
		enemy_level = 2;
	case 3:
		enemy_string = "Zombie";
		enemy_level = 3;
	case 4:
		enemy_string = "Vampire";
		enemy_level = 4;
	case 5:
		enemy_string = "Demon";
		enemy_level = 5;
	}
	damage = level + (rand() % attack);
	if (damage >= enemy_level)
	{
		xp = xp + ((rand() % 10 + 1) * intelligence);
		cout << "You slayed a(n) " << enemy_string << endl;
	}
	else
	{
		health = (health - enemy_level) - (rand() % defense);
		cout << "You were attacked by a(n) " << enemy_string << endl;
	}
	cout << "Health: " << health << endl;
	cout << "Level: " << level << endl;
	cout << "XP: " << xp << "/100" << endl;
	cin.ignore();
	return 0;
}
int main()
{
	char_create();
	cout << "Character successfully created!" << endl;

	while (health >> 0)
	{
	show_menu();
	cin >> input_int;
		switch(input_int)
		{
		case 1:
			goto_war();
		}
	if (xp >= 100)
	{
	level++;
	}
	if (health <= 0)
	{
		break;
	}
	game_save();
	cin.ignore();
	}
	
	cin.ignore();
	return 0;
}


It always rolls 5. I even tried moving the srand(unsigned(time(NULL))); to the main function, still the same result. I see why it's doing it. It's because I'm calling the srand() over and over again, but how do I fix it?
Last edited on
Fixed it. I just moved everything for the goto_war(); function to the case 1 in the place of the goto_war(); function itself. If anyone has a better fix please tell me.

Nevermind. It still doesn't work...
Last edited on
srand should only be called once. However, your issue is with the switch statement. Add a print statement to each case and see if it's doing what you think it is.
kev82 is right. It is your switch statement in the goto_war function. I'll just come right out and tell you what it is. You need to add a break; to the end of each case. The way you have it set up now, it just starts with the case that was passed into it, then it goes down the list all the way until 5, which sets enemy_string to Demon finally.
Last edited on
Topic archived. No new replies allowed.