Help Out A Noob-Classes(C++)

Started by
15 comments, last by Dwiff 16 years, 8 months ago
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.
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.
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?
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...
void askName()
{
...
}

Should be:
void character::askName()
{
...
}

(In the first, you are creating a global function called askName, in the second, you are defining the character function askName).

See also: Scope resolution operator.
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();
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.
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].
Thanks for the help, it works fine now.
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.

This topic is closed to new replies.

Advertisement