Pendo826
Pendo826

Reputation: 1002

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...

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;
}

Upvotes: 0

Views: 2263

Answers (2)

Lews Therin
Lews Therin

Reputation: 10995

 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. Perhaps you really want to return a string. I'm not sure as you name your function getString, yet you return a 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?

To help/answer your question:

if(isDigitInString(name))
{

}

Upvotes: 2

DotNetUser
DotNetUser

Reputation: 6612

you trying to assign a bool value to a string hence an error. You should write something like this -

    if(getString(input1))
    {
       // code which continues the game
    } 
    else
    {
       // show some error message or ask for input again 
    } 

Upvotes: 0

Related Questions