我有一段代码,它接受另一个函数(该函数获取用户输入)的输出并用短划线替换所有空格。或者,相反,这就是它应该做的。相反,它取出字符串的第一个单词并忽略其余部分(例如'Hello World' - >'Hello')。这里是片段:字符串问题
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;
是否有任何理由为什么这不起作用?
编辑:你是什么意思组合C风格的阵列和std::string
?
EDIT2:我试过使用std :: replace,但同样的事情发生。
EDIT3:我无法让getline()工作。下面是我如何使用它:
getline(cin, *title, "/n");
为什么这不工作?
FINAL_EDIT:我终于明白了!这里是我的工作:
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;}
再次,谢谢,所有!
有很多原因实际上不起作用! –
@ Creep2DJ这个声明char arr [title.size()+ 1];在C++中无效:) –
TL; DR ['std :: replace'](http://en.cppreference.com/w/cpp/algorithm/replace)。 – juanchopanza