2015-10-02 20 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