2 errors inn rpg battle test

ey, i have started with c++ a couple of days ago, when i was now trying out the start of a rpg battle system i get 2 errors, first time i tryed i got 6 errors, but figured out 4 of them. The 2 last however i can't figure out, been looking through it a couple of hours and tryed searching etc, but nothing. So this is my last resort:P

#include <iostream>
#include <ctime>
using namespace std;

int main ()

{
int a, b;
int health;
a = 50;
b = rand()%100+1;
health = a - b;

cout << "Goblin hits you for" << b << "damage!\n";

if (health < 0)
cout << "you take" << b << " damage and die!\n";

if (health > 0)
cout << "you take" << b << "damage and have" << health << " health left!\n";
return 0;
}


The 2 errors:
(15) : error C2146: syntax error : missing ';' before identifier 'cin'
(19) : error C2447: '{' : missing function header (old-style formal list?)

any help will be greatly appriciated:)
Confusion here.
cin does not even turn up in the code you have posted. Can you recheck??
If you are absolutely sure that the code you have posted is the only code in that particular file - then check that there isn't a second file being compiled that you have missed.
Last edited on
nope, i'm pretty sure thats the error(s) i get from that code, it might mean i should have cin somewere in the code?

you know what might be wrong with the other error?
mingw32-g++.exe -o ".../test.exe" ".../test.o"
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings

Looks fine to me.
Last edited on
hmm, ye, somehow it mixed up with another code or something, started a new project and it work, sigh... alot of wasted time:P. but how do i make it so i have to click enter for each line? and also, somehow the goblin always does 42 damage :\
You didn't seed rand().
Put srand(time(0)) somewhere before the call.
ye, thx, now the dmg is random:)
is it possible to add an "if" like if the health remaining is over 1 but under 10, i tryed this: if (health5>1<10). but that didn't work. I just ended adding an if for every letter from 1-9. But there has to be an easier way. sorry if i ask to much. you guys have really helped me out alot allready:D
and also, somehow the goblin always does 42 damage :\

The goblin must be a messenger for Life, the Universe, and Everything.
Oh, lawl, AZERTYman.

Look into the boolean operators && (and), || (or), and ! (not).
In particular, what you want to do is this: health>1 && health<10
Last edited on
It's working perfect now! thx once again!:)

this is the final result for this test:) will be trying to add so you can have different types of attacks, and hopefully get it to work without asking here^^

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
#include <iostream>
#include <ctime>
using namespace std;

int main () 
{
	int a, b, c, d, e, f, g;
	int health1;
	int health2;
	int health3;
	int health4;
	int health5;
	a=100;
	c=50;
    srand(time(0));
	b=rand()%15;
	d=rand()%10;
	e=rand()%30;
	f=rand()%10;
	g=rand()%50;
	health1= a-d;
	health2= c-b;
	health3= health1-f;
	health4= health2-e;
	health5= health4-g;

	cout << "A goblin appears out of nowhere and slices at you dealing " << d << " damage! You have " << health1 << " health left.\n" << endl;
	cout << endl;
	cout << "After recovering from the strike you rush towards the goblin inflicting\n " << b << " damage! The goblin now have " << health2 << " health left.\n" << endl;
	cout << endl;
	cout << "The goblin is quick to return the favor and claw you for " << f << " damage! you now have " << health3 << " health left.\n" << endl;
	cout << endl;
	cout << "You finally get the chance to draw your sword and cut the goblin for " << e << " damage!\n Your strike makes a deep wound immoblizing the goblin and gives you the\n opportunity to strike again.  The goblin have " << health4 << " health remaining\n" << endl;
	cout << endl;
	cout << "You swing your sword with massive force hoping to eliminate this scum once and\n for all, your strike lands and deal " << g << " damage!\n" << endl;
	cout << endl;

	if (health5 < 1)
		cout << "When you open your eyes after that final blow, you see the lifeless body of the goblin covered inn blood. After taking a deep breath you continue your journey\n inn this forsaken land.";

	if (health5 > 1 && health5 < 10)
		cout << "The goblin falls to the ground, exhausted and wounded. You take your sword and\n plants it inn his chest to end his misery.";

	if (health5 > 9 && health5 < 19)
		cout << "Finally realising that he is no match against you, the goblin runs into the\n forest before you can react.";

	if (health5 > 19)
		cout << "You put to much strenght into your massive strike which throws you forward crushing your head\n into a rock. Then all goes black.....";
}


The \n in mid-sentance is so the words don't get cut when in full screen which im using:)
Lines 27-36: Wrong!
 
cout << "A goblin appears out of nowhere and slices at you dealing " << d << " damage! You have " << health1 << " health left.\n\nAfter recovering from the strike you rush towards the goblin inflicting\n " << b << " damage! The goblin now have " << health2 << " health left.\n\nThe goblin is quick to return the favor and claw you for " << f << " damage! you now have " << health3 << " health left.\n\nYou finally get the chance to draw your sword and cut the goblin for " << e << " damage!\n Your strike makes a deep wound immoblizing the goblin and gives you the\n opportunity to strike again.  The goblin have " << health4 << " health remaining\n\nYou swing your sword with massive force hoping to eliminate this scum once and\n for all, your strike lands and deal " << g << " damage!\n\n; 
well, it works... but whats wrong then?
You're wasting calls to output functions. You should do it in as few << operators as possible.
so what can i cut down on? but it isent wrong then:P
Calls to output functions should be as few in number as possible. So instead of:
1
2
3
cout<<"asdf"<<endl;
cout<<"asdf"<<endl;
cout<<"asdf"<<endl;

You should prefer:
1
2
3
cout<<"asdf"<<endl
    <<"asdf"<<endl
    <<"asdf"<<endl;
ok, thx for the tip. So i can just remove all but the first cout?
Yes, just make sure to remove the semicolons on the end of the other lines.
1
2
3
cout<<"asdf"<<endl // no semicolon - statement continues
    <<"asdf"<<endl // on the next line
    <<"asdf"<<endl;
Topic archived. No new replies allowed.