//添加功能 void Create_LinkList(LinkList& L) { system("cls"); cout << "\t\t\t添加联系人功能" << endl; Node* t = L; int i = 1, flag = 1; while(t->next) { i++; t = t->next; } while (flag) { Node* p = new Node; cout<< "\t\t\t输入姓名:"; cin>> p->data.name; cout<< "\t\t\t输入性别:"; cin>> p->data.sex; cout<< "\t\t\t输入年龄:"; cin>>p->data.age; cout<< "\t\t\t输入电话:"; int m=1; while(m) { cin>>p->data.telephone; int n=p->data.telephone.length(); if(n!=11) { cout<< "\t\t\t输入格式错误,请重新输入:"; m=1; } else { for(int j=0;j<n;j++) { if(!(p->data.telephone[j]>='0' && p->data.telephone[j]<='9')) { cout<< "\t\t\t输入格式错误,请重新输入:"; m=1; break; } } m=0; } } cout << "\t\t\t输入地址:"; cin >> p->data.address; cout << "\t\t\t输入分组:"; cin >> p->data.relation; p->data.num = i++; p->next = NULL; t->next = p; t = t->next; Save(L); cout << "\t\t\t成功添加一次,选择是否继续本功能(1 是 0 否)" << endl; cout << "\t\t\t请选择【0-1】:"; cin >> flag; } }修改一下
时间: 2024-03-29 09:39:52 浏览: 54
void Create_LinkList(LinkList& L) {
system("cls");
cout << "\t\t\t添加联系人功能" << endl;
Node* t = L;
int i = 1, flag = 1;
while (t->next) {
i++;
t = t->next;
}
while (flag) {
Node* p = new Node;
cout << "\t\t\t输入姓名:";
cin >> p->data.name;
cout << "\t\t\t输入性别:";
cin >> p->data.sex;
cout << "\t\t\t输入年龄:";
cin >> p->data.age;
cout << "\t\t\t输入电话:";
int m = 1;
while (m) {
cin >> p->data.telephone;
int n = p->data.telephone.length();
if (n != 11) {
cout << "\t\t\t输入格式错误,请重新输入:";
m = 1;
}
else {
for (int j = 0; j < n; j++) {
if (!(p->data.telephone[j] >= '0' && p->data.telephone[j] <= '9')) {
cout << "\t\t\t输入格式错误,请重新输入:";
m = 1;
break;
}
}
m = 0;
}
}
cout << "\t\t\t输入地址:";
cin >> p->data.address;
cout << "\t\t\t输入分组:";
cin >> p->data.relation;
p->data.num = i++;
p->next = NULL;
t->next = p;
t = t->next;
Save(L);
cout << "\t\t\t成功添加一次,选择是否继续本功能(1 是 0 否)" << endl;
while (1) {
cout << "\t\t\t请选择【0-1】:";
cin >> flag;
if (flag == 0 || flag == 1) break;
else cout << "\t\t\t输入错误,请重新输入!" << endl;
}
}
}
修改后的代码加入了一个循环,可以防止用户输入非0和1的值。同时,将flag的赋值放到循环内部,可以保证每次循环结束时flag都被赋值为0或1,避免出现不确定的情况。
阅读全文