Trying to write a simple "Battle Script"

I'm super new to c++ obviously. I decided to take on a challenge and see if I could use my limited knowledge of variables and functions to make a simple battle script where you fight a monster by rolling a random number. (Wanted to work on the skills i've learned so far.) By the way i'm using Orwell Dev C++ if that's something you need to know. I've been working on it for hours and I'm getting nowhere.

Too list the many problems
-"elses" arn't connected to "ifs"
-None of the "gotos" work
-I'm pretty sure the thing I did to check if they are dead is wrong



The debug log
Compiler: TDM-GCC 4.6.1 64-bit
Building Makefile "C:\Dev-Cpp\Makefile.win"
Executing make...
mingw32-make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -D__DEBUG__ -c main.cpp -o main.o -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -g3

main.cpp: In function 'int monster(int, int)':

main.cpp:17:2: error: 'else' without a previous 'if'
main.cpp: In function 'int player(int, int)':
main.cpp:30:5: error: 'else' without a previous 'if'

main.cpp: In function 'int main()':
main.cpp:48:18: error: expected primary-expression before 'int'
main.cpp:48:28: error: expected primary-expression before 'int'
main.cpp:49:15: error: expected primary-expression before 'int'
main.cpp:49:15: error: expected ')' before 'int'
main.cpp:53:5: error: expected '}' before 'else'
main.cpp:53:14: error: expected primary-expression before 'int'
main.cpp:53:14: error: expected ')' before 'int'
main.cpp:51:13: error: label 'died' used but not defined
main.cpp: At global scope:
main.cpp:57:3: error: expected unqualified-id before 'else'
main.cpp:59:10: error: expected constructor, destructor, or type conversion before '(' token

main.cpp:60:7: error: expected unqualified-id before 'goto'
main.cpp:62:5: error: 'yourattack' does not name a type
main.cpp:75:3: error: expected unqualified-id before 'else'
main.cpp:77:7: error: expected unqualified-id before 'goto'

main.cpp:81:1: error: expected declaration before '}' token

mingw32-make.exe: *** [main.o] Error 1

Execution terminated


--------------------------------------------------------------------------------
The actual script
--------------------------------------------------------------------------------
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
#include <cstdlib>
#include <iostream>


using namespace std;
 int mhealth =100; // monster health
 int health = 100; // your health
 int attack = rand() / 1000; // amount of damage attack does (variables later)
 int attackroll = rand(); // roll to wether or not attack is successful


int monster(int dead, int alive)
{
	if (mhealth <= 0) // Checks if monster's health is 0 or lower
		cout << "You killed the monster!" << endl;
    	        return dead;
	else
		cout << "He's still alive!" << endl;
		system("PAUSE");
    	return alive;
    		
}
int player(int dead, int alive)
{
	if (health <= 0) // Checks if your health is 0 or lower
    			cout << "You were killed!" << endl;
    			system("PAUSE");
    			return dead;
    			
       else
    	cout << "Your still standing!" << endl;
    	system("PAUSE");
    	return alive;
}
int main()
{
    cout << "Testing a battle. All coded by Thomas" << endl;
    cout << "A monster is attacking you!" << endl;
    cout << "Your Health: "<< health << "Monster Health: "<< mhealth << endl; //Displaying both current healt amounts
    int attack;
    int attackroll;
    monsterattack:
		cout << "Monster's turn" << endl;
		if (attackroll > 2400) // Seeing if the hit was a success or not, 75% chance to hit
		{
			int health = health - attack; // your health is now your previous health minus the attack
       		cout << "Attack success! Your health now: "<< health << endl;\
       		monster(int dead, int alive);
       			if (int dead)
				   system("PAUSE");
				   goto died;
				
				else if (int alive)
					system("PAUSE");
					return 1;
		}
		else
			cout << "Monster missed!" << endl;
			system("PAUSE");
    		goto yourattack;
    		
    yourattack:
    	if (attackroll > 2800) // Seeing if the hit was a success or not, 87.5% chance to hit
    	{
			int mhealth = health - attack; // monster health is now his previous health minus the attack
    		cout << "Attack Success! Monster's health now: " << mhealth << endl;
    		player(int dead, int alive)
    			if (int dead)
    				system("PAUSE");
    				goto dead;
    			else if (int alive)
    				system("PAUSE");
    				return 1;
		}
		else
    		cout << "You missed!" << endl;
    		goto monsterattack;  
    	
 	   	
    	
}


Last edited on
any chance you go to UH Hilo?
You've made two mistakes consistently.
1) If you have more than one statement in an if/else, they must be enclosed in { }.
2) You're putting int in places where it is not allowed.

Lines 15-16, 26-28, 50-52, 68-70: You need { } around the statements after the if.
Lines 18-19, 31-33, 53-55, 71-72, 76-78: You need { } around the statements after the else.
Line 46: Remove the int. You're tryinng to declare another variable named health when you want to use the global.
Line 48: Remove the ints from the call to monster()
Line 49: Remove the int in front of dead.
Line 53: Remove the int in front of alive.
Note that dead and alive are undefined.
Line 51: died is not defined.
Line 65: Remove the int in front of mhealth.
Line 67: Remove the ints in the call to player.
Line 68: Remove the int in front of dead.
Line 70: You can't go to a variable.
Line 71: Remove the int.

You also have a number of logic errors, but I'll let you find those on your own.
Thanks! I got all errors cleared up and the program runs. When I got it running I started seeing the logic errors your talking about and I'm working on them now. Thanks again for the help.

and to kaydee, No I don't. I'm trying to teach myself C++ and it's going really slow. I decided that I would make a semi-complex (For me at least) program so I can practice and better understand the stuff I just learned


edit: Sorry to ask another stupid question but why does my RAND_MAX keep giving me the same number every time

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
#include <cstdlib>
#include <iostream>


using namespace std;
 int mhealth =100; // monster health
 int health = 100; // your health
 int attack = RAND_MAX % 100; // amount of damage attack does (variables later)
 int attackroll = RAND_MAX % 10; // roll to wether or not attack is successful
 
 
 
int monster(int dead, int alive)
{
	if (mhealth <= 0) // Checks if monster's health is 0 or lower
	{
		cout << "You killed the monster!" << endl;
		system("PAUSE > null");
    	return dead; 
	}
	else
	{
		cout << "He's still alive!" << endl;
		system("PAUSE > null");
    	return alive; 
    }		
}
int player(int dead, int alive)
{
	if (health <= 0) // Checks if your health is 0 or lower
	{
    			cout << "You were killed!" << endl;
    			system("PAUSE > null");
    			return dead;
	}
    else
    {
    	cout << "Your still standing!" << endl;
    	system("PAUSE > null");
    	return alive;
	}	
}
int main()
{
    cout << "Testing a battle. All coded by Thomas" << endl;
    system("PAUSE > null");
    cout << "A monster is attacking you!" << endl;
    system("PAUSE > null");
    cout << "Your Health: "<< health << " Monster Health: "<< mhealth << endl; //Displaying both current healt amounts
    system("PAUSE > null");
    monsterattack:
		cout << "Monster's turn" << endl;
		system("PAUSE > null");
		if (attackroll < 2400) // Seeing if the hit was a success or not, 75% chance to hit
		{
			health = health - attack; // your health is now your previous health minus the attack
       		cout << "Attack success! Your health now: "<< health << endl;
       		int dead;
       		int alive;
			player(dead, alive); //calls the if dead function to see if your alive
       			if (dead)
       			{
				   system("PAUSE > null");
				   goto died;
				}
				else if (alive)
				{
					system("PAUSE > null");
					goto yourattack;
				}
		}
		else
		{
			cout << "Monster missed!" << endl;
			system("PAUSE > null");
    		goto yourattack;
    	}
    yourattack:
    	cout << "Your attack!" << endl;
    	system("PAUSE > null");
    	if (attackroll < 2800) // Seeing if the hit was a success or not, 87.5% chance to hit
    	{
			mhealth = health - attack; // monster health is now his previous health minus the attack
    		cout << "Attack Success! Monster's health now: " << mhealth << endl;
    		int dead;
    		int alive;
			monster(dead, alive);// calls the check dead function to see if the mosnter is alive
    			if (dead)
    			{
    				system("PAUSE > null");
    				goto killed;
    			}
    			else if (alive)
    			{
    				system("PAUSE > null");
    				goto monsterattack;
    			}
		}
		else
		{
    		cout << "You missed!" << endl;
    		system("PAUSE > null");
    		goto monsterattack;  
    	}
    	
    killed:
    	char win;
    	return win;
    died:
    	char died;
    	return died;
 	   	
    	
}
Last edited on
why does my RAND_MAX keep giving me the same number every time


Because it's a constant. :)
http://www.cplusplus.com/reference/cstdlib/RAND_MAX/?kw=RAND_MAX

Some comments on your latest code.
Lines 58-61 and 85-88: dead and alive are uninitialized when passed into player() and monster(). dead and alive are passed by value, therefore are not changed by player() or monster().

In reality, there is no reason to even have these variables.
Make player() and monster() bool functions and use the return value of the function to indicate dead or alive.

Lines 106-111: win is uninitialized. died is both a label and a variable. That's a non-no.









Last edited on
For rand to work, you need to use srand in Main to create a seed;

#include <ctime>

srand(time(0)); // this inside main at beginning

then you can use rand in your functions, etc.
Topic archived. No new replies allowed.