How to fix my code to follow the sequence

Pages: 12
currently doing a project for my class to create a program to run off a microcontroller. The program, my classmates and I in our group made was to make a MUI interface for D&D during hostile encounters starting off the with the bare bones currently. This is what I have so far, but what is happening is when I try to run it, it just ends before i can put in the input: What am I doing wrong?
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
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

int main()
{
	srand(time(NULL));

	int nrOfThrows = 0;
	int sizeOfDice = 0;
	int runact = 0;
	int spellorcant = 0;
	int cont = 0; 
	int result = 0;
	int attack = 0;
	int total = 0;
	
	Top: 
	{
		cout << "Please input the action you are doing: ";

		if (cin >> runact)
		{
			cout << "Please tell the DM your movement location.";
			goto Top;
		}

		else if ( cin >> spellorcant)

		{
			cout << "Please tell the DM which spell or cantrip you are using: ";
			cout << "Please roll your dice for attack: ";
			cin >> nrOfThrows;


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

			for (int i = 0; i < nrOfThrows; i++)
			{
				result = rand() % sizeOfDice + 1;
				total += result;
				cout << result << endl;
			}
			cout << "Total is: " << total << endl;
		}

		else if ( cin >> attack)
		{
			cout << "Please roll your dice for attack: ";
			cin >> nrOfThrows;


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

			for (int i = 0; i < nrOfThrows; i++)
			{
				result = rand() % sizeOfDice + 1;
				total += result;
				cout << result << endl;
			}
			cout << "Total is: " << total << endl;
		} 
		
	}

	system("PAUSE");

	return 0; 
Last edited on
What is your input?
The inputs are suppose to be runact, attack, and spellorcant. I tried to do it with numbering but it just ran infinite loop and I didn't like that. What I am trying to do is if the input is one of those three (for now) options, that it will excute what is inside the brackets. I thought an if and else if statement will help me but it seems unlikely.

Last edited on
If you're literally typing in "runact", then that's a string, but you're trying to put a string into an int variable, which fails and puts your input stream in a bad state.

Let's start simple, perhaps you want something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;
int main()
{
    cout << "Enter string: ";
    string input;
    cin >> input;

    if (input == "runact")
    {
        cout << "Doing runact logic...\n";
    }
    else if (input == "attack")
    {
        cout << "Doing attack logic...\n";
    }
    else
    {
        cout << "Unhandled command: " << input << '\n';
    }

}
Last edited on
okay it works now if I wanted to expand the options of in return of the total as result. I rather it be for disadvantage(lowest roll), advantage(highest roll), or straight roll (regular roll) for rolls what should I use:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

else if (input == "attack")
		{
			cout << "Please roll your dice for attack: ";
			cin >> nrOfThrows;


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

			for (int i = 0; i < nrOfThrows; i++)
			{
				result = rand() % sizeOfDice + 1;
				total += result;
				cout << result << endl;
			}
			cout << "Total is: " << total << endl;
		} 

Last edited on
I don't know what your question is.
And please use code formatting. Edit your post and add


[code]

your code

[/code]

This is what I am trying to do
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
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>

using namespace std;

int main()
{
	srand(time(NULL));

	int nrOfThrows = 0;
	int sizeOfDice = 0;
	int result = 0;
	int total = 0;
	

	{
		std::cout << "Please input the action you are doing: " << std::endl;

		std::string input;

		std::cin >> input;

		do
		{
			if (input == "run" or "action")
			{
				std::cout << "Please tell the DM your movement location." << std::endl;
			}

			else if (input =! "run" or "action")
			{
				if (input == "cantrip" or "spell")
				{
					std::cout << "Please tell the DM which spell or cantrip you are using: " << std::endl;
				} 
				
				
			
				if (input == "attack")
				{
					std::cout << "what arer you waiting for attack!" << std::endl; 
				}

				die: 
					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++)
					{
						result = rand() % sizeOfDice + 1;


						total += result;
						std::cout << result << std::endl;

						std::cout << "Total is: " << total << std::endl;
					}
			}

		} while (input == "run" or "action" or "spell" or "cantrip" or "attack");
	}
		return 0;
	
}
	





Last edited on
Your code to calculate the total rolled looks right.

However:

1. You really need to set total = 0 before you calculate it each time. When you get your loop working, you will not want to keep adding to the previous total.

2. You never seeded your random number generator. As a result, you will always get the same result every time you run the function.
Sorry, you did. I missed it.

3. It is not considered good practice to use goto. I doubt you want to end the program after 1 set of dice rolls. You probably want to change line 17 to "while (true)" to give you an infinite loop. You will need to figure out how to break out of the loop to gracefully exit the program, but you can work on that.

4. When you are ready to learn about functions, lines 31 - 45 and 51 - 63 make good candidates to be placed in a common function.
Last edited on
So I tried to do what ya said to fix some of the parts and redo to make it easier, but now every time I try to do a d20 or dice sides 20 it overloads. In addition, how should I do the while loop also how to make the function set for the 31-45 and 51-63?

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

int main()
{
	srand(time(NULL));

	int nrOfThrows = 0;
	int sizeOfDice = 0;
	int result = 0;
	int total = 0;
	

	{
		std::cout << "Please input the action you are doing: " << std::endl;

		std::string input;

		std::cin >> input;

		do
		{
			if (input == "run" or "action")
			{
				std::cout << "Please tell the DM your movement location." << std::endl;
			}

			else if (input == "cantrip" or "spell")
			{
				std::cout << "Please tell the DM which spell or cantrip you are using: " << std::endl;

				{
					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++)
					{
						result = rand() % sizeOfDice + 1;

						
						total += result;
						std::cout << result << std::endl;

						std::cout << "Total is: " << total << std::endl;
					}
				}
			}
			if (input == "attack")
			{


				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++)
				{
					result = rand() % sizeOfDice + 1;

			
					total += result;
					std::cout << result << std::endl;

					std::cout << "Total is: " << total << std::endl;
				}
			}
		} while (input == "run" or "action" or "spell" or "cantrip" or "attack");
	}
		return 0;
	
}
	
	
}
Last edited on
not sure what you are saying but the logic is wrong.
set total = 0 outside the loop.
currently, say you are rolling 3d6. you set total = 0 in the loop, roll a 3, total is 3. loop again. total is 0, rolled a 1, total is 1. .... that seems like not what you want.

you also spam the output. just write the total once at the end of the loop, outside of it, not in it. result printing in the loop is debug type info, do you need to see all the rolls?

goto can be beautiful, but it can also make a huge mess in a short span. Try to not do that. Let your code flow naturally to where you need to be at each point.

a loop might look like
char doneyet = 0;
do
{
play game
cout << "we done yet"
cin >> doneyet;
}while(doneyet != 'y') //example, they type y to stop, anything else is taken as a not done.

and your die goto is just silly, let me translate your code logic into words
if input is not runact
if its a spell, write a prompt for user input but don't let them type any, then go to the same line the program goes to anyway after the if statements.
else if its not a spell, go to the same line it goes to anyway.
die:
the code here is next up to run...

basically, then, if its runact you want to bypass the die block, which is doable by a simple if statement. If its not runact, you want to run the die block. you can do exactly that without any of the die gotos. the top goto is replaced by the while loop, but as you wrote it, the top goto actually does accomplish something (its a loop surrogate, which isnt a good way to use goto).

put all that together, then, and you get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
do
 ...
   if runact
       {
         runact things
       }
    else //isnt run-act
    {
        if(spell) 
            {anything spell specific here}
        if(attack)
            {anything attack specific here}
      {die block} //just do this anyway, its protected from run-act by the outer else. 
    }


does the above flow make sense to you?
Last edited on
So I fixed the top loop issue, but I am still lost at the moment. I am better at building circuits than programming but I want to get better and this project is helping me, but I feel a bit like an old dog. Still I am slowly trying to fix it and learn so I apologize in advance.
Last edited on
First of all, when someone comments on your code, don't edit the post and change it--the comments will no longer make sense. Just post your new code in a new post.

Based on the latest code, one problem you have is that input is not defined anywhere and it's not being set or changed anywhere. There is no way to enter or exit the loop.
Okay I will remember to do so. the issue I am having now is an overload on line 33.


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
int main()
{
	srand(time(NULL));

	int nrOfThrows = 0;
	int sizeOfDice = 0;
	int result = 0;
	int total = 0;
	

	{
		std::cout << "Please input the action you are doing: " << std::endl;

		std::string input;

		std::cin >> input;

		do
		{
			if (input == "run" or "action")
			{
				std::cout << "Please tell the DM your movement location." << std::endl;
			}

			else if (input =! "run" or "action")
			{
				if (input == "cantrip" or "spell")
				{
					std::cout << "Please tell the DM which spell or cantrip you are using: " << std::endl;
				} 
				
				
			
				if (input == "attack")
				{
					std::cout << "what arer you waiting for attack!" << std::endl; 
				}

				die: 
					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++)
					{
						result = rand() % sizeOfDice + 1;


						total += result;
						std::cout << result << std::endl;

						std::cout << "Total is: " << total << std::endl;
					}
			}

		} while (input == "run" or "action" or "spell" or "cantrip" or "attack");
	}
		return 0;
	
}
	



Last edited on
line 33 is empty.
if this is a compiler or linker error, can you just post the error message? If more than 1, post the top 1-3.

if it is a logic error, explain it in detail what went in, what came out, and why its wrong.

For this time, I just pasted your code into my stuff and got an error.

else if (input =! "run" or "action")

this is wrong. ^^
most c++ users prefer && (boolean and) || (boolean or) over the word versions, but the words are ok to use, just make sure you can read both varieties.

c++ (and every other coding language I know of) does not allow implicit human conditions. A human can read "if x is not a,b, or c". The compiler is a moron, and you have to tell it if x!= a and x!=b and x!= c.
so your statement needs to be
else if (input =! "run" or input !="action")

also its !=
=! is not a valid c++ operator. If the compiler accepts it, it will do something you absolutely did not want, it will not the right side, converting whatever it was to boolean if possible, and assign that to the left side. And it may do that silently, while screwing things up and not working but being a legal thing to do.
Last edited on
You are doing the same thing with the if's over and over.
if (input == "run" or "action") //wrong
else //if (input =! "run" or "action") //wrong and redundant: the else is tied to the if above so you already know this condition is met

fix these and it should start working.

I cant stress this enough
if (input == "run" or "action")
means
if (input is run or true)
or true evaluates to true.
this if statement is just saying if(true) which always happens.
Last edited on
I was able to fix the problems now, so I am gonna try and fix the stop when you apply the (run or action) into the input. And also fixing the die situation cause instead of asking for your action input it just lets you roll again.

So my two questions are:
1. How do you rotate the program to go back to the top once a sequence is finished (aka a turn).
2. How would you do a file insert of the character sheet with modifiers so that you can add the modifiers to the rolls when it comes to checks





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
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>




int main()
{
	srand(time(NULL));

	int nrOfThrows = 0;
	int sizeOfDice = 0;
	int result = 0;
	int total = 0;


	{
		std::cout << "Please input the action you are doing: " << std::endl;

		std::string input;

		std::cin >> input;

		do
		{
			if (input == "run" && input == "action")
			{
				std::cout << "Please tell the DM your movement location." << std::endl;

			}

			else if (input != "run" && input != "action")
			{
				if (input == "cantrip" && input == "spell")
				{
					std::cout << "Please tell the DM which spell or cantrip you are using: " << std::endl;
				}



				if (input == "attack")
				{
					std::cout << "what arer you waiting for attack!" << std::endl;
				}

			die:
				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++)
				{
					result = rand() % sizeOfDice + 1;


					total += result;

					std::cout << "Total is: " << total << std::endl;
				}
			}

		} while (input == "run" or "action" or "spell" or "cantrip" or "attack");
	}
	return 0;

}












1. How do you rotate the program to go back to the top once a sequence is finished (aka a turn).

Use a loop. Figure out what the conditions are for continuing to repeat (i.e. do more turns), and use a loop that repeats while that conditon is true.

2. How would you do a file insert of the character sheet with modifiers so that you can add the modifiers to the rolls when it comes to checks

For an introduction to file I/O, look here:

http://www.cplusplus.com/doc/tutorial/files/

You'll need to:
- figure out the format of your character sheet file
- write code to read the contents of the file, and parse it to get the specific modifiers you want
because I know the system you speak of, you can do the modifiers in the code. Updating them in the file if you want to. its just (x-10)/2 .. 12 gives +1, 8 gives -1, etc, right?
whether you just use that in the code or write it out is up to you.
Lines 48 - 64. Is that supposed to be a function? That looks similar to how you might do it in Python, but not c++.

If line 48 is just a label, you never "goto" it (which would be bad anyway), so there is no real reason for line 49.

Your logic in line 34 is wrong. The input variable will never be both "run" and "action". I think you meant to use "or" (the || token).

Then, line 34 can just be else.

The logic you might be looking for is below (based on your code). I have not compiled it, so there might be some errors. You can debug it. Your job is to add conditions to make it work better than it does now.


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
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>

void rollDice();

int main()
{
	srand(time(NULL));

	while (true) // this will let the program run through multiple inputs without
	                   // having to restart the program
	{
		std::cout << "Please input the action you are doing: " << std::endl;

		std::string input;

		std::cin >> input;

		if (input == "run" || input == "action")   // Accept either run or action
		{
			std::cout << "Please tell the DM your movement location." << std::endl;
		}

		else if (input == "cantrip" && input == "spell")
		{
			std::cout << "Please tell the DM which spell or cantrip you are using: " << std::endl;
			rollDice();
		}

		else if (input == "attack")
		{
			std::cout << "what arer you waiting for attack!" << std::endl;
			rollDice();
		}

		else // Nothing matched
		{
			// Not a valid command, so leave the program
			break;
		}
	}

	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++)
	{
		result = rand() % sizeOfDice + 1;
		total += result;

		std::cout << "Total is: " << total << std::endl;
	}
}

Last edited on
Is there anyway to return to the beginning out of the loop cause that is what I am trying to do when it comes to the run or action thing to return to the top instead of infinite replaying of the message
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

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

void rollDice();

int main()
{
	/*
	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::string input;
	std::cin >> input;
	 do
	{
		if (input == "Randal" && input == "Warlock" && input == "Randal Greyman" && input == "1")
		{
			std::cout << "Congrulations! You have chosen to be Randal Greyman, Warlock of the ages!" << std::endl;
		}
		
		else if (input == "Roya" && input =="Bard" && input == "fucking Bard" && input == "Sexy and I know it" && input == "2")
		{
		 std::cout << "Congratulations! You chosen to become Roya, the every woman is my woman bard! << std:: endl;
		}
		
		else if (input == "Oggie" && input == "Ogg-Gnar" && input == "The Cloak" && input == "Ogg-Gnar the Cloak Ranger" && input == "3")
		 {
		 std::cout << "Congratulations! You chosen to be Ogg-Gnar the Cloak, better watch out even his Bow has a bad temper! << std::endl;
		 }

		 else if (input == "




	} while (input == "Randal" or "Warlock" or "Randal Greyman" or "Roya" or "Bard" or "Fucking Bard" or "Sexy and I know it" or "Ogg-Gnar" or "Oggie" or "Ranger" or "Ogg-Gnar the Cloak");
	*/
	
	
	
	
	
	srand(time(NULL));
	
	int nrOfThrows = 0;
	int sizeOfDice = 0;
	int result = 0;
	int total = 0;

	{
		std::cout << "Please input the action you are doing: " << std::endl;

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

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



				if (input == "attack")
				{
					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 >>
					}
				*/
				
			}
		} while (input == "run" || input == "action" || input == "spell" || input == "cantrip" || input == "attack");
	}
	

		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;
	}
}





Last edited on
Pages: 12