0

I have read several lines from a text file using std::getline, but now I need to convert the string array of lines into a char array so that I can use isalpha and isdigit. The ultimate goal here is to identify which chars are numbers and which are letters.

i.e.:

convert string content[50] to an array of chars.

I have tried using strcopy, but it doesn't let me use that string because "'string[50]' is not a structure or union"

Any help is appreciated, thank you.

Code4Fun
  • 41
  • 1
  • 5
  • `string.c_str()` returns the C-style string inside the `std::string`. – Barmar Sep 04 '15 at 01:35
  • Why not use the string functions instead of the c_string ones? – stark Sep 04 '15 at 01:35
  • how about `content[0].c_str() ?` for contents in range [0..49] – Arun Sep 04 '15 at 01:35
  • http://stackoverflow.com/questions/16029324/c-splitting-string-into-array – Abend Sep 04 '15 at 01:35
  • 3
    Why do you need to convert it into a char array in the first place? You can access characters of a `std::string` and then call `isalpha()` on them. `contents[0][10]` will access the character 10 in the first string in the array. – Barmar Sep 04 '15 at 01:36
  • For instance, `isalpha(contents[i][j])` – Barmar Sep 04 '15 at 01:37
  • This is the first time I've ever used std::getline, as well as isalpha and isdigit, so please excuse my ignorance. when I check to see where content[1][0], content[1][1], and content[1][2] start it shows me "4nt". the '4' is indeed part of my file, but the 'nt' is/are not... where are those coming from? @Barmar – Code4Fun Sep 04 '15 at 01:57
  • What's in the second line of the file? Make sure you're not accessing beyond the end of the string. – Barmar Sep 04 '15 at 02:07
  • the first line is comprised of 36 characters, second line is 24, and the last line is 1. However, there will be different input during testing, so I can't hard code it to just one unique file @barmar – Code4Fun Sep 04 '15 at 02:13
  • When you're looping over a string, use `.contents[i].size()` to limit the loop to the size of that string. – Barmar Sep 04 '15 at 02:15

1 Answers1

0
std::string[50] arr;
//Populate somehow

std::stringstream sstream;

for(str : arr) {
   sstream << str;
}

char* final_char_array = sstream.str().c_string();
Brad
  • 2,261
  • 3
  • 22
  • 32