simple string management

Discussion in 'C++' started by gurumare@gmail.com, Jul 11, 2005.

  1. Guest

    hello, i would like to increment a string in a for loop. The desired
    output would look like:

    team0
    team1
    team2

    and so on. I use the code

    for (int i = 0; i < numTeams; i++) {
    sName = "team"+i;
    a.SetName(sName);
    }

    and get the output:

    team
    eam
    am

    and so on. What can i do to get the desired effect in the most simple
    way possible? thank you
     
    , Jul 11, 2005
    #1
    1. Advertising

  2. Lionel Guest

    wrote:
    > hello, i would like to increment a string in a for loop. The desired
    > output would look like:
    >
    > team0
    > team1
    > team2
    >
    > and so on. I use the code
    >
    > for (int i = 0; i < numTeams; i++) {
    > sName = "team"+i;
    > a.SetName(sName);
    > }
    >
    > and get the output:
    >
    > team
    > eam
    > am


    Amusing. What I believe you are doing is getting a memory address from
    "team" and then you are adding a unit of i byte(s) to that memory
    address with the +i. In C++ you can't use the + operator for string
    manipulation. From the best of my knowledge you will need to convert the
    integer i to an ascii then append it to the string using something like
    strcat which can be found at:

    http://www.cplusplus.com/ref/cstring/

    Of course if you use google you will find all this information very easily.
     
    Lionel, Jul 11, 2005
    #2
    1. Advertising

  3. benben Guest

    > and so on. What can i do to get the desired effect in the most simple
    > way possible? thank you


    Use string stream.

    ben
     
    benben, Jul 11, 2005
    #3
  4. Bart Guest

    wrote:
    > hello, i would like to increment a string in a for loop. The desired
    > output would look like:
    >
    > team0
    > team1
    > team2
    >
    > and so on. I use the code
    >
    > for (int i = 0; i < numTeams; i++) {
    > sName = "team"+i;
    > a.SetName(sName);
    > }
    >
    > and get the output:
    >
    > team
    > eam
    > am
    >
    > and so on. What can i do to get the desired effect in the most simple
    > way possible? thank you


    The simplest possible way, assuming sName is a std::string, is:

    sName = "team";
    sName += "0123";

    But this has limited use and of course you have to be careful not to
    overrun the string literal. The best solution is to use
    std::stringstream as was already suggested.
     
    Bart, Jul 11, 2005
    #4
  5. Ben Pope Guest

    wrote:
    > hello, i would like to increment a string


    I think you should ask yourself what you actually mean here.

    > in a for loop. The desired
    > output would look like:
    >
    > team0
    > team1
    > team2
    >
    > and so on.


    OK, so do you want to:

    a) Increment the ASCII code of the last character of the string, starting at '0'.

    b) Increment the integer value of the last bit of the string, assuming it to be of base x, and starting at 0.

    c) Append to a string, a string representation of a number, which is of base x, starting at 0, which you are able to increment, in a loop.

    I would guess that you mean option c, in which case the implementation should be a bit more obvious, when expressed in that way.

    Also, do you have any bounds on these numbers? What happens at these bounds?

    Ben
    --
    I'm not just a number. To many, I'm known as a String...
     
    Ben Pope, Jul 11, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cG11ZA==?=

    Session state management simple problem

    =?Utf-8?B?cG11ZA==?=, Jan 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    341
    =?Utf-8?B?cG11ZA==?=
    Jan 24, 2005
  2. Floris van Haaster

    Project management / bug management

    Floris van Haaster, Sep 23, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,260
    Jon Paal
    Sep 23, 2005
  3. pouet
    Replies:
    2
    Views:
    793
    Will Hartung
    Jul 30, 2004
  4. ankur
    Replies:
    1
    Views:
    12,982
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  5. Jeff Uchtman

    Simple Banner Management

    Jeff Uchtman, Oct 7, 2003, in forum: ASP General
    Replies:
    3
    Views:
    146
    James Monroe
    Oct 8, 2003
Loading...

Share This Page