#include <cstdlib>
#include <iostream>
#include <string>
int name();
int race();
int main()
{
usingnamespace std;
cout<<"Welcome...Who are you again?"<<endl;
name();
cout<<"And your race is? Elf, Human, or Dwarf."<<endl;
race();
system("PAUSE");
}
int name() //begin game, insert player name
{
usingnamespace std;
char NAME[30]; // sets the limit to how long the name can be
cin>>NAME; //player name
cout<<"Ah, Hello, "<<NAME<<". "<<endl;
return 0;
}
int ELF()
{
int hp=60;
int mp=15;
int lvl=1;
int exp=0;
int mhp=60;
int mmp=15;
int mexp=100;
int STR=4;
int DEF=6;
int SPD=7;
int INT=7;
int stat_points=3;
}
int HUMAN()
{
int hp=50;
int mp=10;
int lvl=1;
int exp=0;
int mhp=50;
int mmp=10;
int mexp=100;
int STR=5;
int DEF=5;
int SPD=5;
int INT=5;
int stat_points=3;
}
int DWARF()
{
int hp=55;
int mp=15;
int lvl=1;
int exp=0;
int mhp=55;
int mmp=15;
int mexp=100;
int STR=7;
int DEF=6;
int SPD=3;
int INT=4;
int stat_points=3;
}
int race()
{
usingnamespace std;
char RACE[10];
int ifs=0;
int elses=0;
cin>>RACE;
if(RACE=="elf")
ELF();
elseif(RACE=="human")
HUMAN();
if(RACE=="dwarf")
DWARF();
else
{
cout<<"Invalid response. Please try again."<<endl;
race();
}
}
I'm trying to get the code to take the choice of race the person takes, and activate the specific code for that input. everything else works except the race() code.
all of those variables in your dwarf, elf and human functions are local variables i.e. they will die as soon as the function has finished. You need to assign those values to something.
Maybe create a class called PlayerCharacter or something like that?
Also you've told the compiler to return an integer for those functions but you dont actually return anything from those 3 functions.
edit: on line 14 you call race(), but you dont even have a mechanism to capture the user's input (what race he or she is), and then to pass this information into your race() method.
Lines 85, 87, 89 you're doing a string compare, but RACE is a character array. You need to change RACE to type string, or use the C-style strcmp function.
//First make a universal class that every race has in common (a body, or soul, or player)
struct Body{
//List everything a player will have
int health;
int speed;
int level;
string race;
};
//struct player comes before main, as a global variable
int main(){
//First, create your player
Body Player;
....
WELCOME TO THE GAME
....
....
NOW ENTER YOUR RACE
std::string RACE;
....
....
while(std::cin >> RACE)
{
if(RACE == "dwarf")
DWARF(&Player);
break;
ELSE IF(RACE == "elf")
ELF(&Player);
break;
ELSE IF(Race == "human")
HUMAN(&Player);
break;
ELSE
cout << "Please enter a valid race"
}
//End of Main
And one of your functions for the races should look like this:
(This goes beforeoutsidemain, like you had it)
//This function takes a reference to a variable of type "Body", and returns nothing
(If you adjust the values of body, through reference, then the original gets affected and you wont need to return any values.)
void ELF(Body& user)
{
user.health = 60;
user.speed = 7;
user.level = 1;
user.race = "Elf"
}
void HUMAN(Body& user)
{
user.health = 50;
user.speed = 5;
user.level = 1;
user.race = "Elf"
}
.....
.....
Anyways, if u wanna make that game into a group project, id be willing to help you out a bit =] Shoot me a private message if ur interested
@Chillieman : Actually, this is all just for fun and a way for me to learn. I'm the type of learner that needs to do not read... what i dont know/understand, i look up. doesnt help, then i ask. But i really do appreciate the offer.
No doubt, that writing code is the BEST way to learn code. the only reason I asked, is because once I was in a project with a group, and I learned a lot.