PDA

Click to See Complete Forum and Search --> : Variables in strings (part II)


Wynd
Apr 29th, 2001, 04:44 PM
Ok, I am trying to MessageBox an integer variable. I can convert it to a string using itoa(), but how can i put text into it? This is what i am doing now:


int szTickCount;
char buffer[33];

szTickCount = GetTickCount();
szTickCount = szTickCount/(1000*60); //minutes
itoa(szTickCount, buffer, 10);
MessageBox(NULL, buffer, "How long have you been on?", MB_OK | MB_ICONINFORMATION);


This works, but all it says is the number (ex: 43). How can I redo this so it says something like "Your computer has been on for 43 minutes"?

Vlatko
Apr 29th, 2001, 05:33 PM
Quote from another thread

How would I do something like this:


PHP:--------------------------------------------------------------------------------
int score;
char bufferScore[4];
itoa(score, bufferScore, 10);
MessageBox...--------------------------------------------------------------------------------

I want it to say "Your score is (whatever it is), good job!" or something like that...in vb its the &, what is it in C++


Answer:

#include <iostream.h>
#include <windows.h>

int main(int argc, char* argv[])
{
int score = 5555;
char bufferScore[5];
itoa(score, bufferScore, 10);
char allText[] = "Your score is ";
strcat(allText,bufferScore);
strcat(allText," good job");
MessageBox(NULL,allText,"ok",MB_OK);
return 0;
}


or use the string class (much easier)


#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string t1 = "text1";
string t2 = "text2";
string all = t1+t2;
MessageBox(NULL,all.c_str(),"Caption",MB_OK);
return 0;
}

highlife
Apr 30th, 2001, 08:30 AM
or you could do this:


#include <windows.h>
#include <stdio.h>

void main( void )
{
int szTickCount;
char buffer[33];

szTickCount = GetTickCount();
szTickCount = szTickCount/(1000*60); //minutes

sprintf( buffer,"Your computer has been on for %d minutes", szTickCount );
MessageBox(NULL, buffer,MB_OK | MB_ICONINFORMATION);
}

parksie
Apr 30th, 2001, 01:07 PM
MessageBox(NULL, buffer,MB_OK | MB_ICONINFORMATION);I think you missed a parameter there somewhere ;)MessageBox(NULL, "Hello", buffer, MB_OK | MB_ICONINFORMATION);Also, it's a better idea to use _snprintf as this prevents you overrunning a buffer:TCHAR pcBuffer[100];
_sntprintf(pcBuffer, 100, Your computer has been on for %d minutes", szTickCount);One thing about prefixes -- if you're going to use them, then the official prefix for int is i. sz is for NULL-terminated strings.

sail3005
May 2nd, 2001, 06:28 PM
Are there any other standard prefixes, for say string, char, bool, double etc?

parksie
May 3rd, 2001, 12:15 PM
I posted the official document somewhere, but I have a slightly adapted version of that, so this is my system (similar but different):

Prefix | Type
--------|---------------
p | Pointer
b | bool
ub | unsigned char [b for byte]
sb | signed char
us | unsigned short
ss | signed short
ul | unsigned long
sl | signed long
f | float
d | double
c | TCHAR [char for MBCS, wchar_t==unsigned short for Unicode]

So, for a pointer to a character, the total prefix is pc as used above.

sail3005
May 3rd, 2001, 06:30 PM
Cool, thanks for the info. I like using prefixes, and find them useful. One more question, is there an offical prefix for string, from the string class? perhaps, str?

Thanks again!

parksie
May 3rd, 2001, 06:31 PM
I use s, like I do for VB :)