#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    68
    Rep Power
    9

    Converting a float (or integer) to a character array


    Hi Guys,

    How do you convert a float (or an integer) to a character array? I've tried itoa, but it doesnt seem to work. It keeps on saying:
    E2193 Too few parameters in call to 'itoa(int,char *,int)'

    Im using it like this:
    Char * TempString;
    TempString = itoa(11);


    I've also seen some people using it like this:
    string TempString;
    TempString = itoa(11);


    But neither work for me (I'm using C++Builder 6).

    Thanks in advance!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    20
    Rep Power
    0
    Originally Posted by lozware
    Hi Guys,

    How do you convert a float (or an integer) to a character array? I've tried itoa, but it doesnt seem to work. It keeps on saying:
    E2193 Too few parameters in call to 'itoa(int,char *,int)'

    Im using it like this:
    Char * TempString;
    TempString = itoa(11);


    I've also seen some people using it like this:
    string TempString;
    TempString = itoa(11);


    But neither work for me (I'm using C++Builder 6).

    Thanks in advance!
    What you saw is atoi and not itoa. The first function takes only one parameter and the second (which is not standard) takes three. I think the first parameter is the int which you want to convert, the second param is the result of conversion and the third is the radix.
    EDIT: Read this
  4. #3
  5. No Profile Picture
    Closet coder
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2005
    Location
    Plantation, FL <---south florida
    Posts
    1,431
    Rep Power
    153
    check out the sticky at the top of the forums.
    if you want to make some float a string (char *) then something as simple as
    Code:
    float fSomeNumber = something;
    char[50] sMystring = { 0x00 };
    
    sprintf( sMystring , "%f", fSomeNumber );
    printf( "my float is = %s", sMystring );
    its a different story if you are using std::string...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    68
    Rep Power
    9
    Cheers guys! I'm going to have to get used to printf and sprintf - I've never used them before.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Stupid, stupid question, lozware: C or C++?

    The *printf() family of functions is the established standard for formatted output in C. Very concise, very versitile. I use it all the time. Indeed, when I migrated from Pascal and its requirement that you resort to multiple functions in order to construct a string out of multiple variables (a separate function call for each variable and for each string inbetween), I instantly fell in love with sprintf() and the ability to construct a string of any complexity with but a single function call. That alone more than justified migrating from Pascal to C. So when I encountered iostreams in C++, I didn't like it at all. I saw it as a huge step backwards, replacing a concise, flexible, powerfully expressive mechanism with a verbose, cumbersome one. Over 15 years later, I still don't care for iostreams and very rarely use it.

    So if you are using C++ and its new built-in string class, then you could instead use iostreams. Declare an object of stringstream and << your float and int values into it.

    Or, if you are using C++, then sprintf() would still be a viable option, though you would be restricted to working with a char array (AKA "C-style string") instead of a string.

    But if you're using C, then you are pretty much restricted to sprintf().
    Last edited by dwise1_aol; November 22nd, 2006 at 10:30 AM.
  10. #6
  11. Advanced Programmer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Malaysia
    Posts
    452
    Rep Power
    42
    you mean like this ?
    C++ Code:
     
    char buf[80];
    int mynum = 10;
    sprintf(buf,"%d",mynum);
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music."
    - Kristin Wilson, Nintendo, Inc., 1989.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    68
    Rep Power
    9
    So what do you recommend that I begin using? My course tutor seems to want us to use cin and cout (are these what you are referring to as iostreams?), and she also wants us to use character arrays rather than strings.

    To be perfectly honest, I'd rather take a recommendation from you because I don't think that she's a particularly teacher (no offense to her, she's a nice person). Do you think I should start using character arrays and sprintf instead? Or another combination?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2006
    Posts
    2,270
    Rep Power
    1737
    cin + cout = C++. I recommend you start with C++ std::string.

    You may not have a choice! If your teacher wants you to use character arrays, then possibly she expects you to do the C way of things. I highly doubt it's because she made an explicit curriculum choice to teach C before C++. Rather, if it's anything like some other C++ courses and books, she'll teach you the C way of things, with a bit of C++ here and there, and then perhaps mention the STL (which includes std::string) and templates and some advanced OO in the end. If this is a course, you are more or lessed forced to accept such a progression of topics.

    If it were up to me, I would have you learn the C++ way of doing things. C++ streams, stringstreams, etc. Then deal with more C-ish topics like C strings vs. character arrays, printf, sprintf, etc.

    If you are interested in being a well-informed and well-experienced C++ programmer, you should eventually learn C-ish way of doing things. However, initially when learning the C++ language, it makes sense you study the C++ part first! You shouldn't shelve topics like templates off to the very end as a special mention. They are an integral part of modern C++ and you should deal with them perhaps even before many C topics.

    Anyway, the C way would be character arrays, sprintf, printf, fprintf, and so on. If you're dealing with character arrays, and C-style strings (not C++), might as well get used to the C I/O.
    When you ask a question, be prepared to tell us: what have you tried? If you think you don't need to try anything, we will never be interested in helping you. If you agree with the link, and you refuse to answer that question, you are being a hypocrite.

    Need help with broken code? Your question should be like a good bug report: (1) It has the smallest number of steps to reproduce the problem you see (2) It tells us precisely what you expected to see and (3) It tells us what you saw and how it differed from what you expected. We need all three to help you.
    Want better answers? Tell us what you Googled for and what steps you took to answer your own question.
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by lozware
    So what do you recommend that I begin using? My course tutor seems to want us to use cin and cout (are these what you are referring to as iostreams?), and she also wants us to use character arrays rather than strings.
    Yes, cin and cout are part of iostreams. They had (to my knowledge) always a part of C++.

    The built-in string class only became a part of C++ with the 1998 standard. Before then C++ programmers still needed to use C-style strings and all the books written up to 1998 (and even some beyond that date) would have you new'ing char arrays and using the standard C library functions for string handling. In a number of cases, the programmer or author would create his own string class encapsulating the C-style stuff and offering an interface much like what we now see in the standard string class. A famous example is the CString class that Microsoft offered as part of the MFC in its Visual C++ line -- please note that CString has a Format() method that accepts the exact same format string and argument list as printf() and which encapsulates sprintf().

    So if you find that you are required to use C-style strings in C++, it could be because (among other reasons):

    1. Your textbook or tutorial materials predate 1998. Or the author's expertise predated 1998.

    2. The compiler you are required to use predates the 1998 standard. This would include the Borland (AKA "Turbo") compilers at least up to v4 and undoubtedly also 5 (I'm fairly sure that version 2 is the latest that is available for free download). It would also include Visual C++ v6.

    3. Your instructor's own expertise predates the 1998 standard. For example, I learned C++ in 1991 and worked very intensively in it for a few years prior to 1995. Since then I've mainly been doing embedded programming in C, though I also wrote some MFC applications in VC++6. I didn't even become aware of the 1998 standard until I joined this forum a couple/few years ago.

    4. For your own edification, your instructor wants you to learn C-style strings. This would make sense, since a helluva lot of books and tutorials (eg, one the Navy is still using) and source code still use C-style strings. And you are still likely to encounter compilers that do not support the new 1998 standard (a couple examples listed above). If I were to teach a course in C++, I would start out with C-style strings and C-style I/O (ie, printf) and then as soon as they understood printf I would introduce iostreams. And after they understood and became practiced enough in C-style strings I would introduce them to strings -- there are some important issues in constructors and destructors that are best illustrated with C-style strings, so the students would need to know them for that; refer to "deep copying" in Scott Myer's "Effective C++".

    Originally Posted by lozware
    To be perfectly honest, I'd rather take a recommendation from you because I don't think that she's a particularly teacher (no offense to her, she's a nice person). Do you think I should start using character arrays and sprintf instead? Or another combination?
    You guys all know me. Of course I would say that you should first get familiar with char arrays (AKA "C-style strings") and sprintf. The more you learn the better it is for you.

    Next step would be for you to use iostreams with C-style strings. After all, how do you think they did it pre-1998?

    C-style strings -- 2 options:
    1. use sprintf
    or
    2. use strstream. Create an object of class strstream. Use the << operator to stream a string into it, just as you would use cout.

    C++ built-in string class --
    use stringstream. Conceptually same as strstream, except it needs to be used for string type instead of strstream.

    Give strstream a try and tell us how it worked.


    PS
    Old-style C++ has you including header files, so to use strstream you would need to include its header file thus:
    #include <strstream.h>

    Now, the problem with this is that compilers with a history in DOS and Windows had to adapt to DOS' filename conventions, which was that file names could be no more than 8 characters long and extensions no more than 3 (AKA "8.3"). strstream.h is too long, so those compilers had to change the file's name to strstrea.h. And the more recent versions of those compilers had stayed with that truncated file name for backwards compatibility.

    So if you're on Windows/DOS and your compiler complains that it can't find strstream.h, then give strstrea.h a try. Or else take a look in the compiler's INCLUDE directory to see what it is called. Or, more simply, look up strstream in your help files and see what it tells you to include.
    Last edited by dwise1_aol; November 24th, 2006 at 01:57 PM.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    68
    Rep Power
    9
    Wow, I just used sprintf and its bloody amazing!

    instantly fell in love with sprintf() and the ability to construct a string of any complexity with but a single function call
    I know exactly what you mean!

    Thanks for all the feedback and making me aware of this functions... this makes string manipulation / conversion a lot easier! I personally prefer character arrays to string objects, so I think that I will continue to use the c# method for string manipulation.

IMN logo majestic logo threadwatch logo seochat tools logo