[英]Why does my code do not run the switch statement? (Linked List)
我是初學者,仍在學習如何使用 switch 語句。 我的教授要求我們使用 switch 語句制作一個鏈表。 我真的不知道為什么 switch 語句沒有按預期工作。 希望有人可以幫助我。
程序的output應該是這個樣子
Menu
[1] Inserting value in the link
[2] Deleting value in the link
[3] Exit
Enter your choice: (Input)
Inserting/Deleting a value at
[a] Beginning
[b] Middle
[c] End
[d] Exit
Enter your choice: (Input)
Input the value to be inserted/deleted: (Input)
The values in the link are: (Output)
這是我正在使用的代碼
#include <iostream>
#include <malloc.h>
#include <conio.h>
#define getch() _getch()
using namespace std;
struct dlinklist
{
struct dlinklist* left;
int data;
struct dlinklist* right;
};
typedef struct dlinklist node;
node* start = NULL;
node* getnode()
{
node* newnode;
newnode = (node*)malloc(sizeof(node));
cout << "\n Input the value to be inserted: ";
cin >> newnode->data;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
int menu()
{
char ah;
int ch;
cout << "\n----------------------------------";
cout << "\nMenu";
cout << "\n----------------------------------";
cout << "\n[1] Inserting value in the link";
cout << "\n[2] Deleting value in the link";
cout << "\n[3] Exit";
cout << "\n----------------------------------";
cout << "\n\n Enter your choice: ";
cin >> ch;
if (ch == 1)
{
cout << "\n----------------------------------";
cout << "\n Inserting a value at:";
cout << "\n[a] The Beginning of the list";
cout << "\n[b] The middle of the list";
cout << "\n[c] The end of the list";
cout << "\n[d] Exit";
cout << "\n----------------------------------";
cout << "\n\n Enter your choice: ";
cin >> ah;
}
else if (ch == 2)
{
cout << "\n----------------------------------";
cout << "\n Deleting a value at:";
cout << "\n[a] The Beginning of the list";
cout << "\n[b] The middle of the list";
cout << "\n[c] The end of the list";
cout << "\n[d] Exit";
cout << "\n----------------------------------";
cout << "\n\n Enter your choice: ";
cin >> ah;
}
else if (ch == 3)
{
exit(0);
}
}
void insert_beg()
{
//code
}
void insert_end()
{
//code
}
void insert_mid()
{
//code
}
void delete_beg()
{
//code
}
void delete_end()
{
//code
}
void delete_mid()
{
//code
}
void main(void)
{
int ch = menu();
char ah;
while(1)
{
ah = menu();
switch(ah)
{
case 'A': case 'a':
if (ch == 1)
{
insert_beg();
break;
}
else
{
delete_beg();
break;
}
case 'B': case 'b':
if (ch == 1)
{
insert_mid();
break;
}
else
{
delete_mid();
break;
}
case 'C': case 'c':
if (ch == 1)
{
insert_end();
break;
}
else
{
delete_end();
break;
}
case 'D': case 'd':
exit(0);
}
}
return;
}
menu
不返回任何值。
你可能想要這樣做:
int menu()
{
char ah;
int ch;
cout << "\n----------------------------------";
cout << "\nMenu";
cout << "\n----------------------------------";
cout << "\n[1] Inserting value in the link";
cout << "\n[2] Deleting value in the link";
cout << "\n[3] Exit";
cout << "\n----------------------------------";
cout << "\n\n Enter your choice: ";
cin >> ch;
if (ch == 1)
{
cout << "\n----------------------------------";
cout << "\n Inserting a value at:";
cout << "\n[a] The Beginning of the list";
cout << "\n[b] The middle of the list";
cout << "\n[c] The end of the list";
cout << "\n[d] Exit";
cout << "\n----------------------------------";
cout << "\n\n Enter your choice: ";
cin >> ah;
return ah; // return statement
}
else if (ch == 2)
{
cout << "\n----------------------------------";
cout << "\n Deleting a value at:";
cout << "\n[a] The Beginning of the list";
cout << "\n[b] The middle of the list";
cout << "\n[c] The end of the list";
cout << "\n[d] Exit";
cout << "\n----------------------------------";
cout << "\n\n Enter your choice: ";
cin >> ah;
return ah; // return statement
}
else if (ch == 3)
{
// exit(0); don't write this here, call the exit inside main()
return 3; // 3 or anything, you can return other values if you want
}
}
main
是:
int ch = menu();
if (ch == 3) exit(0);
編寫函數時不要忘記返回語句。
重要提示:不要對變量/函數/類等使用模棱兩可的名稱。例如, ah
或ch
的真正含義是什么? 閱讀您的神秘代碼的人應該如何知道這一點? 始終嘗試使用描述性名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.