c++ - 菜单选项未显示正确的输出

标签 c++ menu output menuitem

我在使用我创建的链接列表类的菜单驱动程序时遇到问题。我创建了一个抽象类并成功地将它链接到这个菜单驱动程序。我怎么有问题呢。

首先,我使用选项 1 成功填充了链接列表,但是当我完成并尝试选择另一个菜单选项(例如显示我的列表)时,它没有显示任何内容,就好像它是空的。

有人能给我指出正确的方向吗,关于如何使用这个菜单驱动程序解决这个问题。

我对这个项目的意图

  1. 选择选项一来填充我的链表类

  2. 我想返回原来的菜单选择其他选项 例如:从我的列表中删除一个号码,或者只是显示我当前的列表

    // This program demonstrates the displayList member function.
    #include <iostream>
    #include "NumberList.h"
    using namespace std;
    
    int main()
    {
       double user_input = 0;
       int menu_choice = 0;
       int num = 0;
       NumberList list;// Define a NumberList object.
    
    
    
       //display for the user 
       cout << "                             Your Menu Options   \n\t\t\t\t  ";
    
       cout << "\n\n\n 1.   This Option Will Allow You To Add Numbers To The Link Class ";
    
       cout << "\n\n 2.   This Option Will Allow You To Insert A Number To The Middle Of The List ";
    
       cout << "\n\n 3.   This Option Will Allow You To Delete A Number From The List ";
    
       cout << "\n\n 4.   This Option Will Allow You To View The List Created ";
    
       cout << "\n\n                          Your Menu Choice Is : ";
       cin >> menu_choice;
    
       switch ( menu_choice)
       {
          case 1:
             // Get the number of days.
             cout << "\n\n                   HOW MANY NUMBERS WILL YOU LIKE TO ENTER : ";
             cin >> num;
    
             // Get the sales for each day and accumulate a total.
             for (int count = 1; count <= num; count++)
             {
                double list_number;
                cout << " Please Enter Number ( " << count << " ): ";
                cin >> list_number;// accept list number to ass to the list
                list.appendNode(list_number); // sends number to list class
             }
             main();
             break;
    
          case 2:
             // Insert a node in the middle of the list.
             list.insertNode(user_input);
             main(); // this is here for me to return to my menu
             break; // this will be a break for the class
    
          case 3:
             // Delete the middle node.
             list.deleteNode(user_input);
             main();
             break;
    
          case 4:
             // Display the values in the list.
             list.displayList();
             main();
             break;
    
          defualt:
             cout << " INVALID ENTRY PLEASE CHOOSE NUMBERS 1-4 ";// invalid menu option message
             break;
       }
    
       system("pause");
       return 0;
    }
    

最佳答案

当您递归调用 main 时,您正在执行 main 的另一个拷贝,它有自己的局部变量:特别是它自己的列表。

您应该将初始化(您只想完成一次)之后的 main 部分包装在一个循环中(您可能需要一个菜单​​选项来结束程序)。

关于c++ - 菜单选项未显示正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868786/

相关文章:

oracle - 如何显示存储过程的输出?

c++ - 需要帮助在循环/数组程序 C++ 中显示值

带有降雪的 R 并行计算 - 从不同的工作人员写入文件

c++ - 不使用 boost 的简单字符串解析

c++ - 推荐一个用于测试 'closedness' 几何图形的 '.STL' 的库?

c++ - 与默认构造函数 : clang produces parse error while gcc did not 成为 friend

c++ - 带轮分解的埃拉托色尼筛法

jquery - 我的 jquery 菜单不想正常工作

javascript - 如何在 ReactJS 中获取下拉菜单的选定值

android - 如何在 Activity 开始时显示选项菜单