array of strings...more confusion

Just trying to do something very basic (and slightly pointless) with an array of strings...just to help with the learning process. Why won't the following work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main()
{
    string str_list[5];
    string list[5] = {"one", "two", "three", "four", "five"};
    int i;

    for(i=0; i<10; i++)
    {
        strcpy(str_list[i], list[i]);
        cout << str_list[i] << endl;
    }

    return 0;
}
Last edited on
You can't use the C string functions with C++ strings.
Instead of calling strcpy, you should write str_list[i]=list[i];
You should write something as below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int N = 5;
    string str_list[N];
    string list[N] = {"one", "two", "three", "four", "five"};
    int i;

    for(i=0; i<N; i++)
    {
        str_list[i] = list[i];
        cout << str_list[i] << endl;
    }

    return 0;
}


Or you can use standard algorithms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    const int N = 5;
    string str_list[N];
    string list[N] = {"one", "two", "three", "four", "five"};

    copy( list, list + N, str_list );

    for_each( str_list, str_list + N,
                    []( const string &s ) { cout << s << endl; } );
    return 0;
}

Last edited on
I've tried that but it just outputs nonsense in the console window. Also, does that mean you have to use character arrays if you want to use the string functions?
Maybe you tried something that looks like that but isn't the same.
http://ideone.com/o09U1

Also, does that mean you have to use character arrays if you want to use the string functions?

In C++, this is a string:
string someString;

This is a pointer to the first element in an array of char:
char* somePointer = "EggsOnToast";

strcpy does not operate on strings. You can check that by looking it up. It operates on char pointers. If you have a function that operates on char pointers, you must feed it char pointers. If you have a function that operates on strings, you must feed it strings.

In C, people often used the word "string" to refer to an array of char. Now that we have proper C++ string objects, you should take care not to get the two mixed up. This is what you did in your code above; you tried to feed a string to something that expected a char pointer.
Last edited on
This is my code now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str_list[5];
    string list[5] = {"one", "two", "three", "four", "five"};
    int i;

    for(i=0; i<5; i++)
    {
        str_list[i] = list[i];
        cout << str_list[i] << endl;
    }

    return 0;
}


and this is what I get in the console window:
au-[0ub3Aub3A( c3AA
Process returned 0 (0x0) execution time: 0.017s
Press any key to continue. 
Last edited on
You have arrays of five elements but in the loop you are trying to deal with ten elements. Change from
for(i=0; i<10; i++)
to
for(i=0; i<5; i++)
Topic archived. No new replies allowed.