为什么最后按0退出时不能关闭界面
#include <iostream>#include <string>
#include<windows.h>
using namespace std;
#define MAX 1000
struct Person{
string name;
int price;
string number;
};
struct Arrayelectric {
struct Person electric;
int length;
};
void mean();
int outSystem();
void addelectric(Arrayelectric * abs);
void dalateelectric(Arrayelectric * abs);
int isExist(Arrayelectric *abs , string name);
void Checkelectric(Arrayelectric * abs);
void cleanelectric(Arrayelectric * abs);
void showelectric(Arrayelectric*abs);
void changeelectric(Arrayelectric * abs);
int main(){
int x;
Arrayelectric abs;
abs.length = 0;
while(true) {
mean();
cin >> x;
switch (x) {
case 1:
addelectric(&abs);
break;
case 2:
dalateelectric(&abs);
break;
case 3:
Checkelectric(&abs);
break;
case 4:
cleanelectric(&abs);
break;
case 5:
changeelectric(&abs);
break;
case 6:
showelectric(&abs);
break;
case 0:
outSystem();
break;
}
}
return 0;
}
void addelectric(Arrayelectric *abs){
if(abs->length >= MAX){
cout << "车辆已满" << endl;
}else {
string name;
cout << "请输入用户的名字: " << endl;
cin >> name;
abs->electric.name = name;
int price;
cout << "请输入车辆的价格: " << endl;
cin >> price;
abs->electric.price = price;
string num;
cout << "请输入车的编号: " << endl;
cin >> num;
abs->electric.number = num;
abs->length++;
cout << "添加成功" << endl;
system("cls");
}
}
int isExist(Arrayelectric *abs , string name){
for (int i = 0; i < abs->length; ++i) {
if(abs->electric.name == name){
return i;
}
}
return -1;
}
void dalateelectric(Arrayelectric * abs){
cout << "请输入你要删除的用户" << endl;
string name;
cin >> name;
int ret = isExist(abs ,name);
if(ret != -1){
for(int i = 0;i < abs->length;i++){
abs->electric.name = abs->electric.name;
abs->length--;
}
cout << "删除成功" << endl;
}else{
cout << "查无此人" << endl;
}
}
void Checkelectric(Arrayelectric * abs){
cout << "输入你要查找的用户"<< endl;
string name;
cin >> name;
int ret = isExist(abs , name);
if(ret != -1){
cout << "用户名为:" << abs->electric.name << "\t";
cout << "车价格为:" << abs->electric.price << "\t";
cout << "车编号为:" << abs->electric.number << endl;
}
else{
cout << "查无此车" << endl;
}
}
void cleanelectric(Arrayelectric * abs){
abs->length = 0;
cout << "清理完成" << endl;
}
void changeelectric(Arrayelectric * abs){
cout << "输入你要修改的车辆信息" << endl;
string name;
cin >> name;
int ret = isExist(abs , name);
if(ret != -1){
cout << "请输入名字: " << endl;
string name;
cin >> name;
abs->electric.name = name;
cout << "请输入价格: " << endl;
int price;
cin >> price;
abs->electric.price = price;
cout << "请输入编号: " << endl;
string num;
cin >> num;
abs->electric.number = num;
}else{
cout << "查无此用户" << endl;
system("cls");
}
}
void showelectric(Arrayelectric*abs){
if(abs->length == 0){
cout << "无"<< endl;
}else{
for (int i = 0; i < abs->length; ++i) {
cout << "用户名字 : " <<abs->electric.name << "\t";
cout << "车价格 : " <<abs->electric.price << "\t";
cout << "车编号 : " << abs->electric.number <<endl;
}
}
}
int outSystem(){
cout << "欢迎下次使用" << endl;
return 0;
}
void mean(){
cout << "*************************" << endl;
cout << "*******1、加入用户信息********" << endl; //OK
cout << "*******2、删除用户信息*****" << endl;//OK
cout << "*******3、查找用户信息*****" << endl;//OK
cout << "*******4、清空用户信息*******" << endl;//OK
cout << "*******5、修改用户信息*******" << endl;
cout << "*******6、显示用户*******" << endl;//OK
cout << "*******0、退出系统*********" << endl; //OK
cout << "**************************" << endl;
}
int outSystem(){
cout << "欢迎下次使用" << endl;
eixt(0);/////试试
} while没有结束条件 本帖最后由 临时号 于 2022-6-4 23:04 编辑
在输入了0之后,你调用了outSystem()函数,但这个函数只是输出了一句话,随后就结束了,而main函数中却只是跳出了switch case,并没有跳出死循环,所以程序不会停止
我能明白你的意思,你的意思是在outSystem函数中将main函数给return 0;不然你也不回将outSystem函数设置为int类型的返回值,但是这样是不行的,你需要在main函数中return 0;
可尝试这样修改main函数
int main(){
int x;
Arrayelectric abs;
abs.length = 0;
while(true) {
mean();
cin >> x;
switch (x) {
case 1:
addelectric(&abs);
break;
case 2:
dalateelectric(&abs);
break;
case 3:
Checkelectric(&abs);
break;
case 4:
cleanelectric(&abs);
break;
case 5:
changeelectric(&abs);
break;
case 6:
showelectric(&abs);
break;
case 0:
outSystem();
return 0;// 将下面的return 0;替换掉break;
}
}
} 临时号 发表于 2022-6-4 23:03
在输入了0之后,你调用了outSystem()函数,但这个函数只是输出了一句话,随后就结束了,而main函数中却只是跳出 ...
我想用CLS来换界面该怎么用啊 giegie666 发表于 2022-6-9 18:40
我想用CLS来换界面该怎么用啊
system("cls");
不过记得加头文件
#include <stdlib.h> 临时号 发表于 2022-6-9 18:43
不过记得加头文件
这个完成后就立马换界面了,能不能等按了键盘之后再换界面 giegie666 发表于 2022-6-9 18:53
这个完成后就立马换界面了,能不能等按了键盘之后再换界面
可以加个if条件判断 临时号 发表于 2022-6-9 18:54
可以加个if条件判断
加在哪里啊 giegie666 发表于 2022-6-9 18:59
加在哪里啊
解决了 giegie666 发表于 2022-6-9 19:03
解决了
如果问题解决了,不要忘了设置最佳答案哦
页:
[1]