function ran even though it's not called

Hello everyone,im creating a program to input product that asks for itemName,size,amount,and hour+minute
everything so far looks good, but when i compile and chose menu "1" which is adding item by pushing the data inputted, it ran del() function even though i havent called it yet and cout<<w<<"--- Add New Order Success ---"; didnt show up

also, when i chose menu "2", it successfully printed "no data available" but didnt return to mainMenu after pressing "enter" key

feel free to point anything im doing wrong ,and i'm using doubly linked list with push pop method in C because i dont quite understand using vector which the data are encapsulated in classes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#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;
    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();
    switch(menu){
    case 1:
        add();
    case 2:
        display();
    case 3:
        del();
    case 4:
        popall();
    }while(menu!=5);
}
int main()
{   
    menu();
    return 0;
}
Last edited on
im kinda stuck because i don't know what to fix
it doesnt show any error
Switch statements are a bit primitive. If you don't use a break statement it will just go on executing the code in the other cases that comes after.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (menu) {
case 1:
	add();
	break;
case 2:
	display();
	break;
case 3:
	del();
	break;
case 4:
	popall();
	break;
}
Last edited on
Topic archived. No new replies allowed.