c++编译小问题 求解答
#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?
[解决办法]
#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, 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;}