Is there a way to convert and substr index of a str to char?
I haven't compiled this yet, so there might be error. But, you can see I'm trying to convert a substr index into a char so I can use it in a switch statement.
(Side note: you can use a string in the same manner as a character array, just use the [ ] brackets like you where working with a character array)
this code should work in any program you throw at it as long as you tell the compiler what mystring is.
1 2 3 4 5 6 7 8
string mystring;
int slength;
/*code to set the contents of 'mystring' */
slength = mystring.length();
char mychar[slength];
for(int a = 0; a <= slength; a++){
mychar[a] = slength[a];
}
this will take every character in the string into the array, including spaces from the space bar, but again, for most uses(don't hold me to it, I could be wrong) just use a string as if it were an array and it will do what you want it to do.
EDIT: this will make the character array the exact length of the string and won't quit the for loop till it copies all the characters. and make sure you include the string header. #include<string>