2015-10-02 88 views
-1

這段代碼有什麼問題嗎? 我覺得它的方式,我認爲切換案件需要多個國家的括號,並且由於倒下風格我已經包括休息。程序意外退出

我能做些什麼有這個循環正常(對不起增加更多的填充物,因爲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語句時它退出,我不知道爲什麼。

例如

輸出:

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

也許一個調試器會幫助你? – user2182349

+1

你應該修復你的縮進。閱讀零件很困難。 – Carcigenicate

+0

而我沒有看到任何循環。請嘗試在主開關周圍小心添加一個? – Carcigenicate

回答

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

@R Sahu就是這樣。我現在覺得很愚蠢。這是一個錯字。 – aaaa

+0

@R Sahu我之前使用過Enum,但從未想過要在開關盒中使用它。嗯,我會考慮這一點 – aaaa