[英]1 IntelliSense: no suitable constructor exists to convert from “bool” to “std::basic_string<char, std::char_traits<char>, std::allocator<char>>”
Hey im getting this error when trying to validate a string. 嘿我在尝试验证字符串时遇到此错误。 Basically i want the game to not continue until the user enters a valid name.
基本上我希望游戏不会继续,直到用户输入有效名称。 John, Mary etc.. and not a number 123434 etc...
约翰,玛丽等..而不是数字123434等......
Here is my code: 这是我的代码:
string input1 ="What is your name ?\n";
string name = getString(input1);//The error is in the getString.
bool getString(string str)
{
for (int i = 0; i < str.size(); i++)
{
if (isdigit(str[i]))
return false;
}
return true;
}
string name = getString(input1);//The error is in the getString.
You can't cast a bool to a string or convert a bool
to string
, there is no implicit conversion
. 你不能将bool转换为字符串或将
bool
转换为string
,没有implicit conversion
。 Perhaps you really want to return a string. 也许你真的想要返回一个字符串。 I'm not sure as you name your function
getString
, yet you return a bool
. 我不确定你的函数
getString
,但是你返回一个bool
。
Unless you have a list of names handy and compare the input against the database, file etc.. comparing the input can be difficult. 除非你有一个方便的名字列表,并将输入与数据库,文件等进行比较。比较输入可能很困难。 What if the user enters Dsjdksdksdksdskd ?
如果用户输入Dsjdksdksdksdskd怎么办 ?
To help/answer your question: 帮助/回答您的问题:
if(isDigitInString(name))
{
}
you trying to assign a bool value to a string hence an error. 您尝试将bool值分配给字符串,因此出错。 You should write something like this -
你应该写这样的东西 -
if(getString(input1))
{
// code which continues the game
}
else
{
// show some error message or ask for input again
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.