Thread: this is ........ing me off severly

  1. #1
    Unregistered
    Guest

    this is ........ing me off severly

    void CPlayer::CreatePlayer()
    {
    char *playtemp;
    cout<<"Enter Player Name: ";
    cin>>playername;
    cout<<"Enter Password: ";
    cin>>pass;
    playtemp = playername;
    strcat(playtemp,".plr");
    ofstream fout(playtemp,ios::binary);
    fout<<playername<<pass;
    fout.close();
    }

    Why dosen't that work?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    can you post your class dec too.

    what exactly do you mean by dont work?
    are you getting any compiler errors ?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    It gives me an access violation in strcat()...i don't know why....and ofstream acts as if its not even there...and it fails to even ask for the password.......so there you go

    class CPlayer
    {
    public:
    CPlayer();
    virtual ~CPlayer();
    void CreatePlayer();

    protected:
    char *playername;
    char *pass;
    };

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    There appear to be two errors here. First, you can't assign one string to another, you need to use strcpy()--or a variant thereof-- or char by char assignment. Second playertemp has no memory assigned to it. Therefore you may need to give it some before you can use it in strcpy(). You could do this by using the new operator or changing the declaration of playertemp to something like:

    char playertemp[80];

    the same argument would hold for playername.
    Last edited by guest; 11-27-2001 at 09:47 AM.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You have char* pointers as members. Remember this is exactly what it is... itis a pointer. It doesn't point to anything until you give it some memory to point at.In your constructor you should use new[] to provide memory for the pointer to point to. This should be freed in the destructor using delete[] .

    Read this thread it covers a very similar topic.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed