Hello today i was programing my text game, and for some reason this error keep showing up: a function-definition is not allowed here before '{' token. Here is the code:
void battle(int HP, int atk, int def, string monster){
int attackType = 0;
int iDamage;
int damageG;
cout << "You decide to attack the " << monster << endl;
cout << "1.Do a strong attack, 2.Do a AOE attack." << endl;
cin >> attackType;
cout << "You chose attack Nr." << attackType << endl;
iDamage = 50 - 5 + atk;
}
The very first line is the problem(the error shows in the first line). If you need the full code tell me. I hope sombody will help me.
This usually happens because you are trying to define a function inside another function. (Which is normally because you are missing a { or } somewhere, so I would double check all of those.
There is nothing directly wrong with that function, so posting the rest of the code may show the problem.
void stats(int HP,int atk,int def);
void items(string item);
void battle(int HP, int atk, int def, string monster);
int main()
{
string Ppas;
string Apas;
string Tpas;
string Kpas;
string monster;
int Pepas;
int HP = 50;
int atk = 0;
int def = 2;
string dress = "Dress";
cout << "Padaryta Dominyko Litvaicio (RoterDOX), ver 0,1" << endl;
cout << "Text RPG" << endl;
cout << "You meet a mysterious man in a cave" << endl;
cout << "He tell you a tale about a dragon that attacked his city." << endl;
cout << "He went to this cave and hided until now." << endl;
cout << "He wants you to kill the dragon, and let him rebuild the city." << endl;
cout << "He gives you a sword that increases your atack(atk) +5." << endl;
atk +=5;
cout << "To see your stats write in /stats." << endl;
cout << "If you want to continue just write /con" << endl;
cin >> Ppas;
if(Ppas == "/stats"){
stats(HP, atk, def);
}
if(Ppas == "/con"){
cout << "" << endl;
}
cin >> Apas;
cout << "You leave the old and mysteriuos man, and venture deeper in to the cave." << endl;
cout << "While walking you see a dead body." << endl;
cout << "You could loot it white the command: /loot" << endl;
cin >> Tpas;
if(Tpas == "/loot"){
items(dress);
cout << "Would you like to equip it? Then just write /equip (item name)." << endl;
cin >> Kpas;
if(Kpas == "/equip"){
cout << "You are now wearing a dress that gives you +2 def" << endl;
def +=2;
}
if(Kpas == "/no"){
cout << "" << endl;
}
if(Tpas == "/no"){
cout << "" << endl;
}
cout << "You go deeper into the cave." << endl;
cout << "You see a walking Skeleton !" << endl;
cout << "The Skeleton looks at you and charges you!!" << endl;
cout << "What should you do?" << endl;
cout << "1.Attack the skeleton. \n2.Try to run away." << endl;
cout << "Write the number of your choise: ";
cin >> Pepas;
if(Pepas == 1){
battle(HP, atk, def, "Skeleton");
}
if(Pepas == 2){
}
}
void battle(int HP, int atk, int def, string monster){
int attackType = 0;
int iDamage;
int damageG;
cout << "You decide to attack the " << monster << endl;
cout << "1.Do a strong attack, 2.Do a AOE attack." << endl;
cin >> attackType;
cout << "You chose attack Nr." << attackType << endl;
iDamage = 50 - 5 + atk;
}
void stats(int HP,int atk,int def){
cout << "Hp: " << HP << endl;
cout << "atk: " << atk << endl;
cout << "def: " << def << endl;
}
void items(string item){
cout << "You looted the body and got:" << endl;
cout << item << endl;
}
Here is the code, i just cant find it.