PDA

View Full Version : [HELP] First c++ text based game :)


Plutonium
08-20-2009, 09:37 AM
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(2 0) : error C2143: syntax error : missing ')' before ';'
1>c:\users\liv\documents\visual studio 2008\projects\dopegame2\dopegame2\crossroads.cpp(4 8) : error C2059: syntax error : ')'
1>Build log was saved at "file://c:\Users\Liv\Documents\Visual Studio 2008\Projects\DopeGame2\DopeGame2\Debug\BuildLog.h tm"
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

Reedbeta
08-20-2009, 10:13 AM
Hi Plutonium, the code tags are .... 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.

Plutonium
08-20-2009, 11:56 AM
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 :)

Reedbeta
08-20-2009, 12: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 (http://en.wikipedia.org/wiki/IntelliSense)"). It should be turned on by default in Visual Studio, if you've set up a project file and solution.

Plutonium
08-20-2009, 12: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];

Reedbeta
08-20-2009, 01: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.

Plutonium
08-22-2009, 07:51 AM
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.h tm"
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();
};

Reedbeta
08-22-2009, 08:46 AM
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.

Plutonium
08-22-2009, 09:01 AM
thx alot m8...

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

Plutonium
08-22-2009, 01: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 ;)

Reedbeta
08-22-2009, 02:36 PM
Yeah, that doesn't sound like a typo. Can you post the context (the code around where the error occurs)?

Plutonium
08-22-2009, 02: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();
}
}

Reedbeta
08-22-2009, 11:34 PM
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 !=.

rouncer
08-23-2009, 01:53 AM
I like your coding style, awesome starting project idea! :)

So I guess each place you can visit is a new function, interesting...

Plutonium
08-23-2009, 09:31 AM
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);
}

rouncer
08-23-2009, 09:38 AM
write return 0 in it. or return "amount"

Reedbeta
08-23-2009, 09:40 AM
Or, declare the function as void instead of int, if you don't want to return any value.

Plutonium
08-23-2009, 10:38 AM
Thx alot !...
great help:)