這段代碼有什麼問題嗎? 我覺得它的方式,我認爲切換案件需要多個國家的括號,並且由於倒下風格我已經包括休息。程序意外退出
我能做些什麼有這個循環正常(對不起增加更多的填充物,因爲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 . . .
也許一個調試器會幫助你? – user2182349
你應該修復你的縮進。閱讀零件很困難。 – Carcigenicate
而我沒有看到任何循環。請嘗試在主開關周圍小心添加一個? – Carcigenicate