Int into a string?

This is a discussion on Int into a string? within the C++ Programming forums, part of the General Programming Boards category; simply put, where can I find out how to put an int into a string? i.e. int x; string str; ...

  1. #1
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    Int into a string?

    simply put, where can I find out how to put an int into a string? i.e.

    int x;
    string str;

    str += int; //?? i dont think it works

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    itoa()
    Blue

  3. #3
    Unregistered
    Guest
    itoa turns your in into a char*, from there you can build a string object if you so wish

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
     /** Converts any type T to string if it has operator << (std::ostream&, const T&) defined */
     template <class T> std::string toString(const T& val) {
      std::ostringstream strWriter;
      strWriter << val;
      return strWriter.str();
     }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    drat...

    i hoped i wouldnt have to ask a dumb question but.. where is itoa defined? which header file? i couldnt find it...

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    itoa() is in conio.h, a non-standard header file for some non-standard functions like itoa(). Some compilers, like Borland have it, and others like VC++ don't.

    templates and ostringstreams are standard as is sprintf(), which could also be used to convert int to string.

  7. #7
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    okay, thanx..

    ony problem with templates is I currently know nothing at all about them , because I am working on Classes and strings at the moment. also I am using Linux, with gcc, so I guess i will have to go see if conipo and atoi works here, otherwise could anyone point me to some tutorials on using any of those other alternatives? It'd be greatly appreciated.
    Thanx in advance.

  8. #8
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Thanx lots guys I tried atoi but discovered either gcc don't have a conio or else i did it wrong, so i looked up sprintf() in my "C++ the complete reference (Herb Schildt)", and used it.. and it works great

    thanx again,
    may the source be with you
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  9. #9
    Unregistered
    Guest
    itoa(..) is also in stdlib.h for future reference. And I have conio.h in my VC++ anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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