Adding To Game

Hello, I posted a while ago to get some help with a simple game that I was creating to test what I had learned. I decided to rewrite the code to add new things that I've learned. I'm getting a lot of errors, I'm sure it has something to do witht he function, but if you could look at this code, it would be greatly appreciated, thank you, and thanks to everyone that's helped me in the past!

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

#include <iostream>
#include <string>
using namespace std;
int hero_health, monster_health, hero_damage, monster_damage, hero_weapon, hero_RH, monster_RH; 
hero_health = 100;
monster_health = 5;
hero_damage = hero_weapon;
monster_damage = 10;
int battle (hero_health, monster_health, hero_damage, monster_damage, hero_weapon);
int main ()
{
	string battle;
	string hero_name;
	cout << "enter your hero's name: ";
	getline(cin, hero_name);
		cout << "welcome to Cravin " << hero_name;
		cout << "What weapon would you like to fight with? \n";
		cout << "press 1 for sword, press 2 for axe, press 3 for hammer";
		cin >> hero_weapon;
chooseweapon:
		switch (hero_weapon)
		{
		case 1:
			cout << "you choose to weild the sword. A long sharp blade, that glints in the sun!";
			hero_weapon = 5;
				break;
		case 2:
			cout << "you choose to wield the axe. It's blade shimmers in the light!";
			hero_weapon = 5;
			break;
		case 3:
			cout << "you choose to wield the hammer. A massive bludgeoning weapon";
			hero_weapon = 10;
			break;
		default:
			cout << "you have pressed an incorrect key.";
				goto chooseweapon;
		}
cout << "you are walking through the hills, and come across a viciouse gobline.\n";
cout << "it sneers it's teeth. Do you wish to fight it, or run away?";
battleflag:
getline (cin,battle)
if (battle == "battle" || battle == "fight" || battle == "battle goblin" || battle == fight goblin)
{
	cout << "you choose to attack the goblin! ";
		cout << "You exchange blows";
		cout << "your remaining health is: " << hero_RH;
	cout << "the monster's remaining health is: " << monster_RH;
	battle (100, 5, hero_damage, 10, hero_weapon);
}
else 
{
	cout << "I didn't understand what you wanted to do";
	goto battleflag;
}
return 0;
}
battle (hero_health, monster_health, hero_damage, monster_damage, hero_weapon)
{
	hero_RH = hero_health - monster_damage;
	monster_RH = monster_health - hero_weapon;
	return hero_RH, monster_RH;
}



Here are the errors:

1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(5) : error C2086: 'int hero_health' : redefinition
1> c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(4) : see declaration of 'hero_health'
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(6) : error C2086: 'int monster_health' : redefinition
1> c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(4) : see declaration of 'monster_health'
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(7) : error C2086: 'int hero_damage' : redefinition
1> c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(4) : see declaration of 'hero_damage'
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(8) : error C2086: 'int monster_damage' : redefinition
1> c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(4) : see declaration of 'monster_damage'
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(9) : error C2078: too many initializers
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(43) : error C2143: syntax error : missing ';' before 'if'
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(43) : error C2065: 'fight' : undeclared identifier
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(43) : error C2146: syntax error : missing ')' before identifier 'goblin'
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(43) : error C2059: syntax error : ')'
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(44) : error C2065: 'goblin' : undeclared identifier
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(44) : error C2143: syntax error : missing ';' before '{'
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(49) : error C2064: term does not evaluate to a function taking 5 arguments
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(51) : error C2181: illegal else without matching if
1>c:\users\scott\desktop\jeremy\c++\programs\program test\program test\program.cpp(59) : error C2448: 'battle' : function-style initializer appears to be a function definition
1>Build log was saved at "file://c:\Users\Scott\Desktop\Jeremy\C++\Programs\Program Test\Program Test\Debug\BuildLog.htm"
1>Program Test - 18 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Lines 6-9: You can't do that outside a function. Put the = in de declaration line ( line 5 ) so the constructor are called ( not the = operator )
Line 10: You didn't specify the parameter types
Line 13: string battle has the same name as the function
Lines 21-38: Don't use a goto there, use a loop
Line 43: Missing semicolon
Line 44: Missing quotes on "fight goblin"
Lines 42-55: Same as 21-38
Line 59: Missing parameter types and function return type specifiers
Last edited on
okay, I've changed some of what you said. Everything but the loops, I'm not sure what loops I should use instead of a goto...I've got one error. It's saying I'm missing a type specifier.

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

#include <iostream>
#include <string>
using namespace std;
int hero_health, monster_health, hero_damage, monster_damage, hero_weapon, hero_RH, monster_RH; 

int battle (int hero_health, int monster_health, int hero_damage, int monster_damage, int hero_weapon);
int main ()
{
	string battle_choice;
	string hero_name;
	cout << "enter your hero's name: ";
	getline(cin, hero_name);
		cout << "welcome to Cravin " << hero_name;
		cout << "What weapon would you like to fight with? \n";
		cout << "press 1 for sword, press 2 for axe, press 3 for hammer";
		cin >> hero_weapon;
chooseweapon:
		switch (hero_weapon)
		{
		case 1:
			cout << "you choose to weild the sword. A long sharp blade, that glints in the sun!";
			hero_weapon = 5;
				break;
		case 2:
			cout << "you choose to wield the axe. It's blade shimmers in the light!";
			hero_weapon = 5;
			break;
		case 3:
			cout << "you choose to wield the hammer. A massive bludgeoning weapon";
			hero_weapon = 10;
			break;
		default:
			cout << "you have pressed an incorrect key.";
				goto chooseweapon;
		}
cout << "you are walking through the hills, and come across a viciouse gobline.\n";
cout << "it sneers it's teeth. Do you wish to fight it, or run away?";
battleflag:
getline (cin,battle_choice);
if (battle_choice == "battle" || battle_choice == "fight" || battle_choice == "battle goblin" || battle_choice == "fight goblin")
{
	cout << "you choose to attack the goblin! ";
		cout << "You exchange blows";
		cout << "your remaining health is: " << hero_RH;
	cout << "the monster's remaining health is: " << monster_RH;
	battle (100, 5, hero_damage, 10, hero_weapon);
}
else 
{
	cout << "I didn't understand what you wanted to do";
	goto battleflag;
}
return 0;
}
battle (int hero_health, int monster_health, int hero_damage, int monster_damage, int hero_weapon)
{
hero_health = 100;
monster_health = 5;
hero_damage = hero_weapon;
monster_damage = 10;

	hero_RH = hero_health - monster_damage;
	monster_RH = monster_health - hero_weapon;
	return hero_RH, monster_RH;
}

Line 56.
Use while loops.
Last edited on
Thanks for all your help, I really appreiciate it. I'm going to look over the while loops in the book, and see if I can replace the code with those loops. there's one more problem tho. When the program is run, it returns 0 for both hero_RH (remaining health), and monster_RH (remaining health).
The battle function is messy, you are confusing global variables with function arguments and you don't need a return value.
I wouldn't doubt it's messy. I've only been doing this for a short while. Do you have any pointers on how to tidy this up?
It's not just messy, like Bazzy said some of it is wrong. Functions can only return one value, so your return is not going to work like you expect, and for some reason you have duplicates of all the global variables as function parameters. Additionally, you unconditionally set them (the local values) to set values, meaning whatever you pass in/whatever their current value, they will be changed to 100, 5, and 10.

You should probably read up more on functions and variables.
Okay, I had made this game previously to return only hero remaining health, and it worked. I wasn't sure if I could have the function return two values or not, I was just seeing if it was possible. These variables aren't meant to have any other value than 100 for hero health, and 5 for monster health, and 10 for monster damage, as this isn't a real game, but a mock game....I'll have to tweak a few things to make it simpler...I'm still just learning...thanks for the help I've recieved
zhuge, can you explain to me what you mean about.

for some reason you have duplicates of all the global variables as function parameters. Additionally, you unconditionally set them (the local values) to set values, meaning whatever you pass in/whatever their current value, they will be changed to 100, 5, and 10.


I know I have all the variables as global variables, does that mean that I did not need to declair them as integers in other places? I was getting errors, until I did in some other places..
Last edited on
What it means is that when you are inside a function, your function parameters (since they have the same name as the global variables), will be used *instead* of your global variables (they are different).

I think you need to go look up more information on functions and how they work.
Hey, I've got two functions in here now, to split up the work, and output the two values. The program is getting errors on it's build tho. I am just learning functions now, and I'll admit, a lot of this was expirimentation so any pointers would really help.

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

#include <iostream>
#include <string>
using namespace std;
int hero_health, monster_health, monster_damage, hero_weapon, hero_RH, monster_RH; 
int Mbattle ( monster_health, hero_weapon);
int Hbattle (hero_health, monster_damage);
int main ()
{
	string battle_choice;
	string hero_name;
	cout << "enter your hero's name: ";
	getline(cin, hero_name);
		cout << "welcome to Cravin " << hero_name;
		cout << " What weapon would you like to fight with? \n";
		chooseweapon:
		cout << " press 1 for sword, press 2 for axe, press 3 for hammer";
		cin >> hero_weapon;

		switch (hero_weapon)
		{
		case 1:
			cout << "you choose to weild the sword. A long sharp blade, that glints in the sun!";
			hero_weapon = 5;
				break;
		case 2:
			cout << "you choose to wield the axe. It's blade shimmers in the light!";
			hero_weapon = 5;
			break;
		case 3:
			cout << "you choose to wield the hammer. A massive bludgeoning weapon";
			hero_weapon = 10;
			break;
		default:
			cout << "you have pressed an incorrect key.";
				goto chooseweapon;
		}
cout << "you are walking through the hills, and come across a viciouse gobline.\n";
cout << "it sneers it's teeth. Do you wish to fight it, or run away?";
battleflag:
getline (cin,battle_choice);
if (battle_choice == "battle" || battle_choice == "fight" || battle_choice == "battle goblin" || battle_choice == "fight goblin")
{
	cout << "you choose to attack the goblin! ";
		cout << "You exchange blows";
		cout << "your remaining health is: " << hero_RH;
	cout << "the monster's remaining health is: " << monster_RH
	Hbattle (100, 5);
	Mbattle (6, hero_weapon);
}
else 
{
	cout << "I didn't understand what you wanted to do";
	goto battleflag;
}
return 0;
}
int Hbattle (hero_health, monster_damage )
{
	hero_RH = hero_health - monster_damage;
		return hero_RH;
}
int Mbattle (monster_health, hero_weapon)
{
	monster_RH = monster_health - hero_weapon;
	return monster_RH;
}


error C2078: too many initializers
error C2146: syntax error : missing ';' before identifier 'Hbattle'
term does not evaluate to a function taking 2 arguments
term does not evaluate to a function taking 2 arguments
function-style initializer appears to be a function definition
function-style initializer appears to be a function definition

Well I can tell you line 47 needs a semi-colon at the end. =)
You have the same errors I corrected you on the first post.
You should read the tutorial about functions http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.