[英]How do I get rid of the “Intellisense: no suitable conversion function from ”std::string“ to ”std::string *“ exists” error?
[英]6 IntelliSense: no suitable conversion function from “std::string” to “int” exists
嘿,我正在驗證一個字符串。
string getString(string q)
{
string input;
do
{
cout << q.c_str() << endl;
cin >> input;
} while (!isalpha(input));
return input;
}
當使用while(!isalpha(input));
輸入給出該錯誤。
誰能幫我這個?
另一個答案描述了問題所在,但這是一個使用標准庫中算法的解決方案,而不是自己編寫(示例要求C ++ 11)
bool all_alpha( std::string const& s )
{
return std::all_of( s.cbegin(), s.cend(), static_cast<int(*)(int)>(std::isalpha) );
}
僅當字符串中的所有字符均為字母時,上述函數才會返回true。 如果只想禁止數字字符,我將使用稍微不同的功能。
bool any_digit( std::string const& s )
{
return std::any_of( s.cbegin(), s.cend(), static_cast<int(*)(int)>(std::isdigit) );
}
要么
bool no_digits( std::string const& s )
{
return std::none_of( s.cbegin(), s.cend(), static_cast<int(*)(int)>(std::isdigit) );
}
使用這些功能可以驗證您從用戶那里收到的輸入。
如果您不能使用C ++ 11層的功能,這些功能可以修改為使用std::find_if
,而是和比較的返回值find_if
到s.end()
來確定是否成功/失敗。
isalpha函數采用整數作為參數,但是您要向其傳遞std::string
。 您可以編寫如下函數來測試您的字符串是否僅包含字母字符:
bool noDigitInString(std::string str)
{
for (int i = 0; i < str.size(); i++)
{
if (isdigit(str[i]))
return false;
}
return true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.