所以我想制作可以多次输入数据的程序,例如我在使用case 1输入数据后再次选择case 1。或者我多次选择案例2来查看我想要的所有数据。
主要是说break语句带来了解决方案。但是,当我在案件后使用break语句时,程序终止了。那么如何在不制作终止程序的情况下重复切换语句呢?
如果我这样做,开关案例会顺序发生。例如,我可以正常输入案例1,但是当我再次输入案例1时,它将转到下一个案例而不是案例1,反之亦然直到退出案例。这是我的源代码:
#include <iostream>
#pragma hdrstop
using namespace std;
struct ig
{
int id;
string name;
ig *next;
ig *prev;
};
ig *head= NULL;
ig *tail= NULL;
//---------------------------------------------------------------------------
int main(int argc, char** argv)
{
cout<<"+---------------------------------------+"<<endl;
cout<<"| Instagram User Data |"<<endl;
cout<<"+---------------------------------------+"<<endl;
cout<<"|1. Add Instagram ID |"<<endl;
cout<<"|2. Look All Data |"<<endl;
cout<<"|3. Look Previous Data |"<<endl;
cout<<"|4. Look Next Data |"<<endl;
cout<<"|5. Exit |"<<endl;
cout<<"+---------------------------------------+"<<endl;
int choose,l;
cout<<"your choices (1/2/3/4/5): ";cin>>choose;
switch(choose)
{
case 1:
{
cout<<"Input limit : ";cin>>l;
for(int a=1;a<=l;a++)
{
ig *new= new ig;
new->next=NULL;
new->prev=NULL;
cout<<"Entry Number : ";cin>>new->id;
cout<<"Instagram ID : ";cin>>new->name;
cout<<"===================="<<endl;
if(head== NULL)
{
head=new;
tail=new;
}
else
{
new->prev=tail;
tail->next=new;
tail=new;
}
}
}
cout<<"your choices (1/2/3/4/5)";cin>>choose;
case 2:
{
while(head!= NULL)
{
cout<<head->id<<endl;
cout<<head->name<<endl;
head=head->next;
}
}
cout<<"your choices (1/2/3/4/5)";cin>>choose;
case 3:
{
if(tail=NULL)
{
ig *show;
show=head;
while(show!= NULL)
{
cout<<head->id<<endl;
cout<<head->name<<endl;
head=head->prev;
}
}
else
{
cout<<"Data belum terisi"<<endl;
}
}
cout<<"your choices (1/2/3/4/5)";cin>>choose;
case 4:
{
ig *show;
show=tail;
while(show!= NULL)
{
cout<<tail->id<<endl;
cout<<tail->name<<endl;
tail=tail->next;
}
}
cout<<"your choices (1/2/3/4/5)";cin>>choose;
case 5:
{
return 0;
}
}
}
我试图多次学习语句和解决方案的逻辑,但仍然不成功。所以我需要帮助,谢谢
答案 0 :(得分:2)
在while
附近放置一个switch
:
while (true) {
int choose = askChoice();
switch (choose)
{
case 1: AddInstagramID(); break;
case 2: LookAllData(); break;
case 3: LookPreviousData(); break;
case 4: LookNextData(); break;
case 5: return 0;
}
}