功能不停止

function not stopping

本文关键字:不停止 功能      更新时间:2023-10-16

我正在创建一个程序来输入要求itemName,size,amount,and hour+minute的产品

到目前为止一切看起来都不错,但是当我编译并选择菜单"1"时,它通过推送输入的数据来添加项目,即使我还没有调用它并且 cout"--- Add New Order Success ---";没有出现,它也运行了del()函数

另外,当我选择菜单"2"时,它成功打印"no data available"但在按"enter"键后没有返回mainMenu

我有点卡住了,因为我不知道要解决什么,在论坛中搜索了问题列表,但没有用c ++编码 另外,我的代码没有显示任何让我感到困惑的错误

随意指出我做错的任何事情,并且我在C中使用带有push pop方法的双链表,因为我不太了解使用将数据封装在类中的向量。

非常感谢您的帮助,提前感谢您:)。

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#define w setw(2)<<flag<<"|"
#define f flag++
#define s cin.sync();cin.clear()
static int flag;
class receiveDate{
public:
int hour;
int minute;
};
class item:public receiveDate{
public:
int jumlah;
char namaProduk[20];
int ukuran;
class item *prev,*next;
};
class item *head=NULL,*curr,*tail;
void clear(){
int i;
for(i=0;i<100;i++){
cout<<endl;
}
}
void header(){
clear();
int a = 1;
cout<<"           ";
for(int i=0;i<=90;i++){
int b = i%10;
if(b==0){
cout<<a;
a++;
}else{
cout<<" ";
}
}
cout<<endl<<"  ";
for(int i = 0;i<=9;i++){
printf("1234567890");
}
cout<<endl<<"  ";
for(int i=0;i<=100;i++){
printf("=");
}
cout<<endl;
}
void pop(){
class item *temp;
if(head!=NULL){
if(curr == head)
{
head = head->next;
free(curr);
if(head != NULL)
{
head->prev = NULL;
}
}
else if(curr == tail)
{
curr = tail;
tail = tail->prev;
free(curr);
tail->next = NULL;
}
else
{
temp = head;
while(temp->next != curr)
{
temp = temp->next;
}
curr->next->prev = curr->prev;
temp->next = curr->next;
free(curr);
}
}
}
void popall()
{
while(head!=NULL)
{
curr=head;
head=head->next;
free(curr);
}
}
void display(){
header();
int i=0;
curr=head;
cout<<w<<setw(22)<<"Item Name"<<setw(8)<<"Ukuran"<<setw(4)<<"Jam Pesan"<<endl;f;
while(curr){
i++;
cout<< w << setw(2) << i <<setw(20) << curr->namaProduk << setw(8)<<curr->ukuran <<setw(4) << curr->hour << ":" << curr->minute;f;
curr=curr->next;
}
}
void push(char namaProduk[],int ukuran,int jumlah,int hour,int minute){
curr=(class item*)malloc(sizeof(class item));
class item *temp;
strcpy(curr->namaProduk,namaProduk);
curr->jumlah=jumlah;
curr->ukuran=ukuran;
curr->hour=hour;
curr->minute=minute;
if(head==NULL){
head=tail=curr;
}else{
curr->next=head;
head->prev=curr;
head=curr;
}
head->prev=NULL;
tail->next=NULL;
}
void add(){
header();
flag=1;
char namaProduk[20];
int ukuran;
int jumlah;
int hour;
int minute;
char jenis[10];
do{
cout<<w<<"Input nama produk [3-30] : ";f;
cin>>namaProduk;s;
}while(strlen(namaProduk)<3 || strlen(namaProduk)>30);
do{
cout<<w<<"Input ukuran [1 = kecil, 2 = sedang, 3 = besar] : ";f;
cin>>ukuran;s;
}while(ukuran<1||ukuran>3);
do{
cout<<w<<"Jumlah item [1 - 999] : ";f;
cin>>jumlah;s;
}while(jumlah<1||jumlah>999);
do{
cout<<w<<"Jam pesan :";f;
cin>>hour;s;
}while(hour<0||hour>24);
do{
cout<<w<<"Menit pesan:";f;
cin>>minute;s;
}while(minute<0||minute>59);
push(namaProduk,ukuran,jumlah,hour,minute);
cout<<w<<"--- Add New Order Success ---";
cin.get();
}
void del(){
int tot = 0;
int pos;
int qty;
int i;
if(head==NULL)
{
cout<<"--- There is No Order in The List ---";
}
else
{
curr = head;
while(curr)
{
tot++;
curr = curr->next;
}
display();
do
{
cout<<" Input Number of The Order [1.."<< tot<<"]";
cin>>pos;s;
}while(pos<1 || pos>tot);
curr = head;
for(i=1;i<pos;i++)
{
curr = curr->next;
}
pop();
cout<<endl<<" --- Take Order Success ---";
cin.get();
}
}
void menu(){
int menu;
do{
header();
flag=1;
cout<<w<<endl;f;
cout<<w<<endl;f;
cout<<w<<endl;f;
cout<<w<<setw(57)<<"PROGRAM INVENTARIS MINIMARKET"<<endl;f;
cout<<w<<setw(58)<<"=============================="<<endl;f;
cout<<w<<"     Pilih Menu:"<<endl;f;
cout<<w<<"      1. Add Item"<<endl;f;
cout<<w<<"      2. View Inventory"<<endl;f;
cout<<w<<"      3. Remove order"<<endl;f;
cout<<w<<"      4. Remove all order"<<endl;f;
cout<<w<<"      5. Exit Program"<<endl;f;
cout<<w<<"      Input Menu: ";f;
cin>>menu;s;
cin.sync();cin.clear();
if (menu =1 ){
add();
}else if(menu=2){
display();
}else if(menu=3){
del();
}else if(menu=4){
popall();
}else{
break;
}
}while(menu<1||menu>5);
}
int main(){   
menu();
return 0;
}

您的问题来自对switch/case的不当使用。请参阅交换机/机箱文档。您的菜单switch/case将触发每个案例

switch(1) {
case 1 : cout << '1'; // prints "1",
case 2 : cout << '2'; // then prints "2"
}

switch(1) {
case 1 : cout << '1'; // prints "1"
break;       // and exits the switch
case 2 : cout << '2';
break;
}

你的代码应该看起来像这样:

switch(menu){
case 1:
add();
break;
case 2:
display();
break;
case 3:
del();
break;
case 4:
popall();
break;
}

使用此代码,您无需在switch末尾while。你的 while 循环是无限的,因为如果你键入1,它会一次又一次地触发add()菜单while(menu!=5);因为除非menu=5