post 555 on type casting~

This is a discussion on post 555 on type casting~ within the C++ Programming forums, part of the General Programming Boards category; hi all~ i'm newbie to C++ string type and get confused when trying to convert an int into string, i ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    post 555 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:
    Code:
    MessageBox(NULL, m.lX, "Hey", MB_OK);
    as u guess m.lX is of type of int, any help ?
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    sprintf or stringstream:

    Code:
    char msg[64];
    
    sprintf(msg,"%d", m.lX);
    
    // Or
    // stringstreams, don't forget the include files needed:
    // # include <sstream> 
    // using namespace std
    
    stringstream ss;
    
    string str;
    ss << m.lX;
    ss >> str;
    
    // output as c string using c_str member function:
    cout << str.c_str() << endl;
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    wor too many steps for debugging. are there any functions can be used ?
    Never end on learning~

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Any functions that can be used? (WTF???) Sure, try sprintf. Or you can try operator>> and operator<< for stringstreams. Either one will work. See above.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    operators like >> dont help caz i'm developing a simple window program. the string may be used in functions like MessageBox so that is why i'm seeking for a simple function which can convert int into a string. any idea ?
    Never end on learning~

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Write a simple function using stringstream:

    Code:
    # include <sstream>
    using namespace std;
    
    string NumToString(double val)
    {
        stringstream ss;
        string str;
    
        ss << val;
        ss >> str;
    
        return str;
    }
    
    int main()
    {
        MessageBox(NULL, NumToString(m.lX).c_str(), "Hey", MB_OK);
        return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I'm in the mood for some easy coding, so here's the function(s) you're looking for:
    Code:
    #include <iostream>
    
    /*
     * itoa is the main function that's used in main
     * it depends on esrever (reverse spelled backwards) to reverse the characters
     * in the string it produces.
     * getlen is just used to get the amount of places in a short integer
     */
    void itoa(const short int in,const short int len,char*buf);
    short int getlen(const short int in);
    void esrever(char*in,const short int len);
    
    int main()
    {
    	/*
    	 * a is the integer we're going to be turning into a string
    	 * and word is going to be the string we put it into.  NOTE: this is not
    	 * a c-style string because there is no null-char on the end of the
    	 * string, nor is there space for one.
    	 */
    	short int a=12345;
    	char*word=new char[getlen(a)];
    
    	/*
    	 * here's the entire reason for this program: the itoa function
    	 */
    	itoa(a,getlen(a),word);
    
    	/*
    	 * and now we just output the results, free up the allocated memory, and
    	 * exit the program
    	 */
    	std::cout<<word<<std::endl;
    	delete[]word;
    	return 0;
    }
    
    /*
     * this function returns the amount of places there are in a short int
     */
    short int getlen(const short int in)
    {
    	/*
    	 * the method is simple: it loops through trying bigger modulo (in
    	 * increments of powers of 10) until it gets to a point where the
    	 * result is zero.  Ever iteration of the loop increases a counter by
    	 * one, and when the loop exits, you're left with the amount of digitsf
    	 * in the number
    	 */
    	short int retval=0;
    	while(in%(10^retval)>0)
    	{
    		retval++;
    	}
    	return retval;
    }
    
    /*
     * this just reverses a character array.  Because we don't null-terminate our
     * array, you have to pass in the size
     */
    void esrever(char*in,const short int len)
    {
    	char tmp;
    	register short int l;
    	register short int r;
    
    	/*
    	 * it works by using a simple swap function, starting at both ends of
    	 * the array and working it's way to the center
    	 */
    	for(l=0,r=len-1;l<r;l++,r--)
    	{
    		tmp=in[r];
    		in[r]=in[l];
    		in[l]=tmp;
    	}
    }
    
    /*
     * this is the main focus of the program: the function that turns an integer
     * into an array of characters
     */
    void itoa(const short int in,const short int len,char*buf)
    {
    	/*
    	 * it's quite simple, actually, after you get the math part down.  What
    	 * it does is takes the number, divides it by a power of 10 to get the
    	 * number you want in the ones place.  Then, to get rid of any other
    	 * numbers in the tens, hundreds, etc. places, you take the original
    	 * number again and divide by one higher power of 10 and multipy by 10.
    	 * now you have a zero in the ones place and the rest of the number in
    	 * their places relative to the number you want.  a simple subtraction
    	 * takes care of those, and you now have a single number in the ones
    	 * place.  These divisions take advantage of the casting to integers
    	 * that truncate the decimals.  After you have that single digit, you
    	 * simply add the integer equivalent of '0' and cast it back to a
    	 * character before saving it in the array.
    	 */
    	for(register short int i=len-1;i>=0;i--)
    	{
    		buf[i]=static_cast<char>(((in/static_cast<int>(pow(10,i)))-((in/static_cast<int>(pow(10,(i+1))))*10))+static_cast<int>('0'));
    	}
    
    	/*
    	 * that method returns a reversed string, so we now use our eserver
    	 * method to reverse the string.
    	 */
    	esrever(buf,len);
    }
    now would you rather use stringstream?
    Last edited by major_small; 01-07-2006 at 03:02 AM. Reason: comments
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I stand corrected. Use his code. Good call Major_small.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> now would you rather use stringstream?
    Yes, because I know that has been used and tested by many programmers. Does your code have any errors? This is the output I got when running your program:
    Code:
    ≡║║
    It's generally better to use existing functionality than it is to use code you wrote yourself that does the same thing. The existing functionality is far less likely to have bugs.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Daved
    Does your code have any errors? This is the output I got when running your program:
    Code:
    ≡║║
    erm, I don't see why you're getting that...
    Quote Originally Posted by Daved
    It's generally better to use existing functionality than it is to use code you wrote yourself that does the same thing. The existing functionality is far less likely to have bugs.
    yeah, because if we stray from what other people tell is good, then we might actually learn something, like oh, say... the world not actually being flat?

    anyways, you guys aren't getting what I was saying... I must have missed the <sarcasm /> tags around it...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    anyways, you guys aren't getting what I was saying... I must have missed the <sarcasm /> tags around it...
    I understood. I was giving you a thumbs up.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How about short AND tested AND Unicode-correct?
    http://www.boost.org/libs/conversion/lexical_cast.htm

    Code:
    using boost::lexical_cast;
    typedef std::basic_string<TCHAR> tstring;
    
    MessageBox(wnd, lexical_cast<tstring>(l.iX).c_str(), TEXT("Hey!"), MB_OK);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21