Hi everyone,

Somewhere in my need i need to create strings like ( x,y,z ) with 0<x,y,z<N.
I was trying to use 3 for loops but it didn't work.
That's what i wrote:

 for(int i=0;i<N;i++){
   for(int j=0;j<N;j++){
      for(int k=0;k<N;k++){
    string t="( ";

        t+=i;
        t+=",";
    t+=j;
        t+=",";
    t+=k;
t+=" )";
}
}
}

I know the reason is that i,j,k are integers and it's not possible to add integers to a string.
So i tried to convert integers into char and then add them to the string.

by writing:

for(i=0;i<N;i++){
   for(int j=0;j<N;j++){
      for(int k=0;k<N;k++){
        string t="( ";
        char* newi,newj,newk;
        newi=itoa (i);
        newk=itoa (k);
        newj=itoa (j);
    t+=newi;
        t+=",";
        t+=newj;
    t+=",";
        t+=newk;
        t+=" )";

}
}
}

But that gives me this error:
error: 'itoa' was not declared in this scope

Can anybody tell me how i can do that?

Code Tags. Please.

Do you mean like this?

for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
for(int k=0;k<N;k++){
string t="( ";

t+=i;
t+=",";
t+=j;
t+=",";
t+=k;
t+=" )";
}
}
}

I know the reason is that i,j,k are integers and it's not possible to add integers to a string.
So i tried to convert integers into char and then add them to the string.

by writing:

for(i=0;i<N;i++){
for(int j=0;j<N;j++){
for(int k=0;k<N;k++){
string t="( ";
char* newi,newj,newk;
newi=itoa (i);
newk=itoa (k);
newj=itoa (j);
t+=newi;
t+=",";
t+=newj;
t+=",";
t+=newk;
t+=" )";

}
}
}

But that gives me this error:
error: 'itoa' was not declared in this scope

Can anybody tell me how i can do that?

Try this.

char temp = new char[10];
string test;
test=itoa(5,temp,10); //here 10 is radix
//when you're done delete it.
delete temp;

Or use stringstream:

stringstream out;
out << 3;
string test=out.str();

error: 'itoa' was not declared in this scope

to use itoa ensure you include cstdlib (see below)

#include <cstdlib>

this should solve the problem allowing you to += the chars into the string

Thanks for your help.
to nbaztec i tried to use the first way you suggested me by writting:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <cstring>


//using std::cout;
//using std::string;

using  namespace std;  

int main()
{
 for(int i=0;i<5;i++){
  for(int j=0;j<5;j++){
    for(int k=0;k<5;k++){
      string t="( ";
      char newi= new char[10];
      char newj= new char[10];
      char newk= new char[10];

      t+=itoa (i,newi,10);
      t+=",";
      t+=itoa (i,newj,10);
      t+=",";
      t+=itoa (i,newk,10);
      t+=" )";
      delete newi;
      delete newj;
      delete newk;  
      cout<<t<<endl;
    }
   }
  }
}

But i got this errors:

checkpositionmaker.cpp:20: error: invalid conversion from 'char*' to 'char'
checkpositionmaker.cpp:21: error: invalid conversion from 'char*' to 'char'
checkpositionmaker.cpp:22: error: invalid conversion from 'char*' to 'char'
checkpositionmaker.cpp:24: error: 'itoa' was not declared in this scope
checkpositionmaker.cpp:30: error: type 'char' argument given to 'delete', expected pointer
checkpositionmaker.cpp:31: error: type 'char' argument given to 'delete', expected pointer
checkpositionmaker.cpp:32: error: type 'char' argument given to 'delete', expected pointer

Can you see why that happend?

To Kanoisa: Thanks for your help but I'd already #include <cstdlib> in my code.

Rather than use itoa which is non-standard try nbaztec's stringstream method:
(portions omitted)

#include <sstream>
//...
std::stringstream ss;
for
{
  for
  {
    for
    {
       ss.str("");  
       ss<<i<<","<<j<<","<<k;
       std::cout<<ss.str()<<std::endl;
     }
   }
}

Oh! Gracias. I'm terribly sorry, I didn't notice. I ommited the * from my code.

char [B]*[/B]temp = new char[10];
string test;
test=itoa(5,temp,10); //here 10 is radix
//when you're done delete it.
delete temp;

Though it is nice to know what other options you have, the best would be to stick with sstream.

Also, you're allocating & deallocating memory inside a loop. This is rather not required & in future projects will prove to be a serious speed issue. You can just declare 1 *temp & re-use it then delete it in the end. All outside a loop. This is just an advice as I'd rather suggest you go with sstream.

char *newi= new char[10];
      char *newj= new char[10];
      char *newk= new char[10];
      . 
      .
      delete newi;
      delete newj;
      delete newk;

Thanks a lot nbaztec and jonsca . I used sstream and It works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.