for 语句判断条件问题
#include<iostream>using namespace std;
#include<string>
int const MAX =1000;
struct Person
{
string m_Name;
intm_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
struct Addressbooks
{
struct Person personArray;
int m_Size;
};
void showMenu()
{
cout << "******1.添加联系人******" << endl;
cout << "******2.显示联系人******" << endl;
cout << "******3.删除联系人******" << endl;
cout << "******4.查找联系人******" << endl;
cout << "******5.修改联系人******" << endl;
cout << "******6.清空联系人******" << endl;
cout << "******0.退出通讯录******" << endl;
}
void addPerson(struct Addressbooks* abs)
{
if (abs->m_Size == MAX)
{
cout << "通讯录已满,无法再输入" << endl;
system("pause");
system("cls");
}
else
{
cout << "请输入姓名" << endl;
string name;
cin >> name;
abs->personArray.m_Name = name;
cout << "请输入性别" << endl;
cout << "1----男" << endl;
cout << "2----女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray.m_Sex = sex;
break;
}
cout << "您输入的有误,请重新输入" << endl;
}
int age = 0;
cout << "请输入年龄" << endl;
while (true)
{
cin >> age;
if (age < 100 && age >0)
{
abs->personArray.m_Age = age;
break;
}
cout << "您输入的有误,请重新输入" << endl;
}
string phone;
cout << "请输入联系电话" << endl;
cin >> phone;
abs->personArray.m_Phone = phone;
string address;
cout << "请输入家庭住址" << endl;
cin >> address;
abs->personArray.m_Addr = address;
abs->m_Size++;
cout << "您输入成功" << endl;
system("pause");
system("cls");
}
}
void showPerson(Addressbooks *abs)
{
if (abs->m_Size==0)
{
cout << "当前记录人数为空" << endl;
system("pause");
system("cls");
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名:" << abs->personArray.m_Name << " \t";
cout << "性别:" << (abs->personArray.m_Sex==1?"男":"女") << " \t";
cout << "年龄:" << abs->personArray.m_Age << " \t";
cout << "电话" << abs->personArray.m_Phone<< " \t";
cout << "住址:" << abs->personArray.m_Addr<< " \t";
}
system("pause");
system("cls");
}
}
int isExist(Addressbooks *abs,string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArray.m_Name == name)
{
returni;
}
}
return -1;
}
int main()
{
int select = 0;
struct Addressbooks abs;
abs.m_Size = 0;
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
{
cout << "请输入删除联系人姓名" << endl;
string name;
cin >> name;
if (isExist(&abs, name) == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "找到此人" << endl;
}
}
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
其中在执行函数isExist中for条件语句判断(int i = 0; i < abs->m_Size; i++)在判断m_size下标为0的情况下为何能使i<0还能运行,还是我理解有错,有些不解
条件是i < abs->m_Size;只要abs->m_Size>=0;i值是小于0的数,i < abs->m_Size;都为真
页:
[1]