Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

String to char C++


ghost's Avatar
0 0

I'm writing a program (a command prompt) in C++ and need to get a string into a char array, however the stringstream stops after getting to the first space… is there any way to get inputted data (wheter from a string or not) into a char array including any spaces


ghost's Avatar
0 0

A string is an array of characters. I don't understand what the problem is. Post a snippet of your code if it is too long so it is visible.


ghost's Avatar
0 0

Don't have code with me… But it won't let you use the system() function with a string… only a char array


ghost's Avatar
0 0

when getting more than one word in cin you have to use cin.get() so try stringstream.get()

EDIT: If your prob is system then use string.c_str()

for example

string asd = "cd .."; system(asd.c_str())


ghost's Avatar
0 0

I think 8i got it to work… But the "cd" command doesn't seem to work… Should it?


alfredwolf's Avatar
Member
0 0

its hard to have it do CD when its looping around if you have a char array you can easily run any command but i dont think it will properly do the CD


ghost's Avatar
0 0

@Zeke: Yeah but the problem is that "system" function won't allow you to use a string… i'll just try again after a few days of googling


ghost's Avatar
0 0
{
const char* new;
new = cpp_string.c_str();
return new;
}```