完善一个程序
完善两个功能:在输入性别(male\female)前,进行判断,如果输入的不是(male\female)则提示“重新输入性别”;在输入年龄前进行判断,如果输入的年龄> 100或 <0则提示 "重新输入年龄“。
#include <iostream.h>
#include <string.h>
int count =1;
struct readerInfo
{
char ID[30];
char name[20];
char gender[8];
char age[4];
char city[20];
bool isValled;
/*char getGender(char gender[])
{
if(strcmp(gender,)!=0 && strcmp(gender,)!=0) )
return (gender[]);
else
return 0;
}
int getAge(char age[])
{
if(((age[0]> '0 ') && (age[0] <= '9 '))||((age[1]> = '0 ')&&(age[1] <= '9 ')))
return (age[0]- '0 ')*10+(age[1]- '0 ');
else
return 0;
}
*******/
}reader[1000];
void addReader();
void modifyReader();
void deleteReader();
void queryReader();
void main()
{
int choice = 1;
while(true)
{
cout < < " 图书馆读者管理系统\n ";
cout < < "-------------------------------------------\n ";
cout < < " -1- 添加读者\n ";
cout < < " -2- 修改信息\n ";
cout < < " -3- 删除信息\n ";
cout < < " -4- 信息查询\n ";
cout < < " -5- 退出系统\n ";
cout < < "请选择输入: ";
cin > > choice;
switch(choice)
{
case 1:
addReader();
break;
case 2:
modifyReader();
break;
case 3:
deleteReader();
break;
case 4:
queryReader();
break;
case 5:
return;
}
}
}
void addReader()
{
char isSave = 'y ';
char isContinue = 'y ';
do
{
readerInfo read;
cout < < "正在添加第 " < < count < < "位读者\n读者呢称: ";
cin > > read.ID;
cout < < "读者真实姓名: ";
cin > > read.name;
cout < < "读者性别(male\\female): ";
cin > > read.gender;
//if(!read.getGender( read.gender))
//cout < < "输入有误! ";
read.isValled = true;
cout < < "读者年龄(0-100): ";
cin > > read.age;
//if(!read.getAge(read.age))
//cout < < "输入有误! ";
cout < < "读者城市: ";
cin > > read.city;
cout < < "是否保存?(y/n) ";
cin > > isSave;
if (( 'y ' == isSave) || ( 'Y ' == isSave))
reader[count++] = read;
cout < < "是否继续?(y/n) ";
cin > > isContinue;
}while(( 'y ' == isContinue) || ( 'Y ' == isContinue));
}
void queryReader()
{
int choice = 0;
cout < < "--------------------------------------------\n ";
cout < < " -1- 查询现有读者\n ";
cout < < " -2- 查询已删除读者\n ";
cout < < "--------------------------------------------\n ";
cin > > choice;
if (choice == 1)
{
for (int i = 0; i < count; i++)
{
if (reader[i].isValled)
{
cout < < reader[i].ID < < "\t\t ";
cout < < reader[i].name < < "\t\t ";
cout < < reader[i].gender < < "\t\t ";
cout < < reader[i].age < < "\t\t ";
cout < < reader[i].city < < "\t\t ";
}
}
}
else
{
for (int i = 0; i < count; i++)
{
if (!reader[i].isValled)
{
cout < < reader[i].ID < < "\t\t ";
cout < < reader[i].name < < "\t\t ";
cout < < reader[i].gender < < "\t\t ";
cout < < reader[i].age < < "\t\t ";
cout < < reader[i].city < < "\t\t ";
}
}
}
}
void deleteReader()
{
int i = 0;
readerInfo read;
char isDelete = 'y ';
cout < < "------------------------ ";
cout < < "请输入要删除的读者昵称: ";
cout < < "-------------------------------- ";
cin > > read.ID;
for (i = 0; i < count; i++)
{
if (! strcmp(reader[i].ID,read.ID))
{
cout < < reader[i].ID < < "\t\t ";
cout < < reader[i].name < < "\t\t ";
cout < < reader[i].gender < < "\t\t ";
cout < < reader[i].age < < "\t\t ";
cout < < reader[i].city < < "\t\t ";
break;
}
}
cout < < "(y/n) ";
cin > > isDelete;
if (( 'y ' == isDelete) || ( 'Y ' == isDelete))
{
reader[i].isValled =false;
cout < < "删除成功! ";
}
}
void modifyReader()
{
int i = 0;
readerInfo tmp;
char isSave = 'y ';
cout < < "------------------------ ";
cout < < "请输入要修改信息的读者昵称: ";
cout < < "---------------------------\n ";
cin > > tmp.ID;
for (i = 0; i < count; i++)
{
if (! strcmp(reader[i].ID,tmp.ID))
{
cout < < "要修改信息的读者信息如下: " < <endl;
cout < < reader[i].ID < < "\t\t ";
cout < < reader[i].name < < "\t\t ";
cout < < reader[i].gender < < "\t\t ";
cout < < reader[i].age < < "\t\t ";
cout < < reader[i].city < < "\t\t ";
break;
}
}
cout < < "正在修改第 " < < i < < "位读者呢称为: " < <reader[i].ID < < "的信息\n ";
cout < < "请输入读者呢称: ";
cin> > tmp.ID;
cout < < "读者真实姓名: ";
cin> > tmp.name;
cout < < "读者性别(male\\female): ";
cin> > tmp.gender;
tmp.isValled = reader[i].isValled;
cout < < "读者年龄(0-100): ";
cin> > tmp.age;
cout < < "读者城市: ";
cin> > tmp.city;
cout < < "是否保存?(y/n) ";
cin> > isSave;
if(( 'y '==isSave)||( 'Y '==isSave))
reader[i] = tmp;
cout < < "修改成功! ";
}
[解决办法]
bool strformat(char *str)
{
if (strcmp(str, "male ") == 0 || strcmp(str, "female ") == 0 )
return true;
printf( "请输入性别\n ");
return false;
}
bool int format(int n)
{
if (n> 0 && n <100)
return true;
printf( "请输入合法年龄\n ");
return false;
}
在每次操作前调用前面俩个函数进行测试下数据的规范性