anna Sjolvikaz
anna Sjolvikaz

Reputation: 141

Format text in c++ way

I'm trying to make a c++ function that can format my text output like this:

#0 id: 80

#1 id: 80

#2 id: 80

#3 id: 80

etc...

and have a parameter called max in the function to limit the amount of output, like:

if the max parameter was set to 10, it most print/output only 10 times:

#0 id: 80

#1 id: 80

#2 id: 80

#3 id: 80

#4 id: 80

#5 id: 80

#6 id: 80

#7 id: 80

#8 id: 80

#9 id: 80

#10 id: 80

What I have tried to do is this code down below, but it doesn't work as I wanted:

void format_text(int max){

char Buffer[100];

static int amount;

for (int x = 0; x <= max; x++){

amount ++;

if (max > amount){

length += sprintf(Buffer+length,"#%d id: %d\n", amount, 80);

printf("%s", Buffer);

}

Please help me in making a function as I have described for you

Upvotes: 1

Views: 509

Answers (1)

jxh
jxh

Reputation: 70502

Perhaps what you are after is an ostringstream:

std::string Buffer;
std::ostringstream oss;

//...
    oss << "#" << amount << " id: " << 80 << "\n";
//...

Buffer = oss.str();

Upvotes: 5

Related Questions