为什么我的代码没有 运行 switch 语句? (链表)

Why does my code do not run the switch statement? (Linked List)

我是初学者,仍在学习如何使用 switch 语句。我的教授要求我们使用 switch 语句制作一个链表。我真的不知道为什么 switch 语句没有按预期工作。希望有人能帮助我。

输出

程序的输出应该是这样的

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 没有 return 任何值。

您可能想要这样做:

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);

写函数时不要忘记return语句

重要提示: 不要对 variables/functions/classes 等使用不明确的名称。例如,ahch 的真正含义是什么?阅读您的神秘代码的人应该如何知道这一点?始终尝试使用 描述性 名称。