首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++编译小疑点 求解答

2012-08-02 
c++编译小问题 求解答#includeiostreamusing namespace std#includestring.hint main(){char passwor

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?

[解决办法]

C/C++ code
#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 );

C/C++ code
#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;} 

热点排行