block of code not working (not printing)

Hello, I just jumped into C++, like 2-4 months ago, I didn't made much progress because I was busy, well, I started working on simple console based combat simulator game lately, and month after, I get back to it, and... well it was really hard for me to understand it again and analyze it, because code was so badly designed. and I seem to be stuck (I already analyzed code, there's just bug that I can't solve), I a have pretty bad habits, and I write very ugly and incorrect code, please take a look at this code and tell me how to improve it, and.. also there's major bug in it, I might be making really silly mistake, I showed some person code, who was a little more competent than me, but even he could not understand whats problem, mainly because code was messed up visually.. anyways here it is:

When battle ends, I am not getting results, this part of code completely does not work. some stuff are for debug purposes, because I had alot of bugs making this program, believe it or not. (see whole code also):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cout << "Humans: " << armyMen << endl << "Skeletons: " << armySkeleton << endl;
			
			if (armyMen <= 0) // Human army

			{ 

				cout << "Victory!" << endl;

				cout << "Humans killed: " << humansKilled << endl << "Skeletons killed: " << skeletonsKilled << endl;


			}
			else if (armySkeleton <= 0) // Skeleton army
			{

				cout << "Defeat" << endl;
				cout << "Humans killed: " << humansKilled << endl << "Skeletons killed: " << skeletonsKilled << endl;
			}
	


Now, see whole code..:
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
 #include <iostream>
#include <string>
#include <Windows.h>
#include <random>
#include <ctime>



using namespace std;




int main()
{

	default_random_engine randomEngine(time(0));
	uniform_real_distribution<float> attack(0.0f, 1.0f);

	string generalName;

	// Human properties
	float humanAttack = 0.6f; //Chance
	float humanHealth = 250.0f;
	float humanDamage = 200.0f;
	float currentHumanHealth = humanHealth;
	int humansKilled;

	// Skeleton properties
	float skeletonAttack = 0.2f;
	float skeletonHealth = 50.0f;
	float skeletonDamage = 40.0f;
	float currentSkeletonHealth = skeletonHealth;
	int skeletonsKilled;

	float attackResult;

	// Armies
	int armyMen;
	int armySkeleton;

	char turn = 'H'; // H - Human

	cout << skeletonAttack << endl;
	cout << "What your name would be?" << endl;
	cin >> generalName;
	cout << "Oh, welcome general " << generalName << endl;
	cout << "How many men do we have?" << endl;
	cin >> armyMen;
	cout << "I see..." << endl;
	cout << "((How many skeletons are attacking? (skeletons are weaker than humans)))" << endl;
	cin >> armySkeleton;

	if (armySkeleton <= 0) 
	{

		cout << "Nice weather..." << endl;

	}

	else if (armySkeleton > 0 && armySkeleton <= 25)
	{

		cout << "Small group of skeletons is attacking!" << endl;
		cout << "We shall attack!" << endl;
		goto battleProcess;
	}
	else if (armySkeleton > 25 && armySkeleton < 100)
	{

		cout << "Big group of skeletons is attacking!" << endl;
		cout << "We shall attack!" << endl;
		goto battleProcess;
	}
	else if (armySkeleton >= 100)
	{

		cout << "Army of skeletons is attacking!" << endl;
		cout << "We shall attack!" << endl;
		//End of if's

	battleProcess:
		while ((armyMen > 0) && (armySkeleton > 0)) // Battle process, goto statement.
		{

			if (turn == 'H')
			{


				if (armyMen--)
				{

					cout << "Human recruit has been killed" << endl;

				}


				// Get attack result
				attackResult = attack(randomEngine);

				// Check if attack was succsessful
				if (attackResult <= humanAttack)
				{
					currentSkeletonHealth -= humanDamage;

					if (currentSkeletonHealth < 0)
					{

						armySkeleton--;
						skeletonsKilled++;

					}

					turn = 'S';

				}

			}
			else {

				if (armySkeleton--)
				{

					cout << "Skeleton recruit has been killed" << endl;

				}

				currentHumanHealth -= skeletonDamage;

				if (currentHumanHealth < 0)
				{

					armyMen--;
					humansKilled++;

				}
				turn = 'H';

			}

			cout << "Humans: " << armyMen << endl << "Skeletons: " << armySkeleton << endl;
			
			if (armyMen <= 0)

			{ 

				cout << "Victory!" << endl;

				cout << "Humans killed: " << humansKilled << endl << "Skeletons killed: " << skeletonsKilled << endl;


			}
			else if (armySkeleton <= 0)
			{

				cout << "Defeat" << endl;
				cout << "Humans killed: " << humansKilled << endl << "Skeletons killed: " << skeletonsKilled << endl;
			}
	

		

		}

	




	system("PAUSE");
}


I wrote this topic pretty fast, I hope I didn't do something stupid. Thanks!
Last edited on
Well first of all, what type of IDE or compiler are you using? Because I'm getting an error from the windows.h header file that says that this file requires compiler and library support for the ISO C++ 2011 standard with my IDE(code blocks).
Last edited on
I removed a bunch of whitespace that made the code harder to read, and I added a closing brace to an 'if' block so that it would compile. I also removed the goto. When you run this code, what problems are you seeing?
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
#include <iostream>
#include <string>
#include <random>
#include <ctime>

using namespace std;

int main()
{
    default_random_engine randomEngine(time(0));
    uniform_real_distribution<float> attack(0.0f, 1.0f);

    string generalName;

    // Human properties
    float humanAttack = 0.6f; //Chance
    float humanHealth = 250.0f;
    float humanDamage = 200.0f;
    float currentHumanHealth = humanHealth;
    int humansKilled;

    // Skeleton properties
    float skeletonAttack = 0.2f;
    float skeletonHealth = 50.0f;
    float skeletonDamage = 40.0f;
    float currentSkeletonHealth = skeletonHealth;
    int skeletonsKilled;

    float attackResult;

    // Armies
    int armyMen;
    int armySkeleton;

    char turn = 'H'; // H - Human

    cout << skeletonAttack << endl;
    cout << "What your name would be?" << endl;
    cin >> generalName;
    cout << "Oh, welcome general " << generalName << endl;
    cout << "How many men do we have?" << endl;
    cin >> armyMen;
    cout << "I see..." << endl;
    cout << "((How many skeletons are attacking? (skeletons are weaker than humans)))" << endl;
    cin >> armySkeleton;

    if (armySkeleton <= 0) 
    {
        cout << "Nice weather..." << endl;
    }
    else if (armySkeleton > 0 && armySkeleton <= 25)
    {
        cout << "Small group of skeletons is attacking!" << endl;
        cout << "We shall attack!" << endl;
    }
    else if (armySkeleton > 25 && armySkeleton < 100)
    {
        cout << "Big group of skeletons is attacking!" << endl;
        cout << "We shall attack!" << endl;
    }
    else if (armySkeleton >= 100)
    {
        cout << "Army of skeletons is attacking!" << endl;
        cout << "We shall attack!" << endl;
    }

    while ((armyMen > 0) && (armySkeleton > 0)) // Battle process
    {
        if (turn == 'H')
        {
            if (armyMen--)
            {
                cout << "Human recruit has been killed" << endl;
            }

            // Get attack result
            attackResult = attack(randomEngine);

            // Check if attack was succsessful
            if (attackResult <= humanAttack)
            {
                currentSkeletonHealth -= humanDamage;
                if (currentSkeletonHealth < 0)
                {
                    armySkeleton--;
                    skeletonsKilled++;
                }
                turn = 'S';
            }
        }
        else
        {
            if (armySkeleton--)
            {
                cout << "Skeleton recruit has been killed" << endl;
            }

            currentHumanHealth -= skeletonDamage;

            if (currentHumanHealth < 0)
            {
                armyMen--;
                humansKilled++;
            }
            turn = 'H';
        }

        cout << "Humans: " << armyMen << endl << "Skeletons: " << armySkeleton << endl;

        if (armyMen <= 0)
        { 
            cout << "Victory!" << endl;
            cout << "Humans killed: " << humansKilled << endl << "Skeletons killed: " << skeletonsKilled << endl;
        }
        else if (armySkeleton <= 0)
        {
            cout << "Defeat" << endl;
            cout << "Humans killed: " << humansKilled << endl << "Skeletons killed: " << skeletonsKilled << endl;
        }
    }

    system("PAUSE");
}
Last edited on
Microsoft Visual Studio, I intended for it to work on Windows, and sorry I forgot to write down details, Its Visual Studio 2013, visual C++, Windows 7 SP1, 64 bit.
Last edited on
Hey there, sorry for late reply, well thank you very much you really helped me, I was just getting few errors and I solved them, I had to initalize humansKilled and skeletonsKilled by some reason, so I set them to 0, hm... confusing. anyways, it is working. I am getting stuck really often, I might open new topic for different help if I will have real hard time, Thanks again.
Topic archived. No new replies allowed.