Archived

This topic is now archived and is closed to further replies.

Problem printing text with std::string

This topic is 6218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all... I'm trying to print text to the screen that displays the players name. I'm NOT using a console app, I'm using WIN32 API windows app. And I'm coding with VC6++. What I've got so far for this part is a player struct that has player variables...
  struct Player { int ID; std::string PlayerName; int Points; etc... }player[10]; 
Then I have a file that stores the actual data...
  void PlayerData() { player[0].ID = 0; player[0].Name = "Chris"; etc... } 
Now when I try to print it out to window I don't get compiler or linker error, but I get either gibberish or the program crashes. the code I use to print to the screen is this...
  HDC hdc; char buffer[80]; hdc = GetDC(G1.hWnd); SetBkColor(hdc, RGB(0, 0, 0)); SetTextColor(hdc, RGB(255, 255, 255)); sprintf(buffer,"ID: %d, %s has %d total points", player[0].ID, player[0].Name, player[0].Points); TextOut(hdc, 5, 200, buffer, strlen(buffer)); ReleaseDC(G1.hWnd, hdc); 
I tried taking out the string to see if the rest worked and the ints worked fine. I even tested the whole formula in a console app using cout and that worked so I've come to the conclusion that the std::string data type isn't compatible with either sprinf or TextOut. If anyone could help I'd be incredibly grateful... Thanks in advance... Edited by - Alien Tesh on June 12, 2001 12:16:32 AM Edited by - Alien Tesh on June 12, 2001 12:17:06 AM Edited by - Alien Tesh on June 12, 2001 12:18:21 AM Edited by - Alien Tesh on June 12, 2001 12:21:47 AM

Share on other sites
I'm probably as experienced as you but I will try to answer anyways...

IMHO, I think that you should use a standard character array(string) for your needs. Unless there are some clear advantages to using std::string that you didn't mention. You could also be missing a library like string.h(though you should have it using strlen() ).

If anyone else replies to this topic, what exactly does the :: operator do other than to define member functions of a class? Ive seen it used in a lot of ways and dont know what it does, and I've seen multiple syntaxes for it like:

::FunctionCall()

and std::string

and ios::nocreate

Edited by - EbonySeraph on June 12, 2001 12:35:35 AM

Share on other sites
You need to append ".c_str ()" to your player.name in the call to sprintf. This gives a const char *, which is what sprintf is expecting.

Since sprintf uses a variable number of arguments, there''s no type-checking on your parameter list other than that the first two arguments are a char * and const char *. You can put anything in the world after that. You happened to put a string, which I believe copies the string by value into the sprintf function (not sure if it passes by value or reference, I never use va_arg stuff for precisely this reason). Then it just treats your string like it''s a char* when in fact it''s a class. So you''ll get a byte-wise output of your string class until/if it hits the first 0 in memory. In my implementation, that would be 4 bytes for the npos, an allocator pointer, then the string pointer, etc., etc.

sorry, long story short: use string::c_str () to cast a std::string to a "C" string.

Share on other sites
quote:
Original post by EbonySeraph
IMHO, I think that you should use a standard character array(string) for your needs. Unless there are some clear advantages to using std::string that you didn''t mention. You could also be missing a library like string.h(though you should have it using strlen() ).

benefits of using std::basic_string:
- automatically grows to whatever size needed
- automatically frees memory when destroyed
- length stored internally so strlen takes constant, not linear time
- can hold nulls
- all the std::string operations you can do on it which are handy member functions, instead of searching through string.h for the right function
- the member functions are all inlined
- I think, but I''m not sure, that it does reference counting too.

liabilities of std::basic_string:
- 16-byte structure instead of a 4-byte pointer.
- mandatory construction costs (which can be the same as for a char * when done correctly)

quote:

If anyone else replies to this topic, what exactly does the :: operator do other than to define member functions of a class?

It doesn''t define anything. It''s the scope resolution operator, which works on any name that has sub-names (namespaces, classes, structs, unions). A::B tells the compiler to find B as a subname of A. ::B tells the compiler to find B at global scope (i.e. all WIN32 names).

Share on other sites
Thank you for that explanation. Things in programming are easy once you have someone explain exactly what it does and how it does it.

But I still dont get the explanation of the string things...(dont worry about it)

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Share on other sites
Thanks Stoffel... It worked perfectly...

By the way...thanks for going into detail on it as well. It''s always good to know why you doing something.

• 11
• 20
• 12
• 10
• 37
• Forum Statistics

• Total Topics
631400
• Total Posts
2999860
×