String with changing number?

Apr 28, 2008 at 11:35pm
Hi, I'm having trouble writing a code that will make a list of strings that have an increasing number at the end of it (ex.The output would be example_1, example_2, example_3 etc.) Can anyone help?
Thanks
Apr 28, 2008 at 11:49pm
1
2
3
4
5
6
7
8
9
  string Base = "example_";
  string Form;

  for( int i = 1; i < MAX; ++i )
  {
    Form = Base;
    Form += i;
    cout<<Form.c_str()<<endl;
  }
Apr 28, 2008 at 11:54pm
Thanks, this works for the most part.
The number at the end (i) is in ASCII though. How do I make it a number so that I can count past single digits?
Apr 29, 2008 at 12:26am
Hm.. I must be rusty for some reason I thought it would convert. Silly me.

Using itoa

1
2
3
4
5
6
7
8
9
10
11
  char Buff[5];
  string Base = "example_";
  string Form;

  for( int i = 1; i < MAX; ++i )
  {
    Form = Base;
    itoa(i,Buff,10);  
    Form += Buff;
    cout<<Form.c_str()<<endl;
  }

Apr 29, 2008 at 12:31am
Thanks, this worked perfectly.
If you don't mind me asking, what does itoa do?
Apr 29, 2008 at 12:52am
itoa

i = int
to = to
a = alphabet

Takes an int and changes it to the correct ascii symbols.

There are other ones like atoi, atof, ftoa, etc.
Topic archived. No new replies allowed.