【发布时间】:2020-07-02 17:27:13
【问题描述】:
我还注意到
例如在 dev c++ 中
#include<string>
#include<strings.h>
using namespace std;
int main()
{
string a="admin";
string b;
cin>>b;
if(strcmp(a,b)==0)
{
cout<<"Equal";
}
}
因此编译器在调用 strcmp() 函数的行显示错误。
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
这个错误实际上意味着什么以及如何解决这个问题以制作登录菜单。
请帮我解决这个问题
我也知道使用 c 类型字符串的替代方法如下,效果很好
#include<strings.h>
using namespace std;
int main()
{
char user[32]="admin",u[32]="";
char password[32]="admin",p[32]="";
cout<<"\nLOGIN>>\n\nEnter User Name:";
cin.getline(u,32);
cout<<"Enter Password:";
cin.getline(p,32);
if(strcmp(u,user)==0 && strcmp(p,password)==0)
{
cout<<"Match";
}
}
但我想比较
【问题讨论】:
-
只使用
a == b有什么问题? -
什么是
strings.h? C 仅包含string.h或在 C++ 中您应该使用cstring(不包含扩展名) -
请注意,cstring 是 C 编程语言中 string.h 的 c++ 版本。 C 没有
std::string,因此C 函数与std::string交互的能力是有限的。 -
非常感谢先生 a==b 工作正常。我不知道
-
先生,我们可以使用赋值运算符复制这些字符串吗?
标签: c++ string authentication copy compare