Click to See Complete Forum and Search --> : I think this is another problem with strings again
coding_delight
July 10th, 2007, 12:32 PM
yesterday I went ahead of the strings and made a jump into classes so I am currently experimenting with constructors and destructors. I'm not sure what the problem with the followign code is although i have a feeling it involves the string library.
#include <iostream>
#include <cstring>
using namespace std;
class player
{
private:
int health;
string name;
int hp;
public:
void show ();
construct_player ();
};
player::construct_player ()
{
int new_health; string new_name; int new_hp;
cout << "please enter the name of your new player: \n";
cin >> new_name;
cout << "\n";
cout << "very good " << new_name << " now tell me your health: \n";
cin >> new_health;
cout << "\n";
cout << "last but not least, how much HP would you like to start with: \n";
cin >> new_hp; cout << "\n";
new_health = health;
new_name = name;
new_hp = hp;
}
void player::show ()
{
cout << "here are the stats of your new player: \n\n\n";
cout << health << "\n";
cout << name << "\n";
cout << hp << "\n";
}
int main ()
{
player new_player;
new_player.construct_player();
new_player.show();
return 0;
}
some one please help me correct this code :( thank you.
ZuK
July 10th, 2007, 12:36 PM
You didn't tell what's wrong so I will guess.
You didn't #include <string>
Kurt
Hnefi
July 10th, 2007, 12:48 PM
I see two critical problems:
1: You need to include <string>, not <cstring>.
2: The constructor of a class must have the same name as the class itself IIRC. construct_player() should be renamed to just player() or, if you want construct_player() to be a regular function member, you must give it a return type (such as void).
Obviously, if you change construct_player() to become the constructor of player(), you can't call the constructor explicitly so the line
"new_player.construct_player();"
must be deleted.
coding_delight
July 10th, 2007, 01:01 PM
I see two critical problems:
1: You need to include <string>, not <cstring>.
2: The constructor of a class must have the same name as the class itself IIRC. construct_player() should be renamed to just player() or, if you want construct_player() to be a regular function member, you must give it a return type (such as void).
Obviously, if you change construct_player() to become the constructor of player(), you can't call the constructor explicitly so the line
"new_player.construct_player();"
must be deleted.
thank you, the code actually executes now and im sorry i didn't state the problem, but now that the first problem has been solved another one came up. The output for my show() member function looks like some kind of ASCII code. I really hope this doesnt involve typecasting, if not what is the problem with the output?
#include <iostream>
#include <string>
using namespace std;
class player
{
private:
int health;
string name;
int hp;
public:
void show ();
player ();
};
player::player ()
{
int new_health; string new_name; int new_hp;
cout << "please enter the name of your new player: \n";
cin >> new_name;
cout << "\n";
cout << "very good " << new_name << " now tell me your health: \n";
cin >> new_health;
cout << "\n";
cout << "last but not least, how much HP would you like to start with: \n";
cin >> new_hp; cout << "\n";
new_health = health;
new_name = name;
new_hp = hp;
}
void player::show ()
{
cout << "here are the stats of your new player: \n\n\n";
cout << health << "\n";
cout << name << "\n";
cout << hp << "\n";
}
int main ()
{
player new_player;
new_player.show();
return 0;
}
laserlight
July 10th, 2007, 01:16 PM
Consider this code:
new_health = health;
new_name = name;
new_hp = hp;
Instead of assigning to the member variables, you are assigning the member variables to the local variables. In fact, you could use the member variables and not have local variables at all for that.
Oh, and use code tags when posting code.
Philip Nicoletti
July 10th, 2007, 01:16 PM
You never set the health, name, and hp variables.
At the end of the constructor, you have the assignments switched.
GNiewerth
July 10th, 2007, 02:49 PM
I see two critical problems:
1: You need to include <string>, not <cstring>.
2: The constructor of a class must have the same name as the class itself IIRC. construct_player() should be renamed to just player() or, if you want construct_player() to be a regular function member, you must give it a return type (such as void).
Obviously, if you change construct_player() to become the constructor of player(), you can't call the constructor explicitly so the line
"new_player.construct_player();"
must be deleted.
A class does not need an explicit constructor. If there´s no constructor declared (neither standard constructor nor specialized constructor) the compiler will implicitely generate a constructor. The two stage construction posted above should be working.
Hnefi
July 10th, 2007, 04:15 PM
A class does not need an explicit constructor. If there´s no constructor declared (neither standard constructor nor specialized constructor) the compiler will implicitely generate a constructor. The two stage construction posted above should be working.
It would, except his original code contained a function with no return type that was obviously meant as a constructor, though misnamed.
coding_delight
July 11th, 2007, 05:27 PM
ok everyone, so the code works well:
#include <iostream>
#include <string>
using namespace std;
class player
{
public:
player(int, string, int);
};
player::player(int new_health, string new_name, int new_hp)
{
cout << "welcome to the game: " << new_name <<"\n";
cout << "your stats are as follows: \n\n";
cout << "health: " << new_health << "\n";
cout << "name: " << new_name << "\n";
cout << "HP: " << new_hp << "\n";
}
int main ()
{
int health;
string name;
int hp;
cout << "please enter in the following information: \n\n";
cout << "health : \n";
cin >> health;
cout << "name : \n";
cin >> name;
cout << "HP : \n";
cin >> hp;
player hamid = player (health, name, hp);
return 0;
}
The code does succeed on running and my initial intention was to test out the concept of constructors, everything is A ok EXCEPT ONE THING :(. This string library is really getting on my nerves for if you run the program and input your name with a space or a symbol like a percentage it skips to the next command without answering. I am positive that this involves something like the cin.getline () function or the cin.get(), except i thought those were functions only intended for character arrays. SOMEONE please explain hwo to modify this code to avoid that problem because im quite confused with the cin.get() and cin.getline() functions and quite frankly, i thought those functions were only for arrays not string variables?????
NMTop40
July 12th, 2007, 09:30 AM
there are two ways you can get it to not skip the space.
You can go to all the effort of resetting the locale such that the space character is not considered whitespace.
The far simpler way is to use std::getline( cin, name );
Note that you can do the same with numbers as well but it requires more steps, however with inputting numbers with cin you must be aware that if the user enters an invalid number you must reset the stream because it will be in a "failed " state until you reset it otherwise.
To convert from a string to a numeric value you use std::istringstream, and if the user has entered a bad value you can just create a new one next time around. Otherwise you must call cin.clear() and then possibly cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' ) to ensure it gets to the end of its input buffer.
Paul McKenzie
July 12th, 2007, 09:35 AM
ok everyone, so the code works well:You were told this before -- please use code tags when posting code!
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.