I have a snippet of code that takes the output of another function (that function gets user input) and replaces all spaces with dashes. Or, rather, that's what it's supposed to do. Instead, it takes the first word of the string and ignores the rest (e.g. 'Hello World' -> 'Hello'). Here is the snippet:
void info::name(string* name, string title){
char arr[title.size() + 1];
strcpy(arr, title.c_str());
int o = 0;
while(arr[o] != 0){
if(arr[o] == ' '){
arr[o] = '-';
};
o++;
};
*name = arr;
Is there any reason why this wouldn't work?
EDIT: What do you mean by combining C-style arrays and std::string
?
EDIT2: I tried using std::replace, but the same thing happens.
EDIT3: I can't get getline() to work. Here is how I'm using it:
getline(cin, *title, "/n");
Why is this not working?
FINAL_EDIT: I finally got it to work! Here is what worked for me:
void info::title(string* title){
cout << "Enter the name of your mod: ";
getline(cin, *title); cout << endl;}
void info::name(string* name, string title){
replace(title.begin(), title.end(), ' ', '-');
*name = title;}
Once again, thanks, all!