As practice for Classes, I decided to create a program that would take user input regarding a characters stats in a game and display them back to the user.The problem is after I input the stats, cout<<champion1.name doesn't display the string I entered and <<chanmpion1.health returns a large negative number (-85993460). To me it seems like there is something wrong with the way the program stores the info I entered.
#include <iostream>
#include <string>
usingnamespace std;
class champStats{
public:
string name;
int health;
int mana;
void getStats(string name, int health, int mana)
{
cout << "Enter the champion name, health and mana, in that order." << endl;
cin >> name >> health >> mana;
}
};
int main()
{
champStats champion1;
champion1.getStats(champion1.name, champion1.health, champion1.mana);
cout << champion1.name << champion1.health;
system("pause");
}
Before this I actually whipped this code up and It worked fine.
#include <iostream>
#include <string>
usingnamespace std;
class champStats{
public:
string name;
int health;
int mana;
void printStats(int health, int mana)
{
cout << name << " has the stats of:" << endl << "Health:" << health << endl << "Mana:" << mana << endl;
}
};
int main()
{
champStats champion1;
cout << "Enter the champion name, health and mana in that order." << endl;
cin >> champion1.name >> champion1.health >> champion1.mana;
champion1.printStats(champion1.health, champion1.mana);
system("pause");
}
But it seems the moment I created my void getStats everything went wrong.
I don't have the function void printStats on the first block of code I posted because I couldn't even get cout <<champion1.health to work. Some help would be much appreciated.
One of the useful purposes of classes is that it implicitely passes needed member variables in to the function. champion1's health and mana is already stored in champion1.
Just declare your member function like this.
1 2 3 4
void printStats()
{
cout << name << " has the stats of:" << endl << "Health:" << health << endl << "Mana:" << mana << endl;
}
It automatically knows that "health" and "mana" are properties in your class (some people like to mark these with a m or m_ before the real name).
EDIT: For some reason I missed the whole first code snippet. What I said still applies, but your specific problem was that you didn't initialize any property in your class in the first code.
1 2 3 4
champStats champion1;
//Nothing is initialize yet...
//Trying to print out uninitialized values, so you're gonna get junk:
champion1.getStats(champion1.name, champion1.health, champion1.mana);
As an aside, I would rather make the variables first, and then feed them into a champStats constructor when you make the object, but initializing the properties and then using a printStats() function should work just fine.
#include <iostream>
#include <string>
usingnamespace std;
class champStats{
public:
string name;
int health;
int mana;
void getStats(string name, int health, int mana)
{
/*name = "";
health = 0;
mana = 0; I've tried initializing here. Gives me large negative numbers like before*/
cout << "Enter the champion name, health and mana, in that order." << endl;
cin >> name >> health >> mana;
}
void printStats(string name, int health, int mana)
{
cout << name << " has the stats of:" << endl << "Health:" << health << endl << "Mana:" << mana << endl;
}
};
int main()
{
champStats champion1;
/* champion1.name = "";
champion1.health = 0;
champion1.mana = 0; I've also tried initializing here. Display gives me */
//I don't get the name I input and I get values of 0 for health and mana.
champion1.getStats(champion1.name, champion1.health, champion1.mana);
champion1.printStats(champion1.name,champion1.health,champion1.mana);
system("pause");
}
What I don't get is how on the second block of code in my first post, I don't think I did any initialization and it worked fine.
I WAS ABLE TO SOLVE MY PROBLEM.
I used Ganado's tip on declaring my void function because my champion.health and mana were already stored. Thanks a lot for the help Ganado.
#include <iostream>
#include <string>
usingnamespace std;
class champStats{
public:
string name;
int health;
int mana;
void setStats()
{
cout << "Enter the name, health and mana, in that order." << endl;
cin >> name >> health >> mana;
}
void printStats()
{
cout << name << " has the stats of:" << endl << "Health:" << health << endl << "Mana:" << mana << endl;
}
};
int main()
{
champStats champion1;
champion1.setStats();
champion1.printStats();
system("pause");
}
Classes are great and all, but if all you're doing is storing and retrieving variables, you might want to look into a struct. Classes are more for function oriented code, and in games, unless you have some very unique characters that have totally different calculations done with their stats, you can generally throw all their data into a struct.
I used to use classes for everything too, and never looked much into structs, but when I finally used them I much prefer using a struct over a class when given the opportunity, since they are just easier to work with. Try tossing all your data from stack to heap with a class and then a struct, and you'll see what I mean ;)
Structs are the same as classes in C++ except structs are public by default. Of course, a comon use case is to only use structs for POD data like in C.