String parameterization as argument


Hi all,

how how can i parameterize the number of string according to this example:

class Prob{
Prob(string);
};

int{
Prob *test[2];

test[0] = new ("test-0");
test[1] = new ("test-1");
}

how can i prameterize the string name as the following:

for(int i=0; i<2; i++){
test[i] = new ("test-"); // what i have to do to get it test-0, test-1...
}

thank a lot for each support...
Best regards
Are you trying to do convert from integer to a null-terminated string?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
class Prob
{
      public:char nameProb[256];
      public:Prob(char*name,int number)
             {//in this array we will store the converted integer
              char n[256];

              //function that converts integers to ascii ("integer to ascii"), third parameters describes the numerical base (decimal in this case:10)

              itoa(number,n,10);
              strcpy(nameProb,name);
              strcat(nameProb,n);
              
              cout<<nameProb<<endl;
             }
};

int main()
{   Prob* probArray[100];
    for(int i=0;i<100;i++)
      {probArray[i]=new Prob("test",i);
      }
    cin.get();
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

class Prob
{
	string name;
public:
	Prob(string n)
	{
		name=n;
	}
	string getName() 
	{
		return name;
	}
};

int main()
{
	const probSize=2;
	Prob *test[2];
	int i;
	for(i=0;i <probSize; i++)
	{
		string s="test-";
		ostringstream o;
		o << i;
		s.append(o.str());
		test[i]= new Prob (s);
	}

	for(i=0; i<probSize; i++)
	{
		cout << "Prob["<<i<<"] is " << test[i]->getName() << endl;
	}

	return 0;
}

Hi all,

first, thanks a lot for your support.

for the first replay from Nicolas, it still dose not work because my cygwin compiler can not regognaize the function itoa(). I have searched in this website for this function and found that there is alternative for it namly sprintf(). But this function can not convert int to char...

do you have please a tip how to fix it ?


thanks a lot in advance
Last edited on
my initial posting was with string streams but if you want to use spritf you can use something like this
1
2
3
4
5
6
7

char buff[2]; // to hold int
char name[256]="test-"; // or whatever you like
int i=1;
sprintf(buff,"%d",i); // convert int to chars
strcat(name,buff); // concatenate 
	
Topic archived. No new replies allowed.