Jump to content


[HELP] First c++ text based game :)


  • You cannot reply to this topic
17 replies to this topic

#1 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 20 August 2009 - 04:37 PM

Hi, this is both my first post here and my first c++ project :)
i have been following a tutorial on how to make text based games in c++ but have a problem... i have tried to go back several times but cant seem to find the problem, so i thaught you guys could help me :)

here is the error i get when building :)

1>------ Build started: Project: DopeGame2, Configuration: Debug Win32 ------
1>Compiling...
1>Crossroads.cpp
1>c:\users\liv\documents\visual studio 2008\projects\dopegame2\dopegame2\crossroads.cpp(20) : error C2143: syntax error : missing ')' before ';'
1>c:\users\liv\documents\visual studio 2008\projects\dopegame2\dopegame2\crossroads.cpp(48) : error C2059: syntax error : ')'
1>Build log was saved at "file://c:\Users\Liv\Documents\Visual Studio 2008\Projects\DopeGame2\DopeGame2\Debug\BuildLog.htm"
1>DopeGame2 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

#include "Library.h"

//CROSSROADS

// GLOBAL VARIABLES

bool glcomplete = false;
bool fcomplete = false;
bool scomplete = false;
bool gcomplete = false;
bool ccomplete = false;

bool Crossroads()
{
	int choice = 0;

	while (choice != 10)
	(
		cout << "You stand at a crossroad with several roads going towards all directions.\n\n";
		cout << "There is a small town behihnd you full of shops and people.\n\n";

		cout << "Which path do you choose?\n";
		cout << "1: Town\n";
		cout << "2: Summoning Portal\n";

		if (!glcomplete)
			cout << "3: Grasslands Road (QUEST)\n";
		
		if (glcomplete && !fcomplete)
			cout << "4: Forest Road (QUEST)\n";

		if (fcomplete && !scomplete)
			cout << "5: Swamp Road (QUEST)\n";

		if (scomplete && !gcomplete)
			cout << "6: Graveyard Road (QUEST)\n";

		if (gcomplete && !ccomplete)
			cout << "7: Castle Road (QUEST)\n";

		cout << "10: Exit Game\n";

		cout << ">";

		cin >> choice;

	)

	return true;
}

obviously i dont know how to get "code" tags in this forum so if annyone wants to give me a heads up on that matter too it would be great ;)

thx alot in advance ;)

cheers, Plutonium

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 20 August 2009 - 05:13 PM

Hi Plutonium, the code tags are [code]...[/code]. You almost had it right; you were using a backslash instead of a forward slash. :) Anyway, your problem is that you're trying to use parentheses () around the body of the while loop. In C/C++ you need to use curly braces {} to enclose compound statements like bodies of functions, loops, and so on.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 20 August 2009 - 06:56 PM

thanks alot m8 ;)
worked out great this time and i will remember it for the future ;)

got a new question tho :P
in the tutorial video when he uses the following codes...


class PLAYER

{

private

	char name[32];

	int strength;

	int intelligence;

	int gold;

	int experience;

	int health;

	int maxhealth;

	int mana;

	int maxmana;

	int level;

public:

	void setNAME(char* newname);

	char* GetName();

	void SetStrength(int nwestr);

	int GetStength();

	void SetIntelligence(int newint);

	int GetIntelligence();

	void SetGold(intgold);

	void AddGold(int amount);

	int GetGold();

	void SetExperience(int newexperience);

	void AddExperience(int amount);

	int GetExperience();

	void SetHealth(int newhealth);

	void AddHealth(int amount);

	void LooseHealth(int amount);

	int GetHealth();

	void SetMaxHealth(int newmaxhealth);

	int GetMaxHealth();

	void SetMana(int newmana);

	void AddMana(int amount);

	void LooseMana (int amount);

	int GetMana();

	void SetMaxMana(int newmana);

	int GetMaxMana();	

};

he uses the voids and ints later on but when he types in for example:
void PLAYER:: he gets a list of all the voids? how can i get the list too? cause i dont have it :S
and it will save me a lot of typing^^

thx!
greetings, plutonium :)

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 20 August 2009 - 07:00 PM

What you're talking about sounds like autocompletion, and it's a feature of the IDE you use (for instance, Visual Studio, where the feature is named "IntelliSense"). It should be turned on by default in Visual Studio, if you've set up a project file and solution.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 20 August 2009 - 07:14 PM

okey, thx... but do u know why the IntelliSense doesent show with me?
i have a project file, and a solution i think :P but it wont pop up :S

tried the force show with ctrl + j and space but still doesent show.
i am using void PLAYER::AddMana(int amount) in the tried case...

thx

*edit:
i am using Visual c++ 2008 express edition if it matters^^
and found another problem :S
sorry for the bother...

1>Battle.cpp

1>c:\users\liv\documents\visual studio 2008\projects\dopegame2\dopegame2\player.h(5) : error C2144: syntax error : 'char' should be preceded by ':'

1>c:\users\liv\documents\visual studio 2008\projects\dopegame2\dopegame2\player.h(22) : error C2061: syntax error : identifier 'intgold'

wich came from this code

	char name[32];


#6 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 20 August 2009 - 08:32 PM

I think the error is actually in the line in front of that. You need a colon after "private". Also, looks like you lost the space between 'int' and 'gold' in the declaration of 'int gold'.

BTW, you really should be able to figure out these sorts of tiny things yourself. Just remember that sometimes the error message will refer to the line AFTER the line where the error actually is (this is because C/C++ in general let you put whitespace or line breaks anywhere, so it doesn't know there's a problem until it reaches the next bit of code). You may want to get an introductory book about C/C++ and read it so you get more of a handle on the syntax. As you're finding out, programming languages are very unforgiving about typos. :)

As for why your IntelliSense doesn't work, could be the syntax errors are messing it up. Otherwise, check your Visual Studio settings and make sure it's not turned off.
reedbeta.com - developer blog, OpenGL demos, and other projects

#7 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 22 August 2009 - 02:51 PM

Hi again... i know you said i should read books and stuff on syntaxes... but i cant find out exactly what you mean,,, so if you have any good books, tutorials etc. would be great...

here is my problems annyway... thx i guess :)

------ Build started: Project: DopeGame2, Configuration: Debug Win32 ------
Compiling...
Player.cpp
c:\users\eir\documents\visual studio 2008\projects\dopegame2\dopegame2\player.cpp(98) : error C2065: 'newmaxmana' : undeclared identifier
c:\users\eir\documents\visual studio 2008\projects\dopegame2\dopegame2\player.cpp(114) : error C2039: 'SetStrenght' : is not a member of 'PLAYER'
c:\users\eir\documents\visual studio 2008\projects\dopegame2\dopegame2\player.h(3) : see declaration of 'PLAYER'
c:\users\eir\documents\visual studio 2008\projects\dopegame2\dopegame2\player.cpp(116) : error C2065: 'strength' : undeclared identifier
Build log was saved at "file://c:\Users\Eir\Documents\Visual Studio 2008\Projects\DopeGame2\DopeGame2\Debug\BuildLog.htm"
DopeGame2 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



#include "library.h"


void PLAYER::AddExperience(int amount)

{

	SetExperience(experience + amount);

}


void PLAYER::AddGold(int amount)

{

	SetGold(gold + amount);

}


void PLAYER::AddHealth(int amount)

{

	SetHealth(health + amount);

}


void PLAYER::AddMana(int amount)

{

	SetMana(mana + amount);

}


int PLAYER::GetGold()

{

	return gold;

}


int PLAYER::GetHealth()

{

	return health;

}


int PLAYER::GetIntelligence()

{

	return intelligence;

}


int PLAYER::GetMana()

{

	return mana;

}


int PLAYER::GetMaxHealth()

{

	return maxhealth;

}


int PLAYER::GetMaxMana()

{

	return maxmana;

}


char* PLAYER::GetName()

{

	return name;

}


int PLAYER::GetStength()

{

	return strength;

}


void PLAYER::SetExperience(int newexperience)

{

	experience = newexperience;


	//check for any level ups

}


void PLAYER::SetGold(int newgold)

{

	gold = newgold;

}


void PLAYER::SetHealth(int newhealth)

{

	health = newhealth;

}


void PLAYER:SetIntelligence(int newint)

{

	intelligence = newint;

}


void PLAYER::SetMana(int newmana)

{

	mana = newmana;

}


void PLAYER::SetMaxHealth(int newmaxhealth)

{

	maxhealth = newmaxhealth;

}


void PLAYER::SetMaxMana(int newmana)

{

	maxmana = newmaxmana;

}


bool PLAYER::SetName(char *newname)

{

	if (strlen(newname) == 0)

		return false;


	if (strlen(newname) > 32)

		return false;


	strcpy(name,newname);


	return false

}


void PLAYER::SetStrenght(int newstr)

{

	strength = newstr;

}





class PLAYER

{

private:

	char name[32];

	int strength;

	int intelligence;

	int gold;

	int experience;

	int health;

	int maxhealth;

	int mana;

	int maxmana;

	int level;

public:

	bool SetName(char* newname);

	char* GetName();

	void SetStrength(int nwestr);

	int GetStength();

	void SetIntelligence(int newint);

	int GetIntelligence();

	void SetGold(int gold);

	void AddGold(int amount);

	int GetGold();

	void SetExperience(int newexperience);

	void AddExperience(int amount);

	int GetExperience();

	void SetHealth(int newhealth);

	void AddHealth(int amount);

	void LooseHealth(int amount);

	int GetHealth();

	void SetMaxHealth(int newmaxhealth);

	int GetMaxHealth();

	void SetMana(int newmana);

	void AddMana(int amount);

	void LooseMana (int amount);

	int GetMana();

	void SetMaxMana(int newmana);

	int GetMaxMana();	

};


#8 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 22 August 2009 - 03:46 PM

These are all simple typos. You can fix these yourself, if you just READ the errors and the code they're coming from. I'm not going to spoon-feed you the fixes.
reedbeta.com - developer blog, OpenGL demos, and other projects

#9 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 22 August 2009 - 04:01 PM

thx alot m8...

sorry for unnececery help-asking.. but i seem to always think that it is more complicated than it realy is :)

#10 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 22 August 2009 - 08:02 PM

hehe... here i am again :P
i seem to have another problem, probably nothing, but considering typos, i cant seem to find out what it is...

dopegame2\town.cpp(82) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'


thx ;)

#11 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 22 August 2009 - 09:36 PM

Yeah, that doesn't sound like a typo. Can you post the context (the code around where the error occurs)?
reedbeta.com - developer blog, OpenGL demos, and other projects

#12 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 22 August 2009 - 09:43 PM

ok, thats nice to hear :P

here is the code



#include "Library.h"


//TOWN



void Inn(PLAYER*);


bool Town(PLAYER* player)

{

	int choice = 0;


	while (choice != 5)

	{

		cout << "\n\n\nYou walked into town.\n\n";

		cout << "All around you there are people going about their daily lives.\n";

		cout << "To your left you spot an INN and your right you spot the ARMORY and ITEM SHOP\n\n";


		Wait();


		cout << endl;


		cout << "What is your choice?\n";

		cout << "1: Enter the Lucky Pig INN\n";

		cout << "2: Enter the ARMORY\n";

		cout << "3: Enter the ITEM SHOP\n";

		cout << "4: Check your stats\n";

		cout << "5: Leave Town\n";


		cout << ">";


		cin >> choice;


		cout << endl;


		switch (choice)

		{

		case 1:

			{

				Inn(player);

				break;

			}

		case 2:

			{

				//Armory();

			}

		case 3:

			{

				//ItemShop();

				break;

			}

		case 4:

			{

				//Stats();

				break;

			}

		default: break;

		}

	}

	return true;

}


void Inn(PLAYER* player)

{

	cout << "You are now in the INN. It is a vacant inn, exept for the man behind the counter.\n";

	cout << "There are currently nobody else staying here.\n\n";

	cout << "You walk over to the old man, he grins, delighted to see a visiter at last.\n";

	cout << "Hello and welcome to The Lucky Pig. Would you like to stay here for only 10 gold coins?.\n";


	char answer [5];

	answer [0] != "m";


	while (answer [0] != "y" || answer [1] != "n")

	{

		cout << "Stay (10 gold) y/n\n";

		cin >> answer;

	}


	cout << endl;


	if (answer[0] == "Y")

	{

		if (player->GetGold() >= 10)

		{

			player->SpendGold(10);

			cout << "You find a nice cosy bed, and fall fast asleep.\n";

			Wait();

			cout << "\n(HP and MP restored)\n\n";

			cout << "HP:" << player->GetHealth() << "/" << player->GetMaxHealth() << " ";

			cout << "MP:" << player->GetMana() << "/" << player->GetMaxMana() << "\n";

			player->SetHealth(player->GetMaxHealth());

			player->SetMana(player->GetMaxMana());

			Wait();

		}

		else

		{

			cout << "You don't have enough gold to stay here, you need at least 10 gold\n";

			Wait();

			answer[0] = "n";

		}

	}


	if (answer[0] == "n")

	{

		cout << "You turn around and walk back into the Town.\n";

		Wait();

	}

}


#13 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 23 August 2009 - 06:34 AM

Okay. I'm going to assume the error refers to one of the lines where you compare the 'answer' variable to a letter. C/C++ make a distinction between characters and strings. Single quotes create a character, like 'y', while double quotes create a string, like "y". A string one character long isn't the same thing as a character. Since answer is a char array, answer[0] is a character; so, you have to compare it to another character. So, all the cases where you write something like answer[0] == "Y", you should change to answer[0] == 'Y'.

Also note that you should be consistent about case. Sometimes you're using a capital Y and sometimes a lowercase y.

Also, line 70 doesn't do anything. If you want to set answer[0] equal to 'm', just use =, not !=.
reedbeta.com - developer blog, OpenGL demos, and other projects

#14 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2325 posts

Posted 23 August 2009 - 08:53 AM

I like your coding style, awesome starting project idea! :)

So I guess each place you can visit is a new function, interesting...
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#15 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 23 August 2009 - 04:31 PM

thanks :) i felt it was a good way to learn... you learn coding, and you see the results better. intresting ;) hehe

okey, here is the newest problem ^^

player.cpp(156) : error C4716: 'PLAYER::SpendGold' : must return a value


int PLAYER::SpendGold(int amount)

{

	SetGold(gold - amount);

}


#16 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2325 posts

Posted 23 August 2009 - 04:38 PM

write return 0 in it. or return "amount"
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#17 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 23 August 2009 - 04:40 PM

Or, declare the function as void instead of int, if you don't want to return any value.
reedbeta.com - developer blog, OpenGL demos, and other projects

#18 Plutonium

    New Member

  • Members
  • Pip
  • 9 posts

Posted 23 August 2009 - 05:38 PM

Thx alot !...
great help:)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users