if(strcmp(game[i].playname,name)==0)
定义game为一个类的对象数组, playname[8]为char 类型,name[8]为另一个类中的char类型变量
为什么比较不了``
#include<iostream>
#include<string>
using namespace std;
static int k=0;
void sm()
{
cout<<"\t\t申请游戏账号说明\n";
cout<<"a.账号位数不得大于8位字符,可为字母";
cout<<"或数字组合,也可以是单独的字母或数字,";
cout<<"但必须以字母开头\n";
cout<<"b.游戏密码不得大于6位字符,可为字母";
cout<<"或数字组合,也可以是单独的字线或数字\n";
}
void sm1()
{
cout<<"\t\t游戏说明\n";
cout<<"1.进入游戏,请输入'1'\n";
cout<<"2.申请帐号,请输入'2'\n";
cout<<"3.退出,请输入'0'\n";
cout<<"请选择功能:";
}
class play
{
public:
string playname;
string playkey;
play(string n,string k)
{
playname=n;
playkey=k;
}
};
class goplay
{
private:
string name;
string key;
string key1;
public:
int nameworng,nameworng1,f,i,keyworng;
string s;
void nscanf()
{
sm();
cout<<"帐号:";
cin>>name;
f=name.size();
for(i=0;i<f;i++)
if(!(((name[i]>='a'&&name[i]<='z')||
(name[i]>='A'&&name[i]<='Z')||
(name[i]>='0'&&name[i]<='9'))&&
(name[0]>='a'&&name[0]<='z')||
(name[0]>='A'&&name[0]<='Z')))
{nameworng=1;break;}
for(i=k;i>=0;i--)
if(strcmp(game[i].playname,name)==0)
{nameworng1=1;break;}
}
void npd()
{
while(f>8||nameworng==1||nameworng1==1)
{
if(f>8)
{
cout<<"你申请的帐号大于8位字符\n";
nscanf();
}
if(nameworng==1)
{
cout<<"你的帐号含有非法字符\n";
nscanf();
}
if(nameworng1==1)
{
cout<<"你申请的账号以存在\n";
nscanf();
}
}
game[++k].playname=name;
}
void kscanf()
{
cout<<"密码:";
cin>>key;
f=key.size();
for(i=0;i<f;i++)
if(!((key[i]>='a'&&key[i]<='z')||
(key[i]>='A'&&key[i]<='Z')||
(key[i]>='0'&&key[i]<='9')))
{keyworng=1;break;}
}
void kpd()
{
while(f>6||keyworng==1)
{
if(f>6)
{
cout<<"你的密码大于6位字符\n";
kscanf();
}
if(keyworng==1)
{
cout<<"你的密码含有非法字符\n";
kscanf();
}
}
}
void kscanf1()
{
cout<<"验证密码:";
cin>>key1;
while(!strcmp(key,key1)=0)
{
cout<<"密码不一致,请重设\n";
kscanf();
kpd();
cout<<"验证密码:";
cin>>key1;
}
game[k].playkey=key;
cout<<"恭喜你,以申请成功,OY\n";
}
};
class comeplay
{
private:
string goname;
string gokey;
public:
void print()
{
cout<<"帐号码:";
cin>>goname;
cout<<"密码:";
cin>>gokey;
for(int i=0;i<=k;i++)
if(strcmp(game[i].playname,goname)=0&&
strcmp(game[i].playkey,gokey)=0)
int right=1;
if(right==1)
cout<<"欢迎进入本游戏,祝你在游戏中愉快!\n";
else
cout<<"你输入的账号不存在,请申请\n";
}
};
int main()
{
play game[100]={{"gogogo","123456"}};
goplay dc1;
comeplay dc2;
int x;
sm1();
cin>>x;
do
{
switch(x)
{
case 1:
dc1.nscanf();
dc1.npd();
dc1.kscanf();
dc1.kpd();
dc1.kscanf1();
break;
case 2:
dc2.print();
break;
case 0:
exit(0);
default:cout<<"你的输入有误,请重新输入\n";
}
sm1();
cin>>x;
}while(x!=0);
getchar();
return 0;
}
我对string函数还不是很了解`
我还是用char算了`搞定了
#include<iostream>
#include<string>
using namespace std;
static int k=0;
void sm()
{cout<<"\t____________________________________________________\n";
cout<<"\t\t\t***申请游戏账号说明***\n";
cout<<"\t\ta.账号位数不得大于8位字符,可为字母或数字组合,\n";
cout<<"\t\t 也可以是单独的字母或数字,但必须以字母开头\n";
cout<<"\t\tb.游戏密码不得大于6位字符,可为字母或数字组合\n";
cout<<"\t\t ,也可以是单独的字线或数字\n";
cout<<"\t____________________________________________________\n";
}
void sm1()
{cout<<"\t__________________________________________\n";
cout<<"\t\t\t***游戏说明***\n\n";
cout<<"\t\t1.进入游戏,请输入“1”。\n";
cout<<"\t\t2.申请帐号,请输入“2”。\n";
cout<<"\t\t3.退出,请输入“0”。\n";
cout<<"\t__________________________________________\n\n";
cout<<"请选择功能:";
}
class
{
public:
char playname[8];
char playkey[7];
}game[100]={{"gogogo","123456"}};;
class goplay
{
private:
char name[8];
char key[7];
char key1[7];
public:
int nameworng,nameworng1,f,i,keyworng;
string s;
void nscanf()
{
sm();
cout<<"帐号:";
cin>>name;
f=strlen(name);
for(i=0;i<f;i++)
if(!(((name[i]>='a'&&name[i]<='z')||
(name[i]>='A'&&name[i]<='Z')||
(name[i]>='0'&&name[i]<='9'))&&
(name[0]>='a'&&name[0]<='z')||
(name[0]>='A'&&name[0]<='Z')))
{nameworng=1;break;}
for(i=k;i>=0;i--)
if(strcmp(game[i].playname,name)==0)
{nameworng1=1;break;}
}
void npd()
{
while(f>8||nameworng==1||nameworng1==1)
{
if(f>8)
{
cout<<"你申请的帐号大于8位字符\n";
nscanf();
}
if(nameworng==1)
{
cout<<"你的帐号含有非法字符\n";
nscanf();
}
if(nameworng1==1)
{
cout<<"你申请的账号以存在\n";
nscanf();
}
}
strcpy(game[++k].playname,name);
}
void kscanf()
{
cout<<"密码:";
cin>>key;
f=strlen(key);
for(i=0;i<f;i++)
if(!((key[i]>='a'&&key[i]<='z')||
(key[i]>='A'&&key[i]<='Z')||
(key[i]>='0'&&key[i]<='9')))
{keyworng=1;break;}
}
void kpd()
{
while(f>6||keyworng==1)
{
if(f>6)
{
cout<<"你的密码大于6位字符\n";
kscanf();
}
if(keyworng==1)
{
cout<<"你的密码含有非法字符\n";
kscanf();
}
}
}
void kscanf1()
{
cout<<"验证密码:";
cin>>key1;
while(!strcmp(key,key1)==0)
{
cout<<"密码不一致,请重设\n";
kscanf();
kpd();
cout<<"验证密码:";
cin>>key1;
}
strcpy(game[k].playkey,key);
cout<<"恭喜你,以申请成功,OY\n";
}
};
class comeplay
{
private:
char goname[8];
char gokey[7];
public:
int right;
void print()
{
cout<<"帐号:";
cin>>goname;
cout<<"密码:";
cin>>gokey;
for(int i=0;i<=k;i++)
if(strcmp(game[i].playname,goname)==0&&
strcmp(game[i].playkey,gokey)==0)
right=1;
if(right==1)
cout<<"欢迎进入本游戏,祝你在游戏中愉快!\n";
else
cout<<"你输入的账号不存在,请申请\n";
}
};
int main()
{
goplay dc1;
comeplay dc2;
int x;
sm1();
cin>>x;
do
{
switch(x)
{
case 2:
dc1.nscanf();
dc1.npd();
dc1.kscanf();
dc1.kpd();
dc1.kscanf1();
break;
case 1:
dc2.print();
break;
case 0:
exit(0);
default:cout<<"你的输入有误,请重新输入\n";
}
sm1();
cin>>x;
}while(x!=0);
getchar();
return 0;
}
我写了二个程序,一个是上面的申请游戏帐号(含有三个类),另一个是游戏(含有三个类)
我想在一个程序中包含这二个程序,为什么包括不了,这样子把它们组在一个程序里,代码看起来就没有那么长了`
下面是我调试课本的程序也不得:
文件filemax.h的内容为:
int max(int x,int y)
{
int z;
if(x>=y)
z=x;
else
z=y;
return z;
}
文件filemain.cpp的内容为:
#include<iostream>
#include"filemax.h"
using namespace std;
int main()
{
int a,b,m;
cout<<"Input a,b";
cin>>a>>b;
m=max(a,b);
cout<<m<<endl;
return 0;
}