Using User Input to Determine the Overall Path of the Program

Good day, Everyone!

For those who may have seen my posts last month: I'm still in the midst of my D&D Character Generator.
While I'm still fiddling with functions separately, and the whole thing is a bit messy, I'm starting to better understand the syntax.

Now currently, the stat roller is very random. The whole thing is based on this:

1
2
srand((unsigned) time(0));
for (int index = 0; index < 1; index++) {


While I have had naysayers on my use of srand, the consistent psuedo-randomness suits the program just fine. More often than not, my numbers are set differently enough per each roll.

However, I have an issue: The numbers are too random sometimes.
In addition to stats, I've also added rolls for Races (including Subraces) and classes.

About a third of the time, I'll get a pretty well optimized build like a strong Orc Barbarian, or an intelligent Elf Wizard. Another third of the time, I'll get something a bit more average and less specialized. In the last third, I'll get something that would be very difficult to play like a very un-charismatic bard or glass-like-fighter.

Now, that's not at all an issue. I've heard, watched, and read documentation about the entertainment that can come from playing a weak or un-optimized character.

So my dilemma is how to give the user of my program a choice.
Before I can add in separate sets of code for adding racial modifiers and allowing the player to choose their race and class (thus only rolling for stats), I need to be able to split the code using an input; yes or no, true or false, etc.

I've determined that I'd likely need to set this up between the int main() and the srand, and somehow, using the input from the cin, skip over the random rolls of race, class, and stats, to another future block of code allowing for more refined choices and results.

Here's the code up to the for (int index...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>

using namespace std;

int main() {
	int che; //Choice for whether the user wants totally random or optimized results
		cout << "Would you like your character randomized?" << endl;
		cin >> che;
		if (che >> true)

		else if (che >> false)
			cout << "This is placeholder text" << endl;
	srand((unsigned) time(0));
	int rcrl; //Race Roll
	for (int index = 0; index < 1; index++) {


After that it's just the possible results from the race roll (rcrl), the definition of the value for and use of the class roll, and then the rolls for the stats and their displays.

This post was an adventure in itself. I'd be happy to read any feedback and assistance rendered. Ahead of time, thank you for any thereof!

Have fun and stay safe!
Last edited on
as a D&D player, here are some things you can do on the generation side:

- roll the stats and hand place them in any order. (human chooses)
- flat (-1 from x to get +1 to y) or penalized (-2 from x to add 1 to y) adjustment/balancing (16 max allowed from this is often used to prevent the 1 int 18 str 18 con moron fighter)
- flat out reroll of hopeless (total score < x, like < 50 or so) (you can hide this and just silently re-roll until it passes)
- die tricks, roll 4 keep highest 1, roll 7 stats to fill in 6, etc.
- computer die tricks, like 6+ 2d6 (min score is now 8, 6+1+1). This is sort of cheating but a lot of people see stats < 10 as not worth playing. Kind of a bad attitude, but its been that way since the original and at 40+ years its not gonna change. One time we did a group with 2d6 stats when we created the chars, just to see. We had to get creative just to stay alive, and it was fun.

As far as the rest, just offer a menu where they select race from a list, class from a list, etc. If you are doing stat adjustments for classes, remember to take off what is there and put on new ones if they change their mind if that is allowed.

Like anything, the first step is to decide what you want to do, and then break the problem down into how you want to do that, little pieces, then code them into it. Think on how it fits with what you have.
Last edited on
@jonnin

Fair Enough.

Although this is only the first fragment of the overall tool. The totally random aspect will be something that the user can choose.
I'm not looking to work on a GUI quite yet, and so I'm merely trying to implement a cin input that'll "switch the code's track" if you will.

The idea is to allow the user to choose either totally random, or a more specified build based on an applied input of race, class, and stat priority. It's to make the process easy enough to type in what they want, and get a build quickly- no muss, no fuss.
The original point of the program was not actually for Players- but for DMs to make NPCs quickly so that they may better focus on other aspects of their preparation.

Personally, I have an interest in DMing not just because it'd be entertaining, but also because it'd help me practice my writing skills for future professional use.

If a player should want to switch their results around contrary to the roller, that's fine. Such leeway is already an important part of any campaign.

As for the stat minimums and maximums, I'm going to make every stat have a minimum of 7 and a maximum of 15 even in the totally randomized section. While the chances are low, a character having a stat of 3 in anything wouldn't be preferable.

As for rolling for stats manually- you must've learned somewhat differently. If it works for you, cool. However, I'll stick to 4D6, removing the lowest result, and adding them to get a stat. While it needs some eventual clean-up: it works, and I'm proud of it.

For now, I'll keep adding aspects of characters such as Backgrounds and Alignments.

However, I still would appreciate some help on my "Switching of the track."

Thank you thus far!
maybe you seek a switch statement.

cout "enter 1 for something, 2 for something else, 3 for yet another"
cin input //this must be an integer type, switch requires it.
1
2
3
4
5
6
7
switch(input)
{
    case 1: function1();  break;
    case 2: function 2(); break;
    ...
    default : cout "invalid input try again"
}


you can do the same with if statements. you just need to set it up... tell the user what they are choosing in a little menu/print statement, get their choice, and based off their choice, do one or another thing.

nothing I said requires a gui vs text I/O.
At the end of the day, the core of a gui is just a pretty presentation of I/O. They do more than this, but that is their underlying original intent. If you plan to add one later, plan ahead, and be careful about embedding cout statements in the does-work parts of the code.
Last edited on
1
2
3
4
5
6
	int che; //Choice for whether the user wants totally random or optimized results
		cout << "Would you like your character randomized?" << endl;
		cin >> che;
		if (che >> true)

		else if (che >> false)

I don't think the underlined expressions do what you think.
che >> true converts true to 1, then shifts che right 1 bit, effectively dividing it by 2. Then it converts the integer result to a boolean: zero->false and non-zero -> true. So if (che >> true) basically says if ((che / 2) != 0)

che >> false converts false to zero and shifts che right by 0 bits, which does nothing. So
else if (che >> false) is the same as else if (che != 0)
Topic archived. No new replies allowed.