append val of int to a string

This is a discussion on append val of int to a string within the C++ Programming forums, part of the General Programming Boards category; well the subject tells the story.. should be simple to do.. but my brain is drawing a blank i have ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    append val of int to a string

    well the subject tells the story..

    should be simple to do.. but my brain is drawing a blank

    i have ...

    int i;
    string ret="A";

    ...
    ...
    ...

    i need to append the value of i to the ret string...

    the int is just a counter in a loop, the value of the string needs to end up being somthing like.. A1 or A2 ..

    danka

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Code:
    string ret("A");
    
         for(int i=50; i<=57; i++) // 50 is 1, 57 is 9
         {
         ret[1] = i;
         }
    Don't forget to include <string>!

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    3
    thanks for the reply.. but i got it.. i wasnt thinking!!!

    for some reason i didnt think sprintf would print 2 vars to a string... so i fixed it like..

    ret is now a char * ..

    sprintf(ret, "%c%i", theAlphabet, i);

    its alive!!!!!! hahahahahaha....

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    You might try:

    int i = 1;
    string ret="A";
    char temp[100];


    do{

    sprintf( temp, "%i", i++ );

    ret += temp;

    }while(/*condition*/);


    Actually, though, this would result in:

    "A1", "A12", then"A123", etc...


    Well you could do:

    int i = 1;
    string prep = "A";
    char temp[100];


    do{

    string ret = prep;

    sprintf( temp, "%i", i++);

    ret += temp;


    }while(/*condition*/);
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

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, 07:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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