Page 1 of 2

Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 4:01 pm
by davidthefat
:shock: Yes Im a Noob and its my first dedicated program...

You probably have to see the whole Program to see the problem...

main.c

Code: Select all

#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "main.h"
using namespace std;
void Shop();
void fight();
void SkillPoint();
void levelup();
void Items();
void Save();
void Load();
void Game();
void NewGame();

int main(int argc, char *argv[])
{
  cout <<"1=Load \t 2=New Game" << endl;
  cin >> choice;
  if (choice == 1)
  {
	  Load();
  }
  if (choice == 2)
  {
	  NewGame();
  }
}

void fight()
{
	
	
	cout << "Bat appeared!" << endl;
	
	for (;BatHP >0;)	
	{
		cout << BatHP <<"/" << OGBatHP <<"\nYour HP: "<< hp <<"/"<< fullhp <<"\nYour MP: "<<mp <<"/"<< fullmp << endl;
		cout << "1=Attack 2=Power Hit 3=Stab 4=Item" << endl;
		cin >> choice1;
		if (choice1 == 1)
		{
						BatHP = BatHP - (BatDEF / 2 + atk);
						cout << "Bat took " << +(BatDEF / 2 + atk)
							 << " Damage" << endl;
						if(BatHP <= 0)
						{
							cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
								 exp += exprate;
								 money += 30;
								 OGBatHP += 50;
								 BatDEF += 10;
								 BatAtk += 10;
								 exprate += 10;
								 
						}
						if(BatHP >= 0)
						{
							hp = hp - (def / 2 + BatAtk);
							cout << "You took " << -(def / 2 + BatAtk)
								<< " Damage" << endl;
						}
		}
		
		
		else if ((choice1 == 2) && (Super > 0) && (mp >= 150))
		{
			mp -= 150;
			cout <<"MP-150\nPower Hit!"<< endl;
			BatHP = BatHP - (BatDEF / 2 + (atk * Super));
						cout << "Bat took " << -(BatDEF / 2 - (atk * Super))
							 << " Damage" << endl;
						if(BatHP <= 0)
						{
							cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
								 exp += exprate;
								 money += 30;
								 OGBatHP += 50;
								 BatDEF += 10;
								 BatAtk += 10;
								 exprate += 10;
						}
						if(BatHP >= 0)
						{
							hp = hp - (def / 2 + BatAtk);
							cout << "You took " << -(def / 2 + BatAtk)
								<< " Damage" << endl;
						}
		}
		else if ((choice1 == 3) && (Stab > 0) && (mp >= 200))
		{
			mp -= 200;
			cout <<"MP-200\nStab!!!"<< endl;
			BatHP = BatHP - ((atk * Stab)/2);
						cout << "Bat took " <<  (atk * Stab)/2
							 << " Damage" << endl;
						if(BatHP <= 0)
						{
							cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
								  exp += exprate;
								 money += 30;
								 OGBatHP += 50;
								 BatDEF += 10;
								 BatAtk += 10;
								 exprate += 10;
								 
						}
						if(BatHP >= 0)
						{
							hp = hp - (def / 2 + BatAtk);
							cout << "You took " << -(def / 2 + BatAtk)
								<< " Damage" << endl;
						}
		}
		
		else if (choice1 == 4)
		{
			Items();
		}
		
		else
		{
			cout <<"No Can Do"<< endl;
		}
		
		

	}
	
}

void SkillPoint()
{
	cout <<"HP Mastery=" << hpmastery << endl;
	cout <<"MP Mastery=" << mpmastery << endl;
	cout <<"Attack Mastery=" << atkmastery << endl;
	cout <<"Defence Mastery=" << defmastery << endl;
	cout <<"Skill Points=" << SP << endl;
	cout <<"What would you like to upgrade?" << endl;
	cout <<"1=HP Mastery \t 2=MP Mastery \t 3=Attack Mastery \t 4=Defence Mastery \n5=Power Hit \t 6=Stab" <<endl;
	cin >> choice;
	if ((choice == 1) && (SP > 0))
	{
		SP -= 1;
		hpmastery += 5;
	cout <<"HP Mastery Up!" << endl;
	}
else if ((choice == 2) && (SP > 0))
	{
		SP -= 1;
		mpmastery += 5;
		cout <<"MP Mastery Up!" << endl;
	}
	else if ((choice == 3) && (SP > 0))
	{
		SP -= 1;
		atkmastery += 10;
		cout <<"Attack Mastery Up!" << endl;
	}
else if ((choice == 4) && (SP > 0))
	{
		SP -= 1;
		defmastery += 10;
		cout <<"Defence Mastery Up!" << endl;
	}
else if ((choice == 5) && (SP > 0) && (Super < 10))
{
	SP -= 1;
	Super += 1;
	cout <<"Power Hit Up!"<< endl;
}
else if ((choice == 6) && (SP > 0) && (Stab < 10))
{
	SP -= 1;
	Stab += 1;
	cout <<"Stab Up!"<< endl;
}

else 
{
	cout <<"No can do" << endl;
}
}

void levelup()
{
	
		level ++;
		fullhp += hpmastery;
		fullmp += mpmastery;
		hp = fullhp;
		mp = fullmp;
		atk += atkmastery;
		def += defmastery;
		SP += 3;
		explevel *=2;
		cout <<"Level Up!" << endl;
		cout << hp <<"+" << hpmastery <<"=" <<hp + hpmastery <<" HP"<< endl;
		cout << mp <<"+" << mpmastery <<"=" <<mp + mpmastery <<" MP"<< endl;
		cout << atk <<"+" << atkmastery <<"=" <<atk + atkmastery <<" Attack"<< endl;
		cout << def <<"+" << defmastery <<"=" <<def + defmastery <<" Defence"<< endl;
	
	}

void Items()
{
	cout <<"1= HP Potion " << HPpot <<" 2= MP Potion " << MPpot <<" 3= Back" << endl;
	cin >> choice;
		if ((choice == 1) && (HPpot > 0) && (hp <= fullhp))
	{
		HPpot --;
		hp += 100;
		cout <<"Healed 100 HP" <<endl;
			}
		else if ((choice == 2) && (MPpot > 0) && (mp <= fullmp))
		{
			MPpot --;
			mp += 100;
			cout <<"Healed 100 MP" <<endl;
	}
		else if (choice == 3)
		{
			cout <<"OK" << endl;
		}
		else
		{
			cout <<"No can do" << endl;
		}
}
void Save()
{
	ofstream save("DaveSata.dav");
	save << fixed;
	save << left;


    save << name << endl;
    save << level << endl;
	save << money << endl;
	save << exp << endl;
	save << explevel << endl;
	save << exprate << endl;
	save << hp << endl;
	save << mp << endl;
	save << hpmastery << endl;
	save << mpmastery << endl;
	save << fullhp << endl;
	save << fullmp << endl;
	save << atk << endl;
	save << def << endl;
	save << atkmastery << endl;
	save << defmastery << endl;
	save << Super << endl;
	save << Stab << endl;
	save << HPpot << endl;
	save << MPpot << endl;
	save << BatHP << endl;
	save << BatDEF << endl;
	save << BatAtk << endl;
	save << OGBatHP << endl;


	save.close();
	cout <<"Game Saved!" << endl;
}
void Load()
{
	ifstream load("DaveSata.dav");
	load >> name;
    load >> level;
	load >> money;
	load >> exp;
	load >> explevel;
	load >> exprate;
	load >> hp;
	load >> mp;
	load >> hpmastery;
	load >> mpmastery;
	load >> fullhp;
	load >> fullmp;
	load >> atk;
	load >> def;
	load >> atkmastery;
	load >> defmastery;
	load >> Super;
	load >> Stab;
	load >> HPpot;
	load >> MPpot;
	load >> BatHP;
	load >> BatDEF;
	load >> BatAtk;
	load >> OGBatHP;

	cout <<"Game Loaded!" << endl;

	Game();
}
void Game()
{	
	for(; hp > 0 ;)
	{
		cout <<"\n\n\n\n\n\n\n\n\n\n\n";
    cout << "Hi " << name << " how are you doing?" << endl;
	cout <<"Level=" << level << endl;
	cout <<"HP=" << hp <<"/"<< fullhp << "\t Attack=" << atk << endl;
	cout <<"MP=" << mp <<"/"<< fullmp << "\t Defence=" << def << endl;
	cout <<"Money=" << money << "\t EXP=" << exp <<"/"<<explevel<< endl;
	cout <<"HP Mastery=" << hpmastery << endl;
	cout <<"MP Mastery=" << mpmastery << endl;
	cout <<"Attack Mastery=" << atkmastery << endl;
	cout <<"Defence Mastery=" << defmastery << endl;
	cout <<"1=Inn \t 2=Shop \t 3=Suicide" << endl <<
		"4=Fight \t 5=Skills \t 6=Items" << endl <<
		"7=Save" << endl;
	cin >> choice;
	
	if ((choice == 1) && (money >= 50))
	{
		
		hp = fullhp;
		mp = fullmp;
	money -= 50;
	cout <<"Your HP and MP is Fully Restored" << endl;
	}
		else if (choice == 2)
	{
		Shop();
	}
	
	
	else if (choice == 3)
	{
		cout <<"You stab yourself!" << endl;
		    while (hp>=0) {
    
				cout << "HP:" << hp << "\n";
    --hp;
			}
  

 
		
		
	}
	else if (choice == 4)
	{
		BatHP = OGBatHP;
		fight();
	}
	else if (choice == 5)
	{
		SkillPoint();
	}
	else if (choice == 6)
	{
		Items();
	}
	else if (choice == 7)
	{
		Save();
	}
	

	else
	{
		cout << "Make another selection." << endl;
	}
	
	
	if (exp >= explevel)
	{
		levelup();
	}
	
	if (hp <=0)
	{
		cout << "GAME OVER" << endl;
		system("color 40");
		system("PAUSE");
	}
	}
	
    system("PAUSE");
    
	
}



void Shop()
{
	int choice2;
	cout <<"Hello welcome to the Shop \nWhat would you like?\n 1=Attack Up 100 money \t 2=Defence Up 100 money \n 3=HP Potion 50 money\t 4=MP Potion 50 money" << endl;
cin >> choice2;
if ((choice2 == 1) && (money >= 100))
{
	atk += 10;
	money -= 100;
	cout <<"Attack + 10" << endl;
}
else if ((choice2 == 2) && (money >= 100))
{
def += 10;
	money -= 100;
	cout <<"Defence + 10" << endl;
}
else if ((choice2 == 3) && (money >= 50))
{
	HPpot ++;
	money -= 50;
	cout <<"HP Potion +1" << endl;
}
else if ((choice2 == 4) && (money >= 50))
{
	MPpot ++;
	money -= 50;
	cout <<"MP Potion +1" << endl;
}
else
{
	cout <<"No can do" << endl;
}
}
void NewGame()
{
	cout << "What is your name?" << endl;
    cin >> name;
	
	hp = 200,mp = 200,fullhp = 200, fullmp = 200, money = 100, choice , level = 1 ,hpmastery = 25,mpmastery = 25,atk = 50,def = 50,atkmastery = 10, defmastery = 10,SP = 0;
	BatHP, BatDEF = 10, BatAtk = 10, choice1,OGBatHP = 100;
explevel = 100, exp = 0,exprate = 10;
Super = 0, Stab = 0;
HPpot = 0, MPpot = 0;
Game();
}
main.h

Code: Select all

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;


string name;
	int hp ,mp,fullhp, fullmp, money, choice , level ,hpmastery,mpmastery,atk,def,atkmastery, defmastery,SP;
	int BatHP, BatDEF, BatAtk, choice1,OGBatHP;
int explevel, exp,exprate;
int Super, Stab;
int HPpot, MPpot;

The problem that it does not display "GAME OVER" when the hp reached 0 or lower, I think the problem is that the code that says to run that never actually runs while its during a battle since its in a different function. I was thinking to put it in a battle function, it runs twice since its in the game function... so just help put it into separate source codes and use classes, I tried before, it failed epicly...

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 4:19 pm
by avansc
have you tried a debugger of some sorts?
note : i havent looked at your code, first asking some questions.

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 4:24 pm
by davidthefat
avansc wrote:have you tried a debugger of some sorts?
note : i havent looked at your code, first asking some questions.
I tried the Built in debugger in VisualC++ but :roll: I have no clue what it means :lol: Yes I admit Im a noob at debugging, I usually do it by trial and error by changing the codes and stuff, thats why it came out so messy

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 4:48 pm
by avansc
davidthefat wrote:
avansc wrote:have you tried a debugger of some sorts?
note : i havent looked at your code, first asking some questions.
I tried the Built in debugger in VisualC++ but :roll: I have no clue what it means :lol: Yes I admit Im a noob at debugging, I usually do it by trial and error by changing the codes and stuff, thats why it came out so messy
i have debugged a hundered programs, and still struggle with it sometimes.
a very primitive, but widely used( prodessional developes use this technique alot )technique of debugging is to figuer out where you think your code is messing up. ie nor reacing, or having bad values, go a step back in the program, and print some data out to the console. check if its right, and move to the next logical stem in the program and print out again. do so till you find where it does not do what you expect it to do. then you have a 100% beter chance of fixing your bug.

funny qoute, i cant remember if i hear it somewhere, but im gonna claim it as my own
Andre van-Schalkwyk wrote:If debugging is the process of getting bugs out of a program, then programming must be the process of putting them in.

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 4:51 pm
by avansc
i tried compiling in VS2008, not gonna try and find why is not compiling.
Click here to see the hidden message (It might contain spoilers)
------ Build started: Project: hp, Configuration: Debug Win32 ------
Compiling...
main.c
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(24) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(24) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(24) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(24) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(24) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(24) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(25) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(25) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(25) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(25) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(25) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(25) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(26) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(26) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(26) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(26) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(26) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(26) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(27) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(27) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(27) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(27) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(27) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(27) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(28) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(28) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(28) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(28) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(28) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(28) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(29) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(29) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(29) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(29) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(29) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(29) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(30) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(30) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(30) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(30) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(30) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(30) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(31) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(31) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(31) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(31) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(32) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(32) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(32) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(32) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2143: syntax error : missing '{' before ':'
c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : fatal error C1003: error count exceeds 100; stopping compilation
Build log was saved at "file://c:\Documents and Settings\dev\My Documents\Visual Studio 2008\Projects\hp\hp\Debug\BuildLog.htm"
hp - 102 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 4:52 pm
by davidthefat
I found out using tracepoints that the code runs after the character was leveledup, and not during the battle, so the battle that the charater gets like -30 hp is coincidentally the one that he levels up, so that was quite a problem...

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 4:53 pm
by davidthefat
avansc wrote:i tried compiling in VS2008, not gonna try and find why is not compiling.
LOL WTF? I got the VisualC++ 9.0 express edition and the latest DevC++, both compiles fine...

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 5:05 pm
by avansc
did you copy those files exactly?

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 5:08 pm
by avansc
i'll try and compile in DevC++

but let me just say this, you are using H file for the wrong reason, and you are implimenting them in a very dangerous way. i'll explain a little bit more later.

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 5:08 pm
by M_D_K
made some changes.

Code: Select all

#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "main.h"
using namespace std;
void Shop();
void fight();
void SkillPoint();
void levelup();
void Items();
void Save();
void Load();
void Game();
void NewGame();

int main(int argc, char *argv[])
{
  cout <<"1=Load \t 2=New Game" << endl;
  cin >> choice;
  if (choice == 1)
  {
     Load();
  }
  if (choice == 2)
  {
     NewGame();
  }
  return 0;
}

void fight()
{


   cout << "Bat appeared!" << endl;

   for (;BatHP >0;)
   {
      cout << BatHP <<"/" << OGBatHP <<"\nYour HP: "<< hp <<"/"<< fullhp <<"\nYour MP: "<<mp <<"/"<< fullmp << endl;
      cout << "1=Attack 2=Power Hit 3=Stab 4=Item" << endl;
      cin >> choice1;
      if (choice1 == 1)
      {
                  BatHP = BatHP - (BatDEF / 2 + atk);
                  cout << "Bat took " << +(BatDEF / 2 + atk)
                      << " Damage" << endl;
                  if(BatHP <= 0)
                  {
                     cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
                         exp += exprate;
                         money += 30;
                         OGBatHP += 50;
                         BatDEF += 10;
                         BatAtk += 10;
                         exprate += 10;

                  }
                  if(BatHP >= 0)
                  {
                     hp = hp - (def / 2 + BatAtk);
                     cout << "You took " << -(def / 2 + BatAtk)
                        << " Damage" << endl;
                  }
      }


      else if ((choice1 == 2) && (Super > 0) && (mp >= 150))
      {
         mp -= 150;
         cout <<"MP-150\nPower Hit!"<< endl;
         BatHP = BatHP - (BatDEF / 2 + (atk * Super));
                  cout << "Bat took " << -(BatDEF / 2 - (atk * Super))
                      << " Damage" << endl;
                  if(BatHP <= 0)
                  {
                     cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
                         exp += exprate;
                         money += 30;
                         OGBatHP += 50;
                         BatDEF += 10;
                         BatAtk += 10;
                         exprate += 10;
                  }
                  if(BatHP >= 0)
                  {
                     hp = hp - (def / 2 + BatAtk);
                     cout << "You took " << -(def / 2 + BatAtk)
                        << " Damage" << endl;
                  }
      }
      else if ((choice1 == 3) && (Stab > 0) && (mp >= 200))
      {
         mp -= 200;
         cout <<"MP-200\nStab!!!"<< endl;
         BatHP = BatHP - ((atk * Stab)/2);
                  cout << "Bat took " <<  (atk * Stab)/2
                      << " Damage" << endl;
                  if(BatHP <= 0)
                  {
                     cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
                          exp += exprate;
                         money += 30;
                         OGBatHP += 50;
                         BatDEF += 10;
                         BatAtk += 10;
                         exprate += 10;

                  }
                  if(BatHP >= 0)
                  {
                     hp = hp - (def / 2 + BatAtk);
                     cout << "You took " << -(def / 2 + BatAtk)
                        << " Damage" << endl;
                  }
      }

      else if (choice1 == 4)
      {
         Items();
      }

      else
      {
         cout <<"No Can Do"<< endl;
      }

	if(hp <= 0)
	{
		cout << "you died against a bat...you suck!" << endl;
		break;
	}

   }

}

void SkillPoint()
{
   cout <<"HP Mastery=" << hpmastery << endl;
   cout <<"MP Mastery=" << mpmastery << endl;
   cout <<"Attack Mastery=" << atkmastery << endl;
   cout <<"Defence Mastery=" << defmastery << endl;
   cout <<"Skill Points=" << SP << endl;
   cout <<"What would you like to upgrade?" << endl;
   cout <<"1=HP Mastery \t 2=MP Mastery \t 3=Attack Mastery \t 4=Defence Mastery \n5=Power Hit \t 6=Stab" <<endl;
   cin >> choice;
   if ((choice == 1) && (SP > 0))
   {
      SP -= 1;
      hpmastery += 5;
   cout <<"HP Mastery Up!" << endl;
   }
else if ((choice == 2) && (SP > 0))
   {
      SP -= 1;
      mpmastery += 5;
      cout <<"MP Mastery Up!" << endl;
   }
   else if ((choice == 3) && (SP > 0))
   {
      SP -= 1;
      atkmastery += 10;
      cout <<"Attack Mastery Up!" << endl;
   }
else if ((choice == 4) && (SP > 0))
   {
      SP -= 1;
      defmastery += 10;
      cout <<"Defence Mastery Up!" << endl;
   }
else if ((choice == 5) && (SP > 0) && (Super < 10))
{
   SP -= 1;
   Super += 1;
   cout <<"Power Hit Up!"<< endl;
}
else if ((choice == 6) && (SP > 0) && (Stab < 10))
{
   SP -= 1;
   Stab += 1;
   cout <<"Stab Up!"<< endl;
}

else
{
   cout <<"No can do" << endl;
}
}

void levelup()
{

      level ++;
      fullhp += hpmastery;
      fullmp += mpmastery;
      hp = fullhp;
      mp = fullmp;
      atk += atkmastery;
      def += defmastery;
      SP += 3;
      explevel *=2;
      cout <<"Level Up!" << endl;
      cout << hp <<"+" << hpmastery <<"=" <<hp + hpmastery <<" HP"<< endl;
      cout << mp <<"+" << mpmastery <<"=" <<mp + mpmastery <<" MP"<< endl;
      cout << atk <<"+" << atkmastery <<"=" <<atk + atkmastery <<" Attack"<< endl;
      cout << def <<"+" << defmastery <<"=" <<def + defmastery <<" Defence"<< endl;

   }

void Items()
{
   cout <<"1= HP Potion " << HPpot <<" 2= MP Potion " << MPpot <<" 3= Back" << endl;
   cin >> choice;
      if ((choice == 1) && (HPpot > 0) && (hp <= fullhp))
   {
      HPpot --;
      hp += 100;
      cout <<"Healed 100 HP" <<endl;
         }
      else if ((choice == 2) && (MPpot > 0) && (mp <= fullmp))
      {
         MPpot --;
         mp += 100;
         cout <<"Healed 100 MP" <<endl;
   }
      else if (choice == 3)
      {
         cout <<"OK" << endl;
      }
      else
      {
         cout <<"No can do" << endl;
      }
}
void Save()
{
   ofstream save("DaveSata.dav");
   save << fixed;
   save << left;


    save << name << endl;
    save << level << endl;
   save << money << endl;
   save << exp << endl;
   save << explevel << endl;
   save << exprate << endl;
   save << hp << endl;
   save << mp << endl;
   save << hpmastery << endl;
   save << mpmastery << endl;
   save << fullhp << endl;
   save << fullmp << endl;
   save << atk << endl;
   save << def << endl;
   save << atkmastery << endl;
   save << defmastery << endl;
   save << Super << endl;
   save << Stab << endl;
   save << HPpot << endl;
   save << MPpot << endl;
   save << BatHP << endl;
   save << BatDEF << endl;
   save << BatAtk << endl;
   save << OGBatHP << endl;


   save.close();
   cout <<"Game Saved!" << endl;
}
void Load()
{
   ifstream load("DaveSata.dav");
   load >> name;
    load >> level;
   load >> money;
   load >> exp;
   load >> explevel;
   load >> exprate;
   load >> hp;
   load >> mp;
   load >> hpmastery;
   load >> mpmastery;
   load >> fullhp;
   load >> fullmp;
   load >> atk;
   load >> def;
   load >> atkmastery;
   load >> defmastery;
   load >> Super;
   load >> Stab;
   load >> HPpot;
   load >> MPpot;
   load >> BatHP;
   load >> BatDEF;
   load >> BatAtk;
   load >> OGBatHP;

   cout <<"Game Loaded!" << endl;

   Game();
}
void Game()
{
	bool playing = true;
	while(playing)
	{
	cout <<"\n\n\n\n\n\n\n\n\n\n\n";
	cout << "Hi " << name << " how are you doing?" << endl;
	cout <<"Level=" << level << endl;
	cout <<"HP=" << hp <<"/"<< fullhp << "\t Attack=" << atk << endl;
	cout <<"MP=" << mp <<"/"<< fullmp << "\t Defence=" << def << endl;
	cout <<"Money=" << money << "\t EXP=" << exp <<"/"<<explevel<< endl;
	cout <<"HP Mastery=" << hpmastery << endl;
	cout <<"MP Mastery=" << mpmastery << endl;
	cout <<"Attack Mastery=" << atkmastery << endl;
	cout <<"Defence Mastery=" << defmastery << endl;
	cout <<"1=Inn \t 2=Shop \t 3=Suicide" << endl <<
	"4=Fight \t 5=Skills \t 6=Items" << endl <<
	"7=Save" << endl;
	cin >> choice;

   if ((choice == 1) && (money >= 50))
   {

      hp = fullhp;
      mp = fullmp;
   money -= 50;
   cout <<"Your HP and MP is Fully Restored" << endl;
   }
      else if (choice == 2)
   {
      Shop();
   }


   else if (choice == 3)
   {
      cout <<"You stab yourself!" << endl;
          while (hp>=0) {

            cout << "HP:" << hp << "\n";
    --hp;
         }





   }
   else if (choice == 4)
   {
      BatHP = OGBatHP;
      fight();
   }
   else if (choice == 5)
   {
      SkillPoint();
   }
   else if (choice == 6)
   {
      Items();
   }
   else if (choice == 7)
   {
      Save();
   }


   else
   {
      cout << "Make another selection." << endl;
   }


   if (exp >= explevel)
   {
      levelup();
   }

   if (hp <=0)
   {
      cout << "GAME OVER" << endl;
      system("color 40");
      playing = false;
   }
   }

    system("PAUSE");


}



void Shop()
{
   int choice2;
   cout <<"Hello welcome to the Shop \nWhat would you like?\n 1=Attack Up 100 money \t 2=Defence Up 100 money \n 3=HP Potion 50 money\t 4=MP Potion 50 money" << endl;
cin >> choice2;
if ((choice2 == 1) && (money >= 100))
{
   atk += 10;
   money -= 100;
   cout <<"Attack + 10" << endl;
}
else if ((choice2 == 2) && (money >= 100))
{
def += 10;
   money -= 100;
   cout <<"Defence + 10" << endl;
}
else if ((choice2 == 3) && (money >= 50))
{
   HPpot ++;
   money -= 50;
   cout <<"HP Potion +1" << endl;
}
else if ((choice2 == 4) && (money >= 50))
{
   MPpot ++;
   money -= 50;
   cout <<"MP Potion +1" << endl;
}
else
{
   cout <<"No can do" << endl;
}
}
void NewGame()
{
   cout << "What is your name?" << endl;
    cin >> name;

   hp = 200,mp = 200,fullhp = 200, fullmp = 200, money = 100, choice , level = 1 ,hpmastery = 25,mpmastery = 25,atk = 50,def = 50,atkmastery = 10, defmastery = 10,SP = 0;
   BatHP, BatDEF = 10, BatAtk = 10, choice1,OGBatHP = 100;
explevel = 100, exp = 0,exprate = 10;
Super = 0, Stab = 0;
HPpot = 0, MPpot = 0;
Game();
}
  • Added a return 0 at the end of main
  • Added a check in fight() for hp <= 0
  • changed the game loop replaced the for loop with a while loop and added a boolean check called playing
EDIT: BTW it works :)

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 5:11 pm
by davidthefat
avansc wrote:i'll try and compile in DevC++

but let me just say this, you are using H file for the wrong reason, and you are implimenting them in a very dangerous way. i'll explain a little bit more later.
Then can you explain? Cause H files are usually for class stuff right? honestly its been a long time from reading the C++ books (it was in the summer)

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 5:22 pm
by avansc
davidthefat wrote:
avansc wrote:i'll try and compile in DevC++

but let me just say this, you are using H file for the wrong reason, and you are implimenting them in a very dangerous way. i'll explain a little bit more later.
Then can you explain? Cause H files are usually for class stuff right? honestly its been a long time from reading the C++ books (it was in the summer)
no, H files are just for definitions/prototypes
here is a good example of a pair header and source file,

Code: Select all

//////////////////////////////////////
// File : line.h					//
// Author : Andre van-Schalkwyk		//
// Version : 1.3					//
// Date : 26.10.2006				//
//////////////////////////////////////

#ifndef _line_h
#define _line_h

class line
{
public:
	line();
	line(float x1, float y1, float x2, float y2);
	~line();
	float x1,y1,x2,y2;
private:
};

#endif
note the

#ifndef _line_h
#define _line_h

and

#endif

if you have a big program and have multiple files and you start including alot of things all over the place, particularly 2 files that have each other included you are gonna run into confusin compile time issues. this solves that problem.

Code: Select all

//////////////////////////////////////
// File : line.cpp					//
// Author : Andre van-Schalkwyk		//
// Version : 1.3					//
// Date : 26.10.2006				//
//////////////////////////////////////

#include "line.h"

line::line()
{
	this->x1 = 0;
	this->y1 = 0;
	this->x2 = 0;
	this->y2 = 0;
}

line::line(float x1, float y1, float x2, float y2)
{
	this->x1 = x1;
	this->y1 = y1;
	this->x2 = x2;
	this->y2 = y2;
}

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 5:51 pm
by davidthefat
Well Im gonna rewrite the whole thing with classes, then after I got that part down, split it up into different files

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 5:57 pm
by avansc
davidthefat wrote:Well Im gonna rewrite the whole thing with classes, then after I got that part down, split it up into different files
i would suggest this: get a piece of paper and a pen/pencil, make diagrams of what classes you need, and the start coding from there.
if you first try to write then to segment, you are going to have a hard time, and ultimatly have to change things to make it work. ( i know this from many bad experiences )

Re: Cleaning up my code and Help displaying game over

Posted: Mon Nov 10, 2008 5:58 pm
by davidthefat
avansc wrote:
davidthefat wrote:Well Im gonna rewrite the whole thing with classes, then after I got that part down, split it up into different files
i would suggest this: get a piece of paper and a pen/pencil, make diagrams of what classes you need, and the start coding from there.
if you first try to write then to segment, you are going to have a hard time, and ultimatly have to change things to make it work. ( i know this from many bad experiences )
I think thats why the first time I tried this, it failed miserably, BTW thanks for all your help