Displaying a score using TextOut()

Ok, I have taken a year of Computer Science at a University. However, we did not have time for graphics before we left for the summer. I am expiramenting on my own and have hit a question.

I can display text:

TextOut(hdc, 150, 750, TEXT("Player 1:"),9);

I have not been able to figure out how to display a variable such as:

1
2
int score = 0;
TextOut(hdc, 150, 750, score,1);


I have read some other posts and tutorials but I cannot seem to get a simple sample code. I am guessing it looks something like this but I cannot get it to compile or understand what it does...

1
2
3
char buf[ BUFFER_SIZE ];
TextOut( hdc, x, y, buf, wsprintf( buf, "The number is %d.", score ));
// compile error:  error C2664: 'TextOutW' : cannot convert parameter 4 //from 'char' to 'LPCWSTR' 


Any help would be greatly appreciated. Thanks.
Do you need to use TCHAR:

 
TCHAR buf[ BUFFER_SIZE ];


Ok, I have no idea what TCHAR is, but it seemed to get rid of those earlier compiler errors. However, I am still getting a compiler error:

1
2
3
4
5
6
7
TCHAR buff[5];
	
	 
	 TextOut(hdc, 150, 60, TEXT("Total Guesses:" ),16);
	 TextOut(hdc, 200, 60, buff, wsprintf(buff, "%d", score));

//'wsprintfW' : cannot convert parameter 2 from 'const char [3]' to 'LPCWSTR' 


Any ideas on that one? And is this code going to work as intended?
It seems to be asking for a TEXT() around it, like you have around the other string literal.

..., wsprintf(buff, TEXT("%d"), score)...
That fixed it and it does work as intended. However, I am curious as what a TCHAR is and why the array works but not a regular int.
I'm pretty sure a TCHAR is some sort of wide character type (since wsprintf() and TEXT() turn stuff into wchar I believe).
closed account (z05DSL3A)
TCHAR is a macro, if UNICODE is defined TCHAR is a WCHAR, else it is a char.

TEXT() is also a macro, it prepends an L if needed to the string literal passed to it, So TEXT("Total Guesses:" ) wil become either L"Total Guesses:" or "Total Guesses:" depending on if UNICODE is defined or not.

Most of the Windows API has a macro defined to decide what the required function is, e.g. MessageBox is replaced with either MessageBoxA or MessageBoxW, so using macros like TCHAR and TEXT(), will make sure the correct parameter types are used.

HTH
Topic archived. No new replies allowed.