程序意外退出

时间:2015-10-02 00:45:14

标签: c++

这段代码有什么问题吗? 我认为它的方式,我认为开关盒需要带有多个状态的括号,并且由于跌落的风格而包含了休息。

我可以做些什么才能正确使用此循环(抱歉添加了更多填充因为SO赢了并且让我发帖而没有更多文字)

#include "Link.h"
#include "Node.h"
#include "Student.h"
#include <iostream>
using namespace std;

void displayMenu();
int confirmationDelete(int);
int getDeleteID();

int main(){
    LinkedList* theList = new LinkedList;
    int a = -1;
    do {
    displayMenu();
    cin >> a;

        switch(a){
            case 1:
                // Insert
                theList->insertNode();
                break;
            case 2:
                // Modify
                // Search student by ID
                // Display information
                // ONLY Name, Status, GPA can be modified
                // Confirmation on final update
                {
                if (theList->head){
                theList->modify();
                } else {
                cerr << "Empty list; nothing to modify, sorry!" << endl;
                }
                }
                break;
            case 3:
                {
                theList->printList();
                }
                break;
            case 4:
                // Retrieve
                break;
            case 5:
                // Delete
                {
                    if (theList->head){
                        int iD = getDeleteID(); // get iD
                        int retVal = confirmationDelete(iD); // can we delete
                        if (retVal > 0) // if > 0, true
                        cout << "Hi, yes" << endl;
                        theList->deleteNode(theList->head,iD); // delete
                    } else {
                    cerr << "Empty list; nothing to delete, sorry!" << endl;
                    }
                }
                break;
            case 6: return 0;
                    break;
            default:
                displayMenu();
            }
    } while (a != 5);

return 0;
}

int confirmationDelete(int ID){
    bool valid = false;
    char a;
    do {
    cout << "Are you sure you wish to delete node with ID " << ID << "?\n";
    cin >> a;
    if (!(a == 'y' || a == 'n')){
        cerr << "Invalid input" << endl;
    }
    else {
        valid = true;
    }
    } while (!valid);
if (a == 'y') return 1;
else return -1;
}

int getDeleteID(){
int temp = -1;
cout << "ID to delete?" << endl;
cin >> temp;
return temp;
}

void displayMenu(){
cout << "1. Insert a node" << endl << "2. Modify a node's record" << endl << "3. Print the list" << endl << "4. Retrieve a record" << endl << "5. Delete a node" << endl << "6. Exit the program" << endl;
}

由于似乎有点混乱,我会尝试澄清。 当我插入break语句并且我不知道原因时它会退出。

e.g。

输出:

 1. Insert a node
    2. Modify a node's record
    3. Print the list
    4. Retrieve a record
    5. Delete a node
    6. Exit the program
    5
    Empty list; nothing to delete, sorry!
    Press any key to continue . . .

1 个答案:

答案 0 :(得分:3)

你有:

while (a != 5);

结束do-while循环。循环将在a == 5时结束。这与提示不符。您需要将其更改为:

while (a != 6);

<强> PS

为了避免这样的错误,使用代表行动的令牌总是好的。

enum Choices {E_INSERT = 1, E_MODIFY, E_PRINT, E_RETRIEVE, E_DELETE, E_EXIT};

然后,使用:

do
{
    switch (a)
    {
       case E_INSERT:

       ...

       case E_EXIT:
    }
} while ( a != E_EXIT );