Converting an String[] to Char[]

Socius Decennalis
Decennium
Joined
Mar 30, 2013
Messages
474
Reaction score
56
http://pastebin.com/WAxU1Sxu


Code:
  #include <string>
#include <sstream>
#include <iostream>
using namespace std;


	//global variables
	string stringHolder = "";
	string arr[4];
	double option;


class Ascii { 


public: 
	//outputs the options
	void output() { 
		cout<< "1Encrypt-2Decrypt-3Both" << endl;
		cin >> option;
		cout << endl << "Enter String(Dont make it longer than four words):";
		cin.ignore();
		getline(cin,stringHolder);
	}
	//breaks string into words
	void stringBreaker() {
		int i = 0;
		stringstream ssin(stringHolder);
		while (ssin.good() && i < 4){
        ssin >> arr[i];
        ++i;
	}
		for(i = 0; i < 4; i++) {
			 cout << arr[i] << endl;
		}
	}


	//breaks string into char array
	void charBreaker() {


	}
	//will output both encrypted and decrypted versions
	string all() {
		stringBreaker();
		return (arr[1], " ", arr[2], " ", arr[3], " ", arr[4]);
	}
	//encrypts to ascii
    string encyrpt() { 
		stringBreaker();
		return (arr[1], " ", arr[2], " ", arr[3], " ", arr[4]);
	}
	//decrypts from ascii
    string decrypt() { 
				stringBreaker();
		return (arr[1], " ", arr[2], " ", arr[3], " ", arr[4]);
	}


	private:
	//you dont have privates
	};


int main() {
	//local variables
	string result;
	string output;
	Ascii ascii; 


	//output
	ascii.output();


	if (option == 1) {
			result = ascii.encyrpt();
	} else if(option == 2) {
			result = ascii.decrypt();
	} else {
		result = ascii.all();
	}
	cout << "Result " << result;
	system("Pause");
	return 0; 
}


So, I have an array, arr[4], which can hold four words set into it from stringHolder. I want to break each word inputted into arr[4] into an array of characters. No clue how to do this, thanks to anyone who helps.
 
I may be wrong but couldn't you use the explode function and tell it to use "" as the area to do this it should return an array with each letter?
I could be completely off though I have not worked around c++ or c# in a while x.x
 
I think this is what I was thinking of

Code:
varname = strVal.Split('.');
where the . is it should split so '' would split between each letter
 
So I am not entirely sure on what you are trying to do, but there are some errors in your code, specifically in this line:

PHP:
return (arr[1], " ", arr[2], " ", arr[3], " ", arr[4]);

Arrays are indexed from 0, which means that arr[4] is an out of bounds index. Since arr is an array of length 4, the elements would be like this:
element 1 - arr[0]
element 2 - arr[1]
element 3 - arr[2]
element 4 - arr[3]

Secondly, I think you are trying to concatenate them, but the concatenation operator for strings is +, not ,. Assuming you fix the indexing issues, your code would ignore all elements except the last one.

As for converting strings to char arrays, one way to do it is to use the function string.c_str(), which returns a c-string representation (char array ending with '\0') of the string.
 
Back