Hello Everybody,

For a sorting project I'm working on, I have a string array, with 50 values. I need to convert each string in the array to a two dimensional char array.

For example, Let's say there is a string array[50]. Each position in the array has a name in it. Like say,
array[0] == david, array[1] == robert, etcetera
There's also a char array2[50][32].
array2[0][0] == d, array2[0][1] == a, array2[0][2] == vand so on

Well, from the example above, the string array[50] exists. It contains 50 distinct names (as a matter of interest, the names are from star wars). The very needed char array2[50][32] does not exist. I need that char array[50][32], and it should be pretty much full. If I have that char array, I can do things like convert to ascii/unicode , sort the values, and all sorts of fun stuff like that.

Again, I'm trying to convert a string array[50] into a char array2[50][32]. Some code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream myfile("names.dat");
int count = 0;
string names[50];
char array2[50][32]; // this is the data I need
int ascii[50][32]; //this should hold converted strings
while (count < 50) { //read the data from 'names.dat'
myfile >> names[count]; // and store it in names array
count++; //this loop works fine.
count = 0;
//right here should be a loop that converts
//one by one the array 'names[50]' into the
//char array 'array2[50][32]

//right here should be a conversion array,
//taking the charr array[50][32] and
//converting it to ascii, but I don't need
//any help with this

//right here should be a selection sort array,
//using the converted asciis, but I don't need
//any help with this

Please help me! I need help very badly! :icon_cry:

6 Years
Discussion Span
Last Post by Fbody
Featured Replies
  • 1

    [URL="http://www.cplusplus.com/reference/string/string/c_str/"]Check out the function std::string::c_str().[/URL] Then add some commands to your while loop. Read More


Thanks, I think... But is there a good way to break a string down into individual letters?


Did you read the link? c_str() returns a c-style string which is char*stringName[].

All you have to do is store that return then use stringName[x] to access the xth character in the string.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.