assign char array to string array

how can i go about assigning all of the values in char array
to one element in string array
Last edited on
Anyone?
You should be more specific. Maybe even show the variable declarations. For example, I am unsure if by "string array" you mean std::string *pArray, or if you mean std::vector<std::string> myArray. Or maybe you don't refer to STL strings at all, and you simply use the word "string" to refer to C-style strings, which really are char arrays.

You also say that the origin of the data is a char array, which is a simple C string (as stated above). So, I would like to know what transformation you have in mind between a single C string (the char array) and an array of strings.
Well say
char array = "hello"
char array2 = "world"

So what I need is array to be assigned to array3[0] and array2 to array3[1]

String array3[0] = array
string array3[1] = array2

But I get an invalid conversion error
If you are trying actual code and it doesn't work, then post the actual code here along with the verbatim of the error message(s). It is far easier to get help this way. Also remember that you must put your code between code tags:

[code]
1
2
3
4
//Code in code tags get line numbers
//and nice coloring so it is easy to read.
//It also makes things easy for the expert to
//point out errors by mentioning the line #. 

[/code]

As for your code snippets, I can only guess that you meant "string" instead of "String" (C++ is a case-sensitive language), and that you actually intended to be "std::string".

You also failed to show the declaration of the array3 variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
char temp[25];
string stack[5];
int cursize = 0;
     
     do
     {
     cout << "Write your input(group with [],(),{}): " << flush;
     cin >> temp;
     temp = stack[cursize]; //incompatible types in assignment of `std::string' to `char[25]' 
     cursize++;
     } while (cursize < 4);
     cout << stack << endl;
     
    system("PAUSE");
    return 0;
}
Last edited on
You have it backwards. Line 11 should be:

stack[cursize] = temp;
that gives me an output of some address

output: 0x22fef0
Yes because you are trying to output the entire collection of strings with a single statement (my bad I didn't see line #14 before).

You have to iterate through the items in the collection and output them one at a time.

1
2
3
4
    for (int i = 0; i < 5; i++)
    {
        cout << "Line #" << i << ":  " << theStack[i] << endl;
    }


Also in line #13, you should iterate while you are below 5, not below 4.
SUCCESS!!!
Thank you webJose!
Topic archived. No new replies allowed.