473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

simple question on type casting~

hi all~

i'm newbie to C++ string type and get confused when trying to convert
an int into string, i just want my cute messagebox to popup mouse
positions, here is my code:
Expand|Select|Wrap|Line Numbers
  1. MessageBox(NULL, m.lX, "Hey", MB_OK);
  2. as u guess m.lX is of type of int, any help ?
  3.  
Jan 7 '06 #1
2 1708
black(flashing vampire) wrote:
hi all~

i'm newbie to C++ string type and get confused when trying to convert
an int into string, i just want my cute messagebox to popup mouse
positions, here is my code:
Expand|Select|Wrap|Line Numbers
  1.  MessageBox(NULL, m.lX, "Hey", MB_OK);
  2.  as u guess m.lX is of type of int, any help ?
  3.  


See the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-39.1

Rennie deGraaf
Jan 7 '06 #2
"black(flashing vampire)" <qu********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
hi all~

i'm newbie to C++ string type and get confused when trying to convert
an int into string, i just want my cute messagebox to popup mouse
positions, here is my code:
Expand|Select|Wrap|Line Numbers
  1.  MessageBox(NULL, m.lX, "Hey", MB_OK);
  2.  as u guess m.lX is of type of int, any help ?
  3.  


The way I do it is by using string streams to convert them to std::strings.

#include <string>
#include <sstream>
template<typename T, typename F > T StrmConvert( F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}

The use of this is fairly simple. You just need to specify what you want to
convert to. The compiler figures out (somehow) what you're converting from.

std::string MyString = StrmConvert<std::string>( MyInt );

So in your specific case it would be:

MessageBox( NULL, (StrmConvert<std::string>( m.lX )).c_str(), "Hey",
MB_OK );

or if you want to see what's going on:

std::string TempStr = StrmConvert<std::string>( m.lX );
MessageBox( NULL, TempStr.c_str(), "Hey", MB_OK );

the c_str() converts a std::string into a constant c style string. So you
could copy it to a c-style string even if you wanted (but not advised).

char TempStr[100];
strcpy( TempStr, (StrmConvert<std::string>( m.lX )).c_str() );
MessageBox( NULL, TempStr, "Hey", MB_OK );

But it's best to use std::strings when you can and not convert them to
c-style strings. First off, are you positive that the number will fit in
100 bytes?
Jan 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new...
6
by: Jim Bancroft | last post by:
Hi everyone, I'm having some trouble with the code below. I receive a compile-time error on the second line saying "; expected": private static void myTestFunction(long myLong) { ...
3
by: Dariusz Tomon | last post by:
Hi I'm trying to retrieve data from database (MS Sql serv) from field of BIT type. I'm trying like this: while (myReader.Read()) { Int16 cos = (Int16)myReader.GetSqlInt16(1);
6
by: Justin | last post by:
I am trying to add dollar amounts together and add sales tax but everthing after the decimal point is being cut off in the dollar amounts. Here is my code: if (Adults != "") { AdultTotal =...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
6
by: crook | last post by:
I have code below and it works properly but when I'm compiling it with "--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids casting nonscalar to the same type". How can I change this...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
5
by: aaragon | last post by:
Hi everyone, I wrote a very simple function to try to understand the casting of variables in C++. The function is function foo() { std::vector<inttest(100); randomize(test); unsigned long...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.