-1

Possible Duplicate:
C++ concatenate string and int

Hi,

In C# I can write like this:

int i = 0;
string text = "out.jpg";
while(true)
{
     i++;
     Object.write(i+text, stream);
}

But this is not true for C++. the problem is at: i + default.

How could I fix this in C++?

Thanks in advance. Your help is much appreciated!

Community
  • 1
  • 1
olidev
  • 18,940
  • 49
  • 129
  • 194

4 Answers4

3

You could use a stringstream...

std::stringstream ss;
ss << i << text;
Object.write(ss.str(), stream);
cHao
  • 82,321
  • 20
  • 145
  • 171
  • +1. First I thought about `sprintf()`, but this is not really a C++ solution, rather a C one and it requires some usage of a `char` array. Using `stringstream` is probably the best solution. – rhino Oct 27 '10 at 13:56
1

default is a keyword in C++. You can't have string default in C++. And I don't see what you are trying to achieve. Please clarify

Armen Tsirunyan
  • 125,569
  • 56
  • 315
  • 427
1

take a look at stringstreams or boost.format http://www.boost.org/doc/libs/1_38_0/libs/format/doc/format.html

boost::format("%1%%2%") % i % default_;
DaVinci
  • 1,391
  • 1
  • 11
  • 27
0

default is a reserved keyword. Change the variable name to defaultStr or similar, and everything should work fine.

Olie
  • 24,319
  • 18
  • 96
  • 131