printing some stuff on screen?
hello guys i am having some problems writing some stuff on screen using a function that helps me draw text on the screen. The problem occurs when i want to write a string to the screen, for example the players name.
//in main game loop
strcpy_s(str, "");
wsprintfA(str, "\nPlayer Name: %s", hscore[0].name);
//Draw string
DXUDrawText(&impactFont, str); //<-- function that draws test on screen
//somewhere
hScore[0].name="RYU";
hscore[0].score=1233412;
Now the problem is this wsprintfA(str, "\nPlayer Name: %s", hscore[0].name);
Doesn't print ryu name, i am getting confused how to use it, if i did
wsprintfA(str, "\nPlayer Score: %d", hscore[0].score); this works showing the integer value for player score?
Please post the definitions of the str variable and the hscore variable (and the structure/class).
Also, I note that you have some inconsistent case - you access "hscore" most of the time, but assign the name string into "hScore". Is that just a typo in your post, or is that actually what's in your code?
You shouldn't use strcpy_s, wsprintfA, and so on; they can possibly write into areas of memory that they shouldn't, which can crash your program, corrupt your data, or worse. (Look up "buffer overflow" for more information on how that works.) Instead, use strncpy, snprintf, and so on; those functions accept a "length" parameter which tells them how much memory they can write into, which helps prevent overflows.
If you're writing in C++, you shouldn't use raw string functions at all, but instead use std::string.
Also, I note that you have some inconsistent case - you access "hscore" most of the time, but assign the name string into "hScore". Is that just a typo in your post, or is that actually what's in your code?
You shouldn't use strcpy_s, wsprintfA, and so on; they can possibly write into areas of memory that they shouldn't, which can crash your program, corrupt your data, or worse. (Look up "buffer overflow" for more information on how that works.) Instead, use strncpy, snprintf, and so on; those functions accept a "length" parameter which tells them how much memory they can write into, which helps prevent overflows.
If you're writing in C++, you shouldn't use raw string functions at all, but instead use std::string.
struct HiScoreTable{ string name; int score;};HiScoreTable hscore[10];//will be in vector once it starts working//vector<hiScoreTable> scoreTable;DXUFONT impactFont; // the font we will use to write to screenTCHAR str[MAX_PATH]; // a string variable to write to screenRECT rcImpact = {20, 20, 1024, 768}; // the rectangular area we will write on screen
basically hscore is just a struct which i will input data from the player later on i.e
//when player dies
hscore[0].name=player->getName();
hscore[0].score=player->getScore();
//when player dies
hscore[0].name=player->getName();
hscore[0].score=player->getScore();
hi i did this ->
strcpy_s(str, "");
hscore[0].name="AMIR";
sprintf(str, "\nPlayer Name: %s", hscore[0].name);
but the output is Player Name: (null) ???? whats going on here?
strcpy_s(str, "");
hscore[0].name="AMIR";
sprintf(str, "\nPlayer Name: %s", hscore[0].name);
but the output is Player Name: (null) ???? whats going on here?
The problem is that you are mixing std::string and C-style strings. wsprintfA expects that the string you pass it is a C-style string, but you're passing it a std::string from the hscore struct.
You could fix the problem by converting the std::string to a C-string with the c_str() function; that is, pass hscore[0].name.c_str() to wsprintfA. However, that's not really a good fix.
What you should do is use std::string consistently, and not use C-strings at all if you can possibly help it.
You could fix the problem by converting the std::string to a C-string with the c_str() function; that is, pass hscore[0].name.c_str() to wsprintfA. However, that's not really a good fix.
What you should do is use std::string consistently, and not use C-strings at all if you can possibly help it.
// Include this at the top of the file:#include <sstream>// Replace the string formatting/output code with this:std::ostringstream output;output << "\nPlayer Name: " << hscore[0].name;DXUDrawText(&impactFont, output.str());
Yes, that is technically a valid option. It is, however, an extremely bad idea. As I said in my first post, C-strings are dangerous and error-prone, and can lead to many bad things happening in your program. Even programmers with a lot of experience can screw up C-strings pretty easily.
For more detailed explanation, see section 34.1, here.
I would really strongly recommend that you get out of the habit of using C-strings (this means, in case you're not familiar with the term, anything like char*, char[], TCHAR*, LPTSTR, etc. etc.) and use C++'s string feature properly.
For more detailed explanation, see section 34.1, here.
I would really strongly recommend that you get out of the habit of using C-strings (this means, in case you're not familiar with the term, anything like char*, char[], TCHAR*, LPTSTR, etc. etc.) and use C++'s string feature properly.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement