char loop

Hi, How can write this in a loop ?

1
2
3
4
5
   char *names[5] = {"Name_1_sur_1", 
                     "Name_2_sur_1",
                     "Name_3_sur_1",
                     "Name_4_sur_1",
                     "Name_5_sur_1}; 








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const char* names[5];
const char* prefix = "Name_";
const char* suffix = "_sur_1";

for(int i=0; i<5; i++)
{
     //1. Create a std::string initially containing prefix

     //2. Convert i+1 into a char* using the itoa function

     //3. Append the char* determined above to the string

     //4. Append the suffix to the string

     //5. Set the current name to be the const char* contained in the string (using c_str)
}
thanks for the answer.

if I knew how to do this, I could do ; ) . however, I am trying to apply the steps that you list.
Instead of using itoa in number 2, you could use stringstreams. They're easier to use (and, of course, they're not a C-style function!)
http://www.cplusplus.com/forum/beginner/1057/

Don't hesitate to ask if you have any problems.
I've tried to do it likes this:

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main () {

        //string prefix = "Name_";
        //string suffix = "_sur_1";
        const char* names[5];
        const char* prefix = "Name_";
        const char* suffix = "_sur_1";

        for (int i=0; i<5; ++i) {
                //string s = prefix;
                stringstream ss;
                ss << i+1;
                string name;
                name.append(prefix + ss.str() + suffix);
                //cout << prefix + ss.str() + suffix << " name = " << name << endl;
                //cout << "name.str = " << name.c_str() << endl;
                names[i] = name.c_str();
                cout << "names[" << i << "] = " << names[i] << endl;
        }
                cout << "==============" << endl;
        for (int i=0; i<5; ++i) {
                cout << "names[" << i << "] = " << names[i] << endl;
        }
}


and it seems not correct.

names[0] = Name_1_sur_1
names[1] = Name_2_sur_1
names[2] = Name_3_sur_1
names[3] = Name_4_sur_1
names[4] = Name_5_sur_1
==============
names[0] = Name_5_sur_1
names[1] = Name_5_sur_1
names[2] = Name_4_sur_1
names[3] = Name_5_sur_1
names[4] = Name_5_sur_1


I don't understand where is the problem. ??

closed account (zb0S216C)
This is all that I could come up with, sorry:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    std::string Names[ 5 ];

    char Numbers[ 5 ] = 
    {
        '1', '2', '3', '4', '5'
    };

    for( int i( 0 ); i < 5; i++ )
    {
        Names[ i ].assign( "Name_" );
        Names[ i ].push_back( Numbers[ i ] );
        Names[ i ].insert( Names[ i ].length( ), "_sur_" );
        Names[ i ].push_back( Numbers[ i ] );

        std::cout << Names[ i ] << std::endl;
    }

    std::cin.ignore( std::numeric_limits < std::streamsize >::max( ), '\n' );
    return 0;
}


Wazzak
I don't understand where is the problem. ??

This:

name.append(prefix + ss.str() + suffix);

should be

1
2
3
4
name = prefix;
name.append(ss.str());
name.append(suffix);
ss.clear();

@ Framework,
thanks for pointing the other way, however, I don't want to initialize the array elements like the below:
1
2
3
4
    char Numbers[ 5 ] = 
    {
        '1', '2', '3', '4', '5'
    };


@ stacktar

it doesn't work again.

if you are not sure about the solution, please try it first and write here when you get a reasonable output.


names[0] = Name_1_sur_1
names[1] = Name_2_sur_1
names[2] = Name_3_sur_1
names[3] = Name_4_sur_1
names[4] = Name_5_sur_1
==============
names[0] = Name_5_sur_1
names[1] = Name_5
names[2] = Name_5_sur_1
names[3] = Name_5
names[4] = Name_5_sur_1
if you are not sure about the solution, please try it first and write here when you get a reasonable output.

Sorry, just trying to help, here :p

Although, the reason it wasn't working is due to a bone-headed, beginner mistake on my part (and also I didn't see the two outputs in the program. I thought the top was expected and the bottom was the outputted NOT that the top was the output of the first loop. My bad.) The problem was the const char*s returned from the string were going out of scope. You need to do dynamic allocation to get this to work.

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main () 
{
        char* names[5];
        const char* prefix = "Name_";
        const char* suffix = "_sur_1";

        string name;
        stringstream ss;

        for (int i=0; i<5; ++i) 
        {
                ss << i+1;
                name = (prefix + ss.str() + suffix);
                ss.str(string()); //this clears the stringstream

                names[i] = new char[strlen(name.c_str()) + 1];
                strcpy(names[i], name.c_str());

                cout << "names[" << i << "] = " << names[i] << endl;
        }

        cout << "==============" << endl;

        for (int i=0; i<5; ++i) 
        {
                cout << "names[" << i << "] = " << names[i] << endl;
        }

        for(int i=0; i<5; i++)
        {
               delete [] names[i];
        }
}


Although, if your requirements permit, I would suggest switching to std::strings. They're nicer to work with because they deal with the memory management themselves.

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main () 
{
        string names[5];
        const char* prefix = "Name_";
        const char* suffix = "_sur_1";
        
        stringstream ss;

        for (int i=0; i<5; ++i) 
        {
                ss << i+1;
                names[i] = (prefix + ss.str() + suffix);
                ss.str(string()); //this clears the stringstream

                cout << "names[" << i << "] = " << names[i] << endl;
        }

        cout << "==============" << endl;

        for (int i=0; i<5; ++i) 
        {
                cout << "names[" << i << "] = " << names[i] << endl;
        }
}
Last edited on
it's good example to learn some about strings and so on.

thanks shacktar . it works.........
Topic archived. No new replies allowed.