0

I have tried alot of suggestion solutions to this problem with no success.

I have a const char array, of length 1000, called english_line which containing words seperated by whitespace. This array is passed into a function. This function must be used to implemented the solution as per our assignment brief.

I want to copy the contents of that array, one word at a time into another 2D array, temp_eng_word

char temp_eng_word[2000][50];
int j;

string line = english_line;
string word;

istringstream iss(line, istringstream::in);
while (iss >> word)
{
for (j=0;j<=2000;j++)
 {
 strcpy(temp_eng_word[j],word);
 }
}

`

When I run this, I get the error:

cannot convert 'std::string* *{aka std::basic_string(char)}' to 'const char*' for argument '2' to 'char* strcpy(char*, const char*)'

I have spent the best part of a day just trying to do this problem; obviously I am a relative novice at this.

Any tips or suggestions would be greatly appreciated :)

Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
  • With the condition `j<=2000` in your `for` loop, you're going to loop one time to many. – Some programmer dude May 07 '13 at 08:16
  • I also think the logic in your program is a little off. Right now you copy the first word into all 2000 (or 2001 if you don't change your condition) entries of the array. Then you copy the second word into all 2000 entries of the array, overwriting the first word. And so on. And why use an array of arrays? Why not a `std::vector` of `std::string`? – Some programmer dude May 07 '13 at 08:18
  • better use a std::vector with push_back... Then you don't have to care about the sizes – Mario May 07 '13 at 08:19
  • 1
    "I have a const char array, of length 1000, called english_line" Really? Not in this code. You might be better off describing the problem you are trying to solve rather than talking about 2D arrays and such. – juanchopanza May 07 '13 at 08:23
  • Actually, you don't need any manual looping at all, see [this example](http://ideone.com/6xBmj4). – Some programmer dude May 07 '13 at 08:28
  • 1
    You should be using `std::string` to represent strings. If you must use fixed-sized character arrays, then be careful to check the word length. Even if you restrict your program to non-technical English, where 50 characters should be long enough for any word you'll find in a mainstream dictionary, English text might describe the town of Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch. – Mike Seymour May 07 '13 at 08:29

4 Answers4

2

Use word.c_str() to get a const char* out of the std::string

Also, I don't understand the point of your nested for loop, you may want to do something like this instead (using strncpy to copy a maximum of 49 char with zero-padding if needed, and ensure the last char of the string is always zero) :

istringstream iss(line, istringstream::in);
int nWord = 0; 
while( (nWord < 2000) && (iss >> word) )
{
    strncpy(temp_eng_word[nWord], word.c_str(), 49);
    temp_eng_word[nWord][49] = '\0'; /* if it's not already zero-allocated */
    ++nWord;
}

Note that it would be simpler to use std::vector<std::string> to store your words :

vector<string> words;
istringstream iss(line, istringstream::in);
while(iss >> word)
{
    words.push_back(word);
}

Which could be done without a loop using std::copy :

copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(words));
zakinster
  • 10,508
  • 1
  • 41
  • 52
  • Thanks for your suggestions :) Please see the linked code. http://pastebin.com/xumTLKU0 I tried your first one, in the code, I print to the screen to test. It skips the first word on each line. Every second line, it adds on the last word from the previous line! -Odder things happen later too. As for suggestion 2, when outputting to the screen, it only prints out the first word in each line: I will take a break, and attempt to make sense of all this with a clearer mind! Thanks for the tips - it gives me hope! :) – user2357438 May 07 '13 at 17:59
0

Note the difference between string and char array. Char array is a simple structure of basic data type and string is actually a class having more complicated structure. That's why you need to use c_str() function of the string to get the content as char array (a.k.a C-string).

You should also notice that c_str() adds null termination (additional character '\0') in the end of its output array.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
quinz
  • 1,282
  • 4
  • 21
  • 33
0

1) Loop count wrong (You should correct your array knowledge)

2) string::c_str() to convert std::string to char*

deepdive
  • 9,720
  • 3
  • 30
  • 38
0

You can use string instead of that array temp_eng_word. Like,

std::string temp_eng_word;

Hope that will fix your problem. And the loop is not correct. Please check that, as you are using two dimensional array.

Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52
  • How would you store an array of `char*` in only one `string` ? I suppose you meant an array of `string` or a `vector` of `string`. – zakinster May 07 '13 at 09:45
  • If you can store , temp [][] = "abcdef"; You can also write, std::string temp = "abcd"; – Naseef Chowdhury May 07 '13 at 09:53
  • `temp[][] = "abcdef"` doesn't make any sense. The OP has a string such as `str[] = "this is a sentence"` and he splits it into multiple strings such as `result[0] = "this"`, `result[1] = "is"`, etc. So `result` must be either a `char[][]`, a `string[]`, or a `vector`. – zakinster May 07 '13 at 10:10