5 Replies - 895 Views - Last Post: 28 March 2010 - 09:56 PM Rate Topic: -----

#1 jaia  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 61
  • Joined: 21-March 10

Concatenating Strings and Integers

Posted 28 March 2010 - 08:32 PM

I'm writing a program to output files and want the filenames to have the form "walki.txt", where i is an integer showing how many files have been written: walk1.txt, walk2.txt, etc. My code is:

int i = 1;
...
string filename;
filename = "walk" + i + ".txt";
fout.open(filename);

...



Compiling this code in GCC gives the following error message: "invalid operands of types ‘const char*’ and ‘const char [5]’ to binary ‘operator+’". I tried the int2string function described at http://www.dreaminco...snippet4394.htm but that just gives "error: ‘int2string’ was not declared in this scope" (and yes, I included sstring). What should I do?

Is This A Good Question/Topic? 0
  • +

Replies To: Concatenating Strings and Integers

#2 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Concatenating Strings and Integers

Posted 28 March 2010 - 08:38 PM

Stringstream.. I may have the working code around here somewhere, but make a stringstream variable, and use:

ss << filename << integerVariable << ".txt";

and then use:

string filename=ss.str();

This post has been edited by taylorc8: 28 March 2010 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Concatenating Strings and Integers

Posted 28 March 2010 - 08:44 PM

why not just make 'i' a char?
#include <iostream>
#include <string>

using namespace std;

int main ()
{

	string filename = "test";
	for(char i='1'; i<='9'; i++)
	{
		cout<<filename+i+".txt"<<endl;

	}

	cin.ignore();
	cin.get();
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Concatenating Strings and Integers

Posted 28 March 2010 - 08:48 PM

stringstream does what you're trying to do..

#include <iostream>
#include <string>
#include <sstream>
using namespace std;


int main()
{
	string path="C:/file";
	string final;

	//stringstream variable:
	stringstream ss;

	
	for( int i=0 ; i<12 ; i++ )
	{
		ss.str(""); // clears it..
		ss << path << i;
		final=ss.str();
		cout << final << endl;
	}

	return 0;
}

This post has been edited by taylorc8: 28 March 2010 - 08:49 PM

Was This Post Helpful? 2
  • +
  • -

#5 jaia  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 61
  • Joined: 21-March 10

Re: Concatenating Strings and Integers

Posted 28 March 2010 - 09:48 PM

Thanks, stringstream worked! I had to convert the resulting string to a C-style string for fout.open to accept it, so the final piece of code looks like this.

ofstream fout;
stringstream ss;
int i = 1;
string filename;
...


while (cin >> target) {
	ss.str("");
	ss << "walk" << i << ".txt";
	filename = ss.str();
        fout.open(filename.c_str());

        ...



Now on to catch the next bug! :-)

This post has been edited by jaia: 28 March 2010 - 09:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Concatenating Strings and Integers

Posted 28 March 2010 - 09:56 PM

You can also use strtoi or atoi.

You can also use strtoi() or atoi().
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1