Jump to content
  • Advertisement
Sign in to follow this  
CarlosNYM

Help Out A Noob-Classes(C++)

This topic is 4162 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

So, I'm learning C++ and I have gotten to classes and ussually during the chapters I write up some dumb little program that uses what I am learning to see if I am getting it and apprently i'm not. I was making a little program that was going to be a like a character creation program in a MUDD. You pick a name and you pick your stats but its completely doesnt work. I know i'm totally off base so I need some help. #include<iostream> using namespace std; class character { public: char name; int level; int hp; int mp; int atk; int def; void askName(); int getHp(); int getMp(); int getatk(); int getdef(); }; void askName() { cin >>name; cout << "\n\n You choose " << name; } int main() { character dude; cout<<"Welcome to my RPG! \n\n"; cout<<"What is your name " << dude.askname(name); system("pause"); } It's not done yet but even this snippet doesnt work so any help will be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Quote:

cout<<"What is your name " << dude.askname(name);


character::askName
1) has a capital N
2) does not take in a paramter (which you do not define before trying to pass in)
3) returns void (not something that can be streamed out), and does its own output

Try this:

int main()
{
character dude;
cout<<"Welcome to my RPG! \n\n";
cout<<"What is your name? ";
dude.askName();
system("pause");
}

Edit: I also just noticed that name is a char. I think you want it to be a std::string (don't forget to #include <string> at the top of the file, too), not a single character.

Share this post


Link to post
Share on other sites
Your askname() should probably be askName(), since function names are generally case sensitive. I'm also not convinced that you can chain the askName() into the cout like that, but I don't know enough about C++ to make an authoritative statement and I don't have a C++ compiler readily available.

Edit: beaten to the punch [grin]

Edit2: Guys, don't you need to initialize "dude" before calling all that code? Or is askName() static?

Share this post


Link to post
Share on other sites
Well I made the changes(except for the string thing cause at the moment im just trying to find the problem with the class)and it says once it hit the cin << name; line that name is undeclared but I have declared it...

Also in this situation what am I supposed to make private and what should be public. Classes are really confusing...

Share this post


Link to post
Share on other sites
Im a begginer too but I can help.
Instead of using char use string.
and...

dont use askName() function with cout<< since that function is void.
instead of doing:

cout << "What is your name "<< askName();

do it like:

cout << "What is your name ";
askName();

Share this post


Link to post
Share on other sites
Quote:
Original post by lightbringer
Edit2: Guys, don't you need to initialize "dude" before calling all that code? Or is askName() static?


dude is declared at the beginning of main:
character dude;

This invokes character's default constructor, so the object is usable.

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
dude is declared at the beginning of main:
character dude;

This invokes character's default constructor, so the object is usable.

Oops. Big difference from Java, I should've googled it first [grin].

Share this post


Link to post
Share on other sites
here is what it should be...hope this helps you out.


//main.cpp
#include <iostream>
#include <string>

using namespace std;

class character
{
public:
void askName();
int getHp();
int getMp();
int getatk();
int getdef();
private: //can even do protected
string name;
int level;
int hp;
int mp;
int atk;
int def;
};

void character::askName()
{
cout <<"What is your name?"<<endl;
getline(cin,name);
cout <<"You choose "<<name<<endl;
}

int main()
{
character dude;
cout<<"Welcome to my RPG! \n\n";
dude.askName();
system("pause");
return 0;
}





oh, you already solved it.....
well, this uses strings.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!