0

I'm currently working on a Windows based C++ program. I am used to working/coding console applications so I am not familiar with the syntax for a lot of what I'm trying to do. So I might have a lot more questions later on.

For now, I want to print some text and have a variable displayed within the text. For example, if I wanted to print "I am X years old" and X = 30, how would the syntax of that be formed?

I know this works:

DrawText(hDC,L"I am X years old",-1,&rect,DT_CENTER | DT_WORDBREAK );

What I want to do is:

DrawText(hDC,L"I am "+ X +" years old",-1,&rect,DT_CENTER | DT_WORDBREAK );

But this doesn't work. So how do I correctly implement that?

Musa
  • 96,336
  • 17
  • 118
  • 137
Avalon-96
  • 135
  • 3
  • 8

3 Answers3

2

C++ is not a typeless language like Javascript, etc, and so you can't construct a string like that by adding the parts together. Instead you have to build the string yourself using a function like swprintf_s. For example,

wchar_t wchBuf[128];
swprintf_s(wchBuf, _countof(wchBuf), L"I am %ld years old", X);
DrawText(hDC, wchBuf, -1, &rect, DT_CENTER | DT_WORDBREAK);
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • Don't pass the size, use the template version that detects it. – Ben Voigt Jul 29 '12 at 02:45
  • I apologize but this is the first time I'm programming in windows like this. Can you explain what the swprint_s is doing? What exactly are you doing there, making a string? – Avalon-96 Jul 29 '12 at 02:50
  • @Avalon-96-since he's not responding..`sprintf_s`, in C, forms a string like C++ `strcat`, but IMO, is better because as you can see he provides the format of how it is to turn out. So, I'm assuming that they are equivalent but in different programming realms. – ChiefTwoPencils Jul 29 '12 at 03:05
  • @Avalon-96: C/C++ does not natively support "strings" as a distinct datatype (although in C++ there is the STL string/wstring class which does a decent job as shown in the other answer below). A string is really just an array of characters. The swprintf_s function fills a provided buffer with a formatted array of arguments that you pass to it. The %ld code is a marker for the swprintf_s function that means "the next argument I pass you is an integer". See http://www.cprogramming.com/tutorial/lesson9.html and http://msdn.microsoft.com/en-us/library/ce3zzk1k(v=vs.80).aspx for some more info. – Jonathan Potter Jul 29 '12 at 03:29
0

String literals are just pointers, you can't add them together.

But std::string objects support operator+.

DrawTextA(hDC,("I am " + itostr(X) + " years old").c_str(),-1,&rect,DT_CENTER | DT_WORDBREAK );

(You can find itostr here)

Of course, it's possible to do this with wide characters and std::wstring, but for this example sticking to ASCII is simplest.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Wouldn't you be better of using "int" instead of "string" seeing how strings are manly used to store text? For e.g. You could use: int age = 30;

zTBxN
  • 47
  • 2
  • 9