-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
  • 20,058
  • 51
  • 133
  • 197

4 Answers4

3

You could use a stringstream...

std::stringstream ss;
ss << i << text;
Object.write(ss.str(), stream);
cHao
  • 84,970
  • 20
  • 145
  • 172
  • +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
  • 130,161
  • 59
  • 324
  • 434
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
  • 2
  • 12
  • 27
0

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

Olie
  • 24,597
  • 18
  • 99
  • 131