Random Character Program Assignment!

You must follow these requirements exactly.
Your program must loop, asking for character names that may contain spaces, until the
word "quit" is entered. Each name should be added to a vector. Once “quit” is entered,
loop through the names vector and print out the stats.
There are three attributes of a DnD character for which you will write specific functions.
These are the signatures.
void PrintCharacterStats(string name);
string ChooseRace();
string ChooseAlignment();
string ChooseBehavior();
Each chooses one value from an array of predefined values at random.
string ChooseRace();
can return one of:
● Human
● Orc
● Elf
● Dwarf
● Gnome
string ChooseAlignment();
can return one of:
● Lawful
● Neutral
● Chaotic
string ChooseBehavior();
can return one of:
● Good
● Neutral
● Evil
You must write a function returning a random number within a give range. It will have
the signature:
int RangeRand(int min, int max);
It will return a random integer whose minimum value is specified as the first argument.
Its maximum value is one less than the second argument. For example RangeRand(0, 10)
returns a random integer from 0 to (including) 9. The mod operator % is important here.
You must write a function return a "stat". Stats can range between 6 and 18 inclusive.
The highest (18) is special. If you roll an 18 (choose 18 at random) the character gets a
special massive boost which comes in the form of a number between 0 and 100
inclusive. Because of this, your function returns a string - not a number.
string PickStat();
See the sample output for an example of non-18 values and one special 18 value. You
will choose stats for:
● Strength
● Dexterity
● Intelligence
● Charisma
● Wisdom
Health is different - it ranges from 6 to 30 inclusive.
As a hint, here are the include files I used when writing my implementation:
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
Style: Sometimes you can have dozens of includes. If they are in an order, they are
easier to glance through.
void PrintCharacterStats(string name);
Should Print out one characters stats following the format in the sample output.
Sample output
Enter character's name - use "quit" to exit: Hector the Horrible
Enter character's name - use "quit" to exit: Furry Curry Murray
Enter character's name - use "quit" to exit: quit
Hector the Horrible (Elf) is Neutral alignment and Neutral behavior
Health: 15
Strength: 8
Dexterity: 13
Intelligence: 7
Charisma: 16
Wisdom: 6
Enter character's name - use "quit" to exit:
Furry Curry Murray (Orc) is Lawful alignment and Evil behavior
Health: 17
Strength: 17
Dexterity: 9
Intelligence: 9
Charisma: 8
Wisdom: 18 / 74

This is the context of the assignment.. this is my code so far

#include <iostream>
#include <ctime>



int SelectRandomm(int min, int max);
void PrintNum(int num);


int main()
{
vector <string> alignments = "Lawful", "Neutral", "Chaotic", 3

double min;
double max;

int random;
srand(time(0));
random = SelectRandom (min, max);

return 0;
}

int SelectRandom(int min, int max)
{
int retVal;
retVal = (rand() % (max - min)) + min;

return retVal;
}

I'm not sure if this is the right place to start or if this is correct! If someone could lead me in the right direction that would be great!!
its fine so far. keep going? Maybe consider *testing* as you go?
does your random function do what the requirement says (via testing, not 'I looked at it')?
Hello bethmayweather,

I think I may have said the once before:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



As jonnin says you have a good start, but when I tried to compile the program I did find some problems.

For the moment and with a little guessing I have done this with the program:
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>  // <--- Added.
#include <vector>  // <--- Added.

#include <ctime>

int SelectRandom(int min, int max);  // <--- Check spelling.
void PrintNum(int num);

int main()
{
	std::vector <std::string> alignments{ "Lawful", "Neutral", "Chaotic", "3" };  // <--- Changed.

	constexpr int MIN{ 0 };
	constexpr int MAX{ 100 };

	int random{};

	//srand(time(0));
	srand(static_cast<size_t>(time(nullptr)));

	random = SelectRandom(MIN, MAX);

	return 0;
}

int SelectRandom(int min, int max)
{
	int retVal;
	retVal = (rand() % (max - min)) + min;

	return retVal;
}

I had to add the two header files for line 12, but they are likely to be needed for later code especially the "Print" function.

Since you do not have the line: using namespace std;, which I would redo as:
1
2
//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/ 
, I had to add the "std::".

Line 12 as you have written it produces an error. The number 3 should be in double quotes because the vector is a vector of "std::string"s and all the strings should be inside {}s.

I do not know why you defined "min" and "max" as "doubles" when the function you pass it to receives them as "int"s. This is worth at least a warning of "possible data loss" when you try to stuff a larger "double" into a smaller "int". The "int" will only store the whole number and drop the decimal part. Unless you have some idea later in the program to change the value of "MIN" and "MAX" I would consider making them constant variables. I was only guessing and showing an example for the values of "MIN" and "MAX". You may want to change them before you really test the program.

Your "srand" works, but what I changes is a better way to write this line. I was once directed to this video about why "rand" is bad. It is worth watching at least for a better understanding of the way "rand" works. https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
The header files "chrono" and "random" are better for producing random numbers.

When you pick it apart "srand" takes an unsigned int, AKA size_t, as its seed, but time will return either a struct with time information or a number that is not an unsigned int. Although senting "time" either "0" zero or "NULL" will work I once read somewhere the "nullptr" is the best choice for returning a number from "time".

Other than not knowing what values to use for "MIN" and "MAX" the function should work the way you want.

Hope that helps,

Andy
#include <iostream>
#include <string>
#include <vector>

#include <ctime>
using namespace std;

int SelectRandom(int min, int max);
void PrintNum(int num);

void PrintCharacterStats(string name);
string ChooseRace();
string ChooseAlignment();
string ChooseBehavior();

int main()
{
std::vector <std::string> alignments{ "Lawful", "Neutral", "Chaotic", "3" }; // <--- Changed.

constexpr int MIN{ 0 };
constexpr int MAX{ 100 };

int random{};

//srand(time(0));
srand(static_cast<size_t>(time(nullptr)));

random = SelectRandom(MIN, MAX);

return 0;
}

int SelectRandom(int min, int max)
{
int retVal;
retVal = (rand() % (max - min)) + min;

return retVal;



}

string ChooseRace()
{
retVal = races[SelectRandom(, );

return retVal;
}

string ChooseAlignment()
{
retVal = Alignment[SelectRandom(, ); ]

return retVal;
}

string ChooseBehavior();
{
retVal = Behavior[SelectRandom(, ); ]
}





Thank you! This is what I have so far, but I'm not sure where to go from here and I know there are errors.
Hello bethmayweather,

I moved lines 20 and 21 to above the prototypes so that all the functions have access to them.

Line 8 no longer needs any parameters.

Lines 12, 13 and 14 are missing a parameter. You define vectors in "main" and then try to access them in the functions, but when "main" looses scope in favor of a function the function is not able to see the variables defined in "main".

Line 28 has no real use other than generating a random number that you never use. At this point I have no clue what you might use it for.

Line 33 no longer needs any parameters because of the global variables "MIN" and "MAX". Also you can get rid of the extra blank lines in that function.

The functions starting at line 44 all need to be finished. For the first two each has a syntax error.

Line 60 is the closest to being correct, but the whole function is missing some code or you could write the line as: return Behavior[SelectRandom()];. But your code still has a syntax error.

Hope that helps,

Andy

Edit:

P.S. you could write your vectors like this:
1
2
3
4
5
6
7
std::vector <std::string> alignments
{
	"Lawful",
	"Neutral",
	"Chaotic",
	"3"
}; // <--- Changed. 

You may find this a little easier to read and/or add to.

I gave "MIN" a value of 1 and "MAX" a value of 4 because the two I used each have 4 elements, but the way line 36 is written it will never return a value of (0) zero, so you always miss the first element of the vector.
Last edited on
Hello bethmayweather,

As I was finally reading the specks, or directions, for the program I now have a better idea of what you need to do.

I find breaking up the specks helps to see the different parts you need to work on:

You must follow these requirements exactly.

Your program must loop, asking for character names that may contain spaces, until the
word "quit" is entered.

Each name should be added to a vector.

Once “quit” is entered, loop through the names vector and print out the stats.

There are three attributes of a DnD character for which you will write specific functions.
These are the signatures.
 ● void PrintCharacterStats(string name);
 ● string ChooseRace();
 ● string ChooseAlignment();
 ● string ChooseBehavior();

Each chooses one value from an array of predefined values at random.

string ChooseRace();
can return one of:
 ● Human
 ● Orc
 ● Elf
 ● Dwarf
 ● Gnome

string ChooseAlignment();
can return one of:
 ● Lawful
 ● Neutral
 ● Chaotic

string ChooseBehavior();
can return one of:
 ● Good
 ● Neutral
 ● Evil

You must write a function returning a random number within a give range. It will have
the signature:
int RangeRand(int min, int max);

It will return a random integer whose minimum value is specified as the first argument.

Its maximum value is one less than the second argument. For example RangeRand(0, 10)
returns a random integer from 0 to (including) 9.

The mod operator % is important here.

You must write a function return a "stat".
Stats can range between 6 and 18 inclusive.
The highest (18) is special.

If you roll an 18 (choose 18 at random) the character gets a
special massive boost which comes in the form of a number between 0 and 100
inclusive. Because of this, your function returns a string - not a number.
string PickStat();
See the sample output for an example of non-18 values and one special 18 value.

You will choose stats for:
 ● Strength
 ● Dexterity
 ● Intelligence
 ● Charisma
 ● Wisdom

Health is different - it ranges from 6 to 30 inclusive.

As a hint, here are the include files I used when writing my implementation:
#include <ctime>
#include <iostream>
#include <string>
#include <vector>

Style: Sometimes you can have dozens of includes. If they are in an order, they are
easier to glance through.

void PrintCharacterStats(string name);
Should Print out one characters stats following the format in the sample output.
Sample output
Enter character's name - use "quit" to exit: Hector the Horrible
Enter character's name - use "quit" to exit: Furry Curry Murray
Enter character's name - use "quit" to exit: quit

Hector the Horrible (Elf) is Neutral alignment and Neutral behavior
Health: 15
Strength: 8
Dexterity: 13
Intelligence: 7
Charisma: 16
Wisdom: 6

Enter character's name - use "quit" to exit:
Furry Curry Murray (Orc) is Lawful alignment and Evil behavior
Health: 17
Strength: 17
Dexterity: 9
Intelligence: 9
Charisma: 8
Wisdom: 18 / 74


To start with I would work on this part first:

Your program must loop, asking for character names that may contain spaces, until the
word "quit" is entered.

Each name should be added to a vector.


I used a do/while loop for this. If this is a problem let me know.

First yo need to create the vector of names to work with before the rest makes any sense.

Hope that helps,

Andy
Thank you. I'm struggling with the loop and set up.. Which should come first the vector of names or the while loop??

Hello bethmayweather,

For something to work with I created the vector of names at the beginning of "main" then later I used a range based for loop to step through the vector calling the "PrintCharacterStats" function.

While working with the "PrintCharacterStats" function i realized the other functions that needed to be created and how it all fit together.

If you are still having problems post the code so I can see what you have done and what is there to work with.

Andy
Topic archived. No new replies allowed.