0

Hello everyone,
I don't know if I'm confused with itoa,excuse me for this if so, but what I'm trying to do is:
I have an array of characters, say

char example[7]

I assigned the first 4 characters by

strcpy(example,"Fill");

Then I'm trying to fill the next char with an integer using itoa:

example[4]=itoa(i,example[4],10);

where i is an incremental integer variable in a for loop.
the rest of the char array is constant.
example[5]='x';
example[6]='\0';
Thank you for your replies.
Cheers,

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by ssmg
Featured Replies
  • 1
    jonsca 1,059   6 Years Ago

    It's relatively easy: [code] #include <sstream> //... int numbervariable = 10; //or double, etc. or use your loop counter std::stringstream ss; ss<<"Firstpart"<<numbervariable<<"Secondpart"; //use it just like any other stream std::cout<<ss.str()<<" is the string portion"; [/code] Read More

0

This is an ideal situation which which to use stringstreams. Are you locked into using the characters array?

Also, itoa is non-standard so it's not guaranteed to be available on all compilers.

0

Hello jonsca,
Well, I could switch to a string,doesn't matter at all,if only I knew how to use stringstreams :) :(

1

It's relatively easy:

#include <sstream>
//...
int numbervariable = 10; //or double, etc. or use your loop counter
std::stringstream ss;
ss<<"Firstpart"<<numbervariable<<"Secondpart"; //use it just like any other stream

std::cout<<ss.str()<<" is the string portion";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.