|
-
Apr 29th, 2001, 04:44 PM
#1
Thread Starter
Fanatic Member
Variables in strings (part II)
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:
Code:
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"?
Alcohol & calculus don't mix.
Never drink & derive.
-
Apr 29th, 2001, 05:33 PM
#2
Frenzied Member
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:
Code:
#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)
Code:
#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;
}
-
Apr 30th, 2001, 08:30 AM
#3
Lively Member
or you could do this:
PHP Code:
#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);
}
-
Apr 30th, 2001, 01:07 PM
#4
Monday Morning Lunatic
Code:
MessageBox(NULL, buffer,MB_OK | MB_ICONINFORMATION);
I think you missed a parameter there somewhere 
Code:
MessageBox(NULL, "Hello", buffer, MB_OK | MB_ICONINFORMATION);
Also, it's a better idea to use _snprintf as this prevents you overrunning a buffer:
Code:
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 2nd, 2001, 06:28 PM
#5
PowerPoster
Are there any other standard prefixes, for say string, char, bool, double etc?
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
May 3rd, 2001, 12:15 PM
#6
Monday Morning Lunatic
I posted the official document somewhere, but I have a slightly adapted version of that, so this is my system (similar but different):
Code:
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 3rd, 2001, 06:30 PM
#7
PowerPoster
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!
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
May 3rd, 2001, 06:31 PM
#8
Monday Morning Lunatic
I use s, like I do for VB
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|