字符串类型从用户获取输入'space'
String Type getting input from user with 'space'
>我正在尝试获取一个字符串处理程序,该处理程序从字符串类型变量中的用户那里获取输入...但它正在崩溃,我想知道为什么?或者我做错了什么...
string UIconsole::getString(){
string input;
getline(cin,input);
if(input.empty() == false){
return input;
}
else{getString();}
return 0;
}
编辑:错误:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
您有几个错误,但消息引用的特定错误是这样的:
return 0;
不能从空指针构造std::string
。如果需要空字符串,请尝试以下结构之一:
return "";
return std::string();
<小时 />您的另一个错误是对getString()
的递归调用。完全不清楚你想在那里做什么。也许这符合您的要求:
// untested
std::string UIconsole::getString(){
std::string input;
while(std::getline(std::cin, input) && input.empty()) {
// nothing
}
return input;
}
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- 字符串类型从用户获取输入'space'
- 没有输入数字时,如何删除"Sorted:[space]"后的空格?