Character choosing

I am having a problem with implementing the choosing of the character with the rest of my code. Instead of being able to choose a character from 1-4 and doing a quick roll of level, it instead makes you do it for every character. Also one other thing how would I add a modifier to the rolldice 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>
#include <fstream>

void rollDice();


class
{

public:
	std:: string char_type;
	int Str;
	int Dex;
	int Con;
	int Int;
	int Wis;
	int Cha;
	int roll;

	void Roll_Die(std::string character)
	{
		char_type = character;
		std:: cout << "\nEnter the number you rolled for the " << char_type << "\n";
		std:: cin >> roll;
	}
	void setStr(int value) //void set can be called out in int main
	{
		Str = value + roll;
	}

	void setDex(int value) //title void set___ is how it is found by anil
	{
		Dex = value + roll;
	}

	void setCon(int value)
	{
		Con = value + roll;
	}

	void setInt(int value)
	{
		Int = value + roll;
	}

	void setWis(int value)
	{
		Wis = value + roll;
	}

	void setCha(int value)
	{
		Cha = value + roll;
	}

	void displayStats(std:: string character)  //in order to cout in must be declared
	{
		std:: cout << "Saving throws for " << char_type << " are \n";
		std :: cout << "Str = " << Str << "\n";
		std:: cout << "Dex = " << Dex << "\n";
		std:: cout << "Con = " << Con << "\n";
		std:: cout << "Int = " << Int << "\n";
		std:: cout << "Wis = " << Wis << "\n";
		std:: cout << "Cha = " << Cha << "\n";

	}
} play_char[4];

int main()
{ 
	srand(time(NULL));
	int nrOfThrows = 0;
	int sizeOfDice = 0;
	int result = 0;
	int total = 0;
	int x;
	

	std::cout << "Welcome to the DND Pick-a-Me: Please choose from the following Predesigned and tested Characters!:" << std::endl << "1. Randal Greyman-Warlock" << std::endl << "2. Roya-Bard" << std::endl << "3. Ogg-Gnar the Cloak-Ranger" << std::endl << "4. Sirella Tanir- Sorcerer" << std::endl;
	
	std:: cin >> x;
	
	
	std::string players[4] = { "Ogg Gnar-Ranger", "Sirella Tanir-Sorcerer", "Roya-Bard", "Warlock-Randal Greyman" };
	int strength[4] = {3, 1, 0, -1}, dexterity[4] = {3, 3, 2, 2}, constiution[4] = {-1, 2, 1, 1}, intelligence[4] = {0, 2, 1, 2}, wisdom[4] = {2, 0, 0, 0},charisma[4] = {1, -1, 3, 3 };
	
	for (x = 0; x < 4; x++)
	{
		play_char[x].Roll_Die(players[x]);
		play_char[x].setStr(strength[x]);
		play_char[x].setDex(dexterity[x]);
		play_char[x].setCon(constiution[x]);
		play_char[x].setInt(intelligence[x]);
		play_char[x].setWis(wisdom[x]);
		play_char[x].setCha(charisma[x]);
		play_char[x].displayStats(players[x]);
	}
	
	
	while (1) 
	{
		
		std::cout << "Welcome to Our Dungeons And Dragons Helpful Rolling Application. (If you are going to roll for advantage or disadvantage please roll two dice.) Please input the action you are doing: " << std::endl << "Run=r" << std::endl << "action=a" << std::endl << "action=a" << std::endl << "check=c" << std::endl << "investigation=i"
			<< std::endl << "cantrip=p" << std::endl << "spell= s" << std::endl << "attack=t" << std::endl;

		std::string input;
		
		std::cin >> input;
		 
		do
		{
			if (input == "r")
			{
				std::cout << "Please tell the DM your movement location." << std::endl;
				input = "a"; 
			}
			else if (input == "c" || input == "i")
			{
				std::cout << "Please tell the DM what you are inspecting: " << std::endl;
				rollDice();
				
			}

			else if (input != "r" || input != "a")
			{
				if (input == "c" || input == "s")
				{
					std::cout << "Please tell the DM which spell or cantrip you are using: " << std::endl;
					rollDice(); 
					
				}



				if (input == "t")
				{
					std::cout << "what arer you waiting for attack!" << std::endl;
					rollDice();
					
				}
				/*
					{if (input == "saving throw")
					{
						std::cout << "what modifier type are you adding to your rolls?" << std::endl;
						std::cin >>
					}
					if (input =="sexy time"
					{
					std::out << "You fail you fucking bard!"
					}
				*/
				
			}
			
		} while (input == "r" || input == "a" || input == "s" || input == "c" || input == "t" || input == "e" || input == "i" || input == "c");
		
	}
	

		return 0;

}

void rollDice()
{
	int nrOfThrows;
	int sizeOfDice;
	int total = 0;

	std::cout << "Please roll your dice for attack: " << std::endl;
	std::cin >> nrOfThrows;


	std::cout << " Please input the size of the die: " << std::endl;
	std::cin >> sizeOfDice;

	for (int i = 0; i < nrOfThrows; i++)
	{
		int result;
		result = rand() % sizeOfDice + 1;
		total = result;

		std::cout << "Total is: " << total << std::endl;
	}
}
class ... }play_char[4]; //this works but it is left over from C 1990s era and before.
just make a variable like normal somewhere useful like inside of main. On top of being archaic and ugly, its also creating a global.

main()
{
classname thing;

for (x = 0; x < 4; x++) line 91 is overriding the choice of x and looping over all of them.
remove this line, and it will just use x, but if x is not 0-3 you will have a crash.
Thank you jonnin I got it working now I have one last question what is the best way to reset the code in the various parts I was thinking of something similar to the rollDice(); but I don't now how to carry out the function of it.
I would create another .cpp folder, and throw everything you have in main into it. Then I would use main to allow you to create another character using a linked list. So when you create one character at the end it gives you the option to create another. I did a similar code like this, and here is what my main looked like.

#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include "roll.h"


using namespace std;

int main()
{

default_random_engine * gptr;
gptr = new default_random_engine(time(NULL));

string answer="yes";
character *party= NULL;
character *new1;


bool cont = true;
cout << "Greetings, \n";


while(cont){


if(answer == "yes" || answer == "Yes"){

if(party==NULL)
{
/// Here is where i create my first charterer

party= new character(gptr);
party->characterClass();
party->Choose_Race();
party->class_Type();
party->showAll();
party->~character();


cout << "\nWould You like to create another character?\n ";
cin>>answer;

}
else{
cin.ignore();

//here is where i create a new character

new1=new character(gptr);
new1->characterClass();
new1->Choose_Race();
new1->class_Type();
new1->~character();
new1 -> SetNext(party);


party=new1;
new1= NULL;
cout << "\nWould You like to create another character?\n ";
cin>>answer;
}
}
else if(answer == "no" || answer == "No"){ party->showAll(); terminate();}
else cout << "Please enter valid response\n";}

party->showAll();return 0;
}
C++ is not Java.

In general, don't explicitly call destructors.
In general, don't say new unless it is necessary.
Pair each new with a corresponding delete.

Prefer nullptr over C's NULL, which is potentially ambiguous due to its implementation-defined nature.
Last edited on
Topic archived. No new replies allowed.