我在使用我创建的链接列表类的菜单驱动程序时遇到问题。我创建了一个抽象类并成功地将它链接到这个菜单驱动程序。我怎么有问题呢。
首先,我使用选项 1 成功填充了链接列表,但是当我完成并尝试选择另一个菜单选项(例如显示我的列表)时,它没有显示任何内容,就好像它是空的。
有人能给我指出正确的方向吗,关于如何使用这个菜单驱动程序解决这个问题。
我对这个项目的意图
选择选项一来填充我的链表类
我想返回原来的菜单选择其他选项 例如:从我的列表中删除一个号码,或者只是显示我当前的列表
// 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/