c++编译小问题 求解答

renjun0324 2012-07-08 02:29:50
#include<iostream>
using namespace std;
#include<string.h>
int main()
{
char password[20];
strcpy(password,"123456");
cout<<"please inter:"<<endl;
char a[20];
cin>>a;
if(a==password)
cout<<"yes!";
if(a!=password)
cout<<"no!";
}
如何才能把123456赋值给password?
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fzamygsd 2012-07-08
  • 打赏
  • 举报
回复
用c++的 string类吧
Gloveing 2012-07-08
  • 打赏
  • 举报
回复
一楼也挺快啊。。。
Gloveing 2012-07-08
  • 打赏
  • 举报
回复
记住字符串比较不是像单个字符一样直接比较
用:
strcmp, wcscmp, _mbscmp
Compare strings.

int strcmp( const char *string1, const char *string2 );

int wcscmp( const wchar_t *string1, const wchar_t *string2 );

int _mbscmp(const unsigned char *string1, const unsigned char *string2 );

#include<iostream>
using namespace std;
#include<string.h>

int main()
{
char password[20];
strcpy(password,"123456");
cout<<"please inter:"<<endl;
char a[20];
cin>>a;
if(!strcmp( a, password))
cout<<"yes!";
else
cout<<"no!";

return 0;
}

W170532934 2012-07-08
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;
#include<string.h>
int main()
{
char password[20];
strcpy(password,"123456");
cout<<"please inter:"<<endl;
char a[20];
cin>>a;
if(strcmp(password,a)==0)
cout<<"yes!";
else
cout<<"no!";
}

字符串比较,用==是不可以的。这样比较的只是字符串数组的地址。比较内容需要使用函数strcmp

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧