Thread: appending a number to string?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    39

    appending a number to string?

    How do I append a number to the end of a string (and get a string back)? For example, I want to run a for() loop similar to: for(i = 0; i < num; i++), and for each pass through the loop I want to append i to a predefined string (ex: "number", so what I want to get from each run through the loop is a string like "number1", "number2", etc). thanks!

  2. #2

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    . . . except that you can do the concatenation on the stringstream, rather than after converting the number to a std::string.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Also, you could use lexical_cast, from boost. It can do quite the same thing as using stringstream, but it's "easier" to use most of the time (well it's not that using stringstream is hard to use, but lexical_cast is just really simple).
    I hate real numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM