Problem with if else statements.

ive been reading a book with C++ tutorials and the nested if else's they show are not working in my combat algorithm. Could someone please help me clean it up so it works. thanks


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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;
/*
These constants define our upper
and our lower bounds. The random numbers
will always be between 1 and 6, inclusive.
*/
int Test = 0;
int MonsterLevel = 1;
int MonsterHitPoints = 15;
int MonsterAttack = 8;
int MonsterDefense = 3;
int MonsterHitRate = 60;
int PlayerLevel = 1;
int PlayerHitPoints = 25;
int PlayerAttack = 10;
int PlayerDefense = 5;
int PlayerHitRate = 75;

int LowMonster = 2;
int HighMonster = MonsterAttack - PlayerDefense;
int LowPlayer = 2;
int HighPlayer = PlayerAttack - MonsterDefense;

int PlayerHitCheck = 0;
int MonsterHitCheck = 0;
int PlayerHitLow = 1;
int PlayerHitHigh = 100;
int MonsterHitLow = 1;
int MonsterHitHigh = 100;





int main()
{
/*
Variables to hold random values
for the first and the second die on
each roll.
*/
int first_die, sec_die;
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsigned int) seconds);
/*
Get first and second random numbers.
*/


if ( (MonsterHitPoints >= 1) && (PlayerHitPoints >= 1) )
{
	cout << "Monsters & Player Can Fight HP ok.\n";
	PlayerHitCheck = rand() % (PlayerHitHigh - PlayerHitLow +1) + PlayerHitLow;
	
	if (PlayerHitCheck <= 25)
	cout << "Player swings his weapon and misses.\n";
	cin >> Test;
	
	else (PlayerHitCheck >=26)
	{
		cout << "Player swings his weapon and lands a blow!\n"
		first_die = rand() % (HighPlayer - LowPlayer + 1) + LowPlayer;
		sec_die = rand() % (HighPlayer - LowPlayer +1) + LowPlayer;

		cout<< "Players attack is (" << first_die << ", "
		<< sec_die << "}" << endl << endl;
		cin >> Test;
		MonsterHitCheck = rand() % (MonsterHitHigh - MonsterHitLow +1) + MonsterHitLow;
			
		if (MonsterHitCheck <= 40)
		
			cout << "Monster swings and misses.\n";
			cin >> Test;
		
		else (MonsterHitCheck >=41)
		
					cout << "Monster swings and lands a blow!\n";
					first_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
					sec_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;

					cout<< "Monsters attack is (" << first_die << ", "
					<< sec_die << "}" << endl << endl;
	}			        cin >> Test;
}
else ( (MonsterHitPoints <= 0) && (PlayerHitPoints <= 0) )
	cout << "Unable to fight! Player or Monster HP are 0.\n";
	cin >> Test;

return 0;
}
You're missing braces.

1
2
3
4
if(something)
  This_is_part_of_the_if_block();
  This_isnt();
else  // this is an error (no matching 'if') 


To correct:

1
2
3
4
5
6
7
8
9
if(something)
{  // brace
  OK();
  OK2();
}
else
{
  NoLongerAnError();
}
ah weird sams teach yourself C++ shows only 2 braces which kinda seems weird

@firedraco

the example was 3 nested if else statements and it only showed a total of 4 braces possibly a typo or missprint?
Last edited on
If you only have one statement, you can omit the braces. If you want more than one statement, you must use braces. e.g.:

1
2
3
4
5
6
7
if(something)
{  // brace
  OK();
  OK2();
}
else
  NoLongerAnError();
Here is the code in working format:

Could you point me in the direction of how to make it continue to do "Rounds" until player or monster HP hits 0 and how to convert dice roll dmg back into - player / monster HP variables

@Disch thanks alot that helped very much im working on cleaning up the code now. Ive added more since then so that copy is outdated.

im doing this on a seperate cpp till i get the bugs worked out of it, that way i dont mess up my main code, this is going to be part of a combat() 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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;

// Strings and extra variables
string PlayerName; // Player's name
string PlayerWeapon;  // Name of weapon equipped by player.
string MonsterName = "Zombie";  // Monster name or type
string MonsterWeapon; // Name of weapon equipped by monster if it has one.
char NextRound;  // For testing only

// MONSTER VARIABLES
int MonsterLevel = 1;
int MonsterHitPoints = 15;
int MonsterAttack = 8;
int MonsterDefense = 3;
int MonsterHitRate = 60;

// PLAYER VARIABLES
int PlayerLevel = 1;
int PlayerHitPoints = 25;
int PlayerAttack = 10;
int PlayerDefense = 5;
int PlayerHitRate = 75;

// Monster damage variables using rand
// Also subtracts player defense from monsters damage
int LowMonster = 2;
int HighMonster = MonsterAttack - PlayerDefense;

// Player damage variables using rand
// Also subtraces monster defense from players damage
int LowPlayer = 2;
int HighPlayer = PlayerAttack - MonsterDefense;

// Variable to see if player or monster land a blow
int PlayerHitCheck = 0;
int MonsterHitCheck = 0;

// Player % chance to hit high and low
int PlayerHitLow = 1;
int PlayerHitHigh = 100;

// Monster % chance to hit high and low
int MonsterHitLow = 1;
int MonsterHitHigh = 100;

// Player & Monster damage results die 1 + die 2
int MonsterDamageResults = 0;
int PlayerDamageResults = 0;




int main()
{
/*
Variables to hold random values
for the first and the second die on
each roll.
*/
int first_die, sec_die;
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsigned int) seconds);
/*
Get first and second random numbers.
*/

cout << "You encounter a Level 1 Zombie. Prepare to fight!\n";
cout << "What is your name? <a-z>\n";
cin >> PlayerName;

BEGINCOMBAT:

	if ( (MonsterHitPoints >= 1) && (PlayerHitPoints >= 1) )
	{
		cout << "Starting battle between [" << PlayerName <<"] & [" << MonsterName <<"]!\n";
		PlayerHitCheck = rand() % (PlayerHitHigh - PlayerHitLow +1) + PlayerHitLow;
	
		if (PlayerHitCheck <= 25)
		{
			cout << "" << PlayerName <<" Swings his weapon and misses.\n";
			cout << "Press <c> and enter to continue to next combat round.\n";
			cin >> NextRound;
		}
		else
		{
			cout << "" << PlayerName <<" swings his weapon and lands a blow!\n";
			first_die = rand() % (HighPlayer - LowPlayer + 1) + LowPlayer;  // Calculate damage of first dice roll
			sec_die = rand() % (HighPlayer - LowPlayer +1) + LowPlayer;     // Calculate damage of second dice roll

			cout << "" << PlayerName <<"'s attack is (" << first_die << ", "
			<< sec_die << "}\n\n";
			cout << "Press <c> and enter to continue to next combat round.\n";
			cin >> NextRound;
			MonsterHitCheck = rand() % (MonsterHitHigh - MonsterHitLow +1) + MonsterHitLow; // Check to see if monster hits player
		}	
		if (MonsterHitCheck <= 40)
		{
			cout << "Monster swings and misses.\n";
			cin >> NextRound;
		}	
		else
		{
			cout << "Monster swings and lands a blow!\n";
			first_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
			sec_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;

			cout<< "Monsters attack is (" << first_die << ", "
			<< sec_die << "}" << endl << endl;
			MonsterDamageResults = sec_die + first_die;
			cout << "Monster hits player for " << MonsterDamageResults << " damage.\n";
			PlayerHitPoints = PlayerHitPoints - MonsterDamageResults;
			cout << "Players hitpoints are: " << PlayerHitPoints << ".\n";
			cin >> NextRound;
		}
	}
	else
	{
	cout << "Unable to fight! Player or Monster HP are 0.\n";
	cin >> NextRound;
	}
DEAD:	
	cout << "" << PlayerName <<" has died! [GAME OVER]\n\n";
	cin >> NextRound;

	return 0;
}
Last edited on
You really should get more consistent with your indenting.

Proper indentation REALLY helps code clarity.

EDIT: also.... don't put semicolons after elses... and elses don't have conditional statements!

Here's an improved/corrected version of what you just pasted:

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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;
/*
These constants define our upper
and our lower bounds. The random numbers
will always be between 1 and 6, inclusive.
*/
int Test = 0;
int MonsterLevel = 1;
int MonsterHitPoints = 15;
int MonsterAttack = 8;
int MonsterDefense = 3;
int MonsterHitRate = 60;
int PlayerLevel = 1;
int PlayerHitPoints = 25;
int PlayerAttack = 10;
int PlayerDefense = 5;
int PlayerHitRate = 75;

int LowMonster = 2;
int HighMonster = MonsterAttack - PlayerDefense;
int LowPlayer = 2;
int HighPlayer = PlayerAttack - MonsterDefense;

int PlayerHitCheck = 0;
int MonsterHitCheck = 0;
int PlayerHitLow = 1;
int PlayerHitHigh = 100;
int MonsterHitLow = 1;
int MonsterHitHigh = 100;





int main()
{
    /*
    Variables to hold random values
    for the first and the second die on
    each roll.
    */
    int first_die, sec_die;
    /*
    Declare variable to hold seconds on clock.
    */
    time_t seconds;
    /*
    Get value from system clock and
    place in seconds variable.
    */
    time(&seconds);
    /*
    Convert seconds to a unsigned
    integer.
    */
    srand((unsigned int) seconds);
    /*
    Get first and second random numbers.
    */


    if ( (MonsterHitPoints >= 1) && (PlayerHitPoints >= 1) )
    {
        cout << "Monsters & Player Can Fight HP ok.\n";
        PlayerHitCheck = rand() % (PlayerHitHigh - PlayerHitLow +1) + PlayerHitLow;

        if (PlayerHitCheck <= 25)
        {
            cout << "Player swings his weapon and misses.\n";
            cin >> Test;
        }
        else
        {
            cout << "Player swings his weapon and lands a blow!\n";
            first_die = rand() % (HighPlayer - LowPlayer + 1) + LowPlayer;
            sec_die = rand() % (HighPlayer - LowPlayer +1) + LowPlayer;

            cout<< "Players attack is (" << first_die << ", "
            << sec_die << "}" << endl << endl;
            cin >> Test;
            MonsterHitCheck = rand() % (MonsterHitHigh - MonsterHitLow +1) + MonsterHitLow;
        }	
        if (MonsterHitCheck <= 40)
        {
            cout << "Monster swings and misses.\n";
            cin >> Test;
        }	
        else
        {
            cout << "Monster swings and lands a blow!\n";
            first_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
            sec_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;

            cout<< "Monsters attack is (" << first_die << ", "
            << sec_die << "}" << endl << endl;
            cin >> Test;
        }
    }
    else
    {
        cout << "Unable to fight! Player or Monster HP are 0.\n";
        cin >> Test;
    }
    return 0;
}


So how much cleaner it is? You can clearly see the control blocks. Makes it much easier to at-a-glance follow the overall program flow.

Also notice how nothing is after the 'else'
Last edited on
For "rounds" you are going to want to use loops:
http://www.cprogramming.com/tutorial/lesson3.html
@ccdwiu

What if i want the conditions for the loop to be triggered by player && monster hp not being below or equal to 0 rather then using x++;

I want the rounds to keep going until one of them is 0, then returning player won if monster hp <= 0 or player dies if his hp is <= 0.
Last edited on
Then for the loop condition, just check if player HP > 0 && monster HP > 0
You don't have to use a 'for' loop. Look at the link that I posted it explains 2 other loops. For this situation I'd probably use a 'do while' loop
Topic archived. No new replies allowed.