如题所示,是我在执行我的程序时,在密码函数的某部分出错了。实际上它是一个基本的密码函数,在 Turbo C++ 中可以正常工作,但在 Visual C++ 中 这个错误来了
void user::password()
{
char any_key, ch;
string pass;
system("CLS");
cout << "\n\n\n\n\n\n\n\n\t\t\t\t*****************\n\t\t\t\t*ENTER
PASSWORD:*\n\t\t\t\t*****************\n\t\t\t\t";
start:
getline(cin,pass);
if (strcmp(pass, "sha") == 0) //this is where the error is!*
{
cout << "\n\n\t\t\t\t ACCESS GRANTED!!";
cout << "\n\t\t\t PRESS ANY KEY TO REDIRECT TO HOME PAGE";
cin >> any_key;
}
else
{
cout << "\n\t\t\t\t ACCESS DENIED :(,RETRY AGAIN!!\n\t\t\t\t";
goto start;
}
system("CLS");
}
最佳答案
if语句中的表达式
if (strcmp(pass, "sha") == 0)
不正确。
该函数需要类型为 const char * 的两个参数,而您提供类型为 std::string 的第一个参数,并且没有从类型 std::string 到类型 const char * 的隐式转换。
改用
if ( pass == "sha" )
在这种情况下,由于非显式构造函数,存在从类型 const char *(从数组类型隐式转换后的字符串文字的类型)到类型 std::string 的对象的隐式转换
basic_string(const charT* s, const Allocator& a = Allocator());
关于c++ 不存在从 "std::string"到 "const char*"的合适的转换函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57775764/