Jump to content
  • Advertisement
Sign in to follow this  
metal_kid43

text RPG

This topic is 4726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so im making this cheesey text RPG game. It's basically my second major project as far as c++ goes and i've run into some trouble. Basically the user inputs name, race, and gender and the program then spits the info back at them, but i cant get it to distinguish if the user is a mage, warrior, or cleric. Any suggestions would be greatly appreciated! THanks. #include <iostream.h> using namespace std; void warrior_statstable(); struct player { char name[10]; char race[10]; char gender[1]; }; struct stats { int level; int hp; int strength; int intelligence; int charisma; }; int main() { stats warrior = { 1, 100, 10, 3, 3, }; player * pl = new player; cout << "Enter charecter name: "; cin >> (*pl).name; cout << "Enter your Race (The Name)\n"; cout<< "1. Warrior\n"; cout<< "2. Mage\n"; cout<< "3. Cleric\n"; cin.get((*pl).race[10]); if ((*pl).race[0] == 'W'||'w') cout << "Here are your stats " << (*pl).name << " Warrior." << "\n"; else if ((*pl).race[0] == 'C'||'c') cout << "Here are your stats " << (*pl).name << " Cleric."<<"\n"; else if ((*pl).race[0] == 'M'||'m') cout << "Here are your stats " << (*pl).name << " Mage." << "\n"; else cout << "Error in your race!" <<"\n"; char gender; cout<< "Male or Female?"<<"\n"; cin >> (*pl).gender; cout<< "Name: "<< (*pl).name << "\n"; cout<< "Race: "<< (*pl).race << "\n"; cout<< "Gender: "<< (*pl).gender << "\n"; delete pl; system("pause"); return 0; } void warrior_statstable() { cout << "\n"; cout << "Level |" << "\n"; cout << "------------|--"<< "\n"; cout << "Hp |" << "\n"; cout << "------------|--"<< "\n"; cout << "Strength |" << "\n"; cout << "------------|--"<< "\n"; cout << "Intelligence|" << "\n"; cout << "------------|--"<< "\n"; cout << "Charisma |" << "\n"; }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I dont get your question, first of all you should enumerate a list of races and then you could list them like: 1) mage 2)warrior 3)zombie ect... and then switch the variable that the user would type in the number of.

Share this post


Link to post
Share on other sites
cin.get((*pl).race[10]);
if ((*pl).race[0] == 'W'||'w')

I really don't think you accutually want [10] there.. It means you only read one char, and that at position 10 (which will accutually just overwrite some other data, since the last position is 9).. So, remove the [10] part.. :p

[edit]
Why do you even use cin.get() instead of cin >>?

Also..

char gender;
cout<< "Male or Female?"<<"\n";
cin >> (*pl).gender;

What's that char gender; doing there?

Share this post


Link to post
Share on other sites
ok, i changed it to cin and it still wont distinguish between race inputs. is my loop incorrect? hmmm. . . *reminder* i AM a newb


#include <iostream.h>
using namespace std;
void warrior_statstable();
struct player
{
char name[10];
char race[10];
char gender[1];
};
struct stats
{
int level;
int hp;
int strength;
int intelligence;
int charisma;
};

int main()
{
stats warrior =
{
1,
100,
10,
3,
3,
};
player * pl = new player;
cout << "Enter charecter name: ";
cin >> (*pl).name;
cout << "Enter your Race (The Name)\n";

cout<< "1. Warrior\n";
cout<< "2. Mage\n";
cout<< "3. Cleric\n";

cin >> ((*pl).race);
if ((*pl).race[0] == 'W'||'w')

cout << "Here are your stats " << (*pl).name << " Warrior." << "\n";


else
if ((*pl).race[0] == 'C'||'c')
cout << "Here are your stats " << (*pl).name << " Cleric."<<"\n";


else
if ((*pl).race[0] == 'M'||'m')
cout << "Here are your stats " << (*pl).name << " Mage." << "\n";
else
cout << "Error in your race!" <<"\n";

char gender;
cout<< "Male or Female?"<<"\n";
cin >> (*pl).gender;

cout<< "Name: "<< (*pl).name << "\n";
cout<< "Race: "<< (*pl).race << "\n";
cout<< "Gender: "<< (*pl).gender << "\n";

delete pl;

system("pause");
return 0;
}
void warrior_statstable()
{
cout << "\n";
cout << "Level |" << "\n";
cout << "------------|--"<< "\n";
cout << "Hp |" << "\n";
cout << "------------|--"<< "\n";
cout << "Strength |" << "\n";
cout << "------------|--"<< "\n";
cout << "Intelligence|" << "\n";
cout << "------------|--"<< "\n";
cout << "Charisma |" << "\n";
}




Share this post


Link to post
Share on other sites
A little advice:
use #include <iostream>
rather than <iostream.h>

There are some minor differences between the two - iostream is part of the standard for c++, but I think iostream.h is included for compatibility with older code (though there are more reasons than this)

I'd also suggest using
#include <string>
string name;
string race;
string gender;

rather than
char name[10];
char race[10];
char gender[1];

It might make your life a little easier

[Edited by - Will F on June 16, 2005 3:41:56 PM]

Share this post


Link to post
Share on other sites
if ((*pl).race[0] == 'W'||'w')

What this does is check if ((*p1).race[0] == 'W' evaluates as "true" or if 'w' evaluates as true. In C/C++ any non-zero number is considered "true", and as characters such as 'w' have a non-zero value your if statements will always evaluate as true.

if((*p1).race[0] == 'W' || (*p1).race[0] == 'w')
is what you need.

Share this post


Link to post
Share on other sites
Quote:
Original post by Will F

I'd also suggest using
#include <string>
string name;
string race[10];
string gender[1];


I'm sure this is a typo, but you don't need the brackets. Just

string name;
string race;
string gender;

Also metal_kid instead of using (*p1).race
use p1->race

It is much easier to read!

Share this post


Link to post
Share on other sites
First of all, remove the

char gender;

your not using it. The reason why the gender is crashing is because there is only one position in the gender array. cin is crashing because, say you type in 'm', now the 'm' character and the terminating null character have to be put into a char array with one position. Not going to work:) change the size of the gender array to 2, or even 7, so the user can type in the full string "Male" or "Female".

Hope that helps,



-Dev578

Share this post


Link to post
Share on other sites
Use std::string instead of character arrays.

The reason that they are character arrays instead of null terminated strings is because there is no null character.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!