CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2007
    Posts
    11

    Unhappy I think this is another problem with strings again

    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: I think this is another problem with strings again

    You didn't tell what's wrong so I will guess.
    You didn't #include <string>
    Kurt

  3. #3
    Join Date
    Jul 2003
    Location
    Linköping, Sweden
    Posts
    261

    Re: I think this is another problem with strings again

    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.
    Errare humanum est, ergo non sum humanus.

  4. #4
    Join Date
    Jul 2007
    Posts
    11

    Re: I think this is another problem with strings again

    Quote Originally Posted by Hnefi
    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:layer ()
    {
    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;


    }

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: I think this is another problem with strings again

    Consider this code:
    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: I think this is another problem with strings again

    You never set the health, name, and hp variables.

    At the end of the constructor, you have the assignments switched.

  7. #7
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: I think this is another problem with strings again

    Quote Originally Posted by Hnefi
    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.
    - Guido

  8. #8
    Join Date
    Jul 2003
    Location
    Linköping, Sweden
    Posts
    261

    Re: I think this is another problem with strings again

    Quote Originally Posted by GNiewerth
    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.
    Errare humanum est, ergo non sum humanus.

  9. #9
    Join Date
    Jul 2007
    Posts
    11

    Unhappy Re: I think this is another problem with strings again

    ok everyone, so the code works well:

    #include <iostream>
    #include <string>

    using namespace std;

    class player

    {

    public:
    player(int, string, int);



    };

    player:layer(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?????

  10. #10
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: I think this is another problem with strings again

    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.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: I think this is another problem with strings again

    Quote Originally Posted by coding_delight
    ok everyone, so the code works well:
    You were told this before -- please use code tags when posting code!

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured