Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:12:52+00:00 2026-06-18T05:12:52+00:00

One of my weaknesses is effectively using chars in C++ which is what I

  • 0

One of my weaknesses is effectively using chars in C++ which is what I am trying to do right now. I have a player class in my game and within the player class, I create a playerCard object which displays various information. This works fine for a single instance of the player object (i.e. Player player) but when I attempt to push_back a player object in to a vector it all goes wrong.

Basically, the program continues to run but the player doesn’t render to the screen. When I quit the program, I then get a breakpoint error when main tries to return MSG. The comment about the breakpoint reads:

    /*
     * If this ASSERT fails, a bad pointer has been passed in. It may be
     * totally bogus, or it may have been allocated from another heap.
     * The pointer MUST come from the 'local' heap.
     */
    _ASSERTE(_CrtIsValidHeapPointer(pUserData));

I have located the error to here

    strcat(nameCard, nameChar);
    strcat(nameCard, genderChar);
    strcat(nameCard, ageChar);
    strcat(nameCard, cashHeldChar);
    strcat(nameCard, productWantedChar);

within the playerCard class because when I comment this out, I do not get the error. Here is the full playerCard class (Again, it is messy and probably the wrong way for going about things but I am trying to get my head round using chars/strings etc)
#include “Headers.h”;

class Playercard{

private:

    RECT textbox;
    LPD3DXFONT font;

    std::string nameStr;
    std::string genderStr;
    std::string ageStr;
    std::string cashHeldStr;
    std::string prodWantedStr;

    char nameCard[1000];

public:

    Playercard()
    {
    }

    void load(char* name, bool male, int age, double cash, char* prod)
    {

        if(male)
        {
            genderStr = "Gender: Male\n";
        }
        else
        {
            genderStr = "Gender: Female\n";
        }

        nameStr = "Name: " + static_cast<std::ostringstream*>( &(std::ostringstream() << name))->str() + "\n";
        ageStr = "Age: " + static_cast<std::ostringstream*>( &(std::ostringstream() << age))->str() + "\n";
        cashHeldStr = "Cash Held: " + static_cast<std::ostringstream*>( &(std::ostringstream() << cash))->str() + "\n";
        prodWantedStr = "Product Wanted: " + static_cast<std::ostringstream*>( &(std::ostringstream() << prod))->str() + "\n";

        char * nameChar = new char [nameStr.length()+1];
        char * genderChar = new char [genderStr.length()+1];
        char * ageChar = new char [ageStr.length()+1];
        char * cashHeldChar = new char [cashHeldStr.length()+1];
        char * productWantedChar = new char [prodWantedStr.length()+1];

        strcpy(nameChar, nameStr.c_str());
        strcpy(genderChar, genderStr.c_str());
        strcpy(ageChar, ageStr.c_str());
        strcpy(cashHeldChar, cashHeldStr.c_str());
        strcpy(productWantedChar, prodWantedStr.c_str());

        strcat(nameCard, nameChar);
        strcat(nameCard, genderChar);
        strcat(nameCard, ageChar);
        strcat(nameCard, cashHeldChar);
        strcat(nameCard, productWantedChar);

        diagFile.open("Diag.txt");
        diagFile.write("Test", 100);
        diagFile.close();
    }

    void setUp(int L, int T, int R, int B)
    {
        SetRect(&textbox, L,T,R,B);
    }

    void draw()
    {
        font->DrawTextA(d3dSprite, nameCard, -1, &textbox, DT_LEFT, D3DCOLOR_XRGB(255, 255, 255));
    }

    LPCSTR plCard()
    {
        return nameCard;
    }
};

Any help would be greatly appreciated. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:12:53+00:00Added an answer on June 18, 2026 at 5:12 am

    Your main problem is that nameCard is uninitialized. strcat requires a null-terminated string to do its magic, and there’s no guarantee that the first, or any, character in nameCard is a null.

    However, C strings are unnecessary. Just use std::string all the time. After changing nameCard to a string, I’d change load to (file writing excluded):

    void load(const std::string &name, bool male, int age, double cash, const std::string &prod)
    {
        nameStr = "Name: " + name + "\n";
        genderStr = "Gender: " + (male ? "Male" : "Female") + "\n";
        ageStr = "Age: " + std::to_string(age) + "\n";
        cashHeldStr = "Cash Held: " + std::to_string(cash) + "\n";
        prodWantedStr = "Product Wanted: " + prod + "\n";
    
        nameCard = nameStr + genderStr + ageStr + cashHeldStr + prodWantedStr;
    }
    

    I would actually just make nameCard a data member, removing the others, and use this:

    nameCard.clear();
    nameCard += "Name: " + name + "\n";
    //add on other parts
    

    Other than that, make plCard() return a std::string and in draw(), use nameCard.c_str(). I hope that clears up what you can do with strings a bit more.

    Do note, however, that std::to_string is C++11. C++03 has two common solutions:

    std::string str = boost::lexical_cast<std::string>(someNumber);
    

    Or

    std::ostringstream oss;
    oss << someNumber;
    std::string str = oss.str();
    

    I find the three-liner much more readable than a one-liner or two-liner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One can say a type parameter T must have a specific supertype S_1: class
One of the possible forms of template parameter is a class template. The C++
One useful tip I've been using for XCode is adding breakpoints on exceptions .
One car must have one owner, and an owner can have many cars.
One has txtDateReceived and second has txtVendorPackDate. Before insert will add record I have
One of my sites has a lot of users. They currently each have one
Which tasks would be better suited to using NSOperation as opposed to using GCD
One can iterate through a SortedMap by using the iterator from myMap.entrySet().iterator() . But
Here's the sample C code that I am trying to accelerate using SSE, the
One feature I miss tremendously now I switched from Visual Studio to MonoDevelop (for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.