这段代码有什么问题吗? 我觉得它的方式,我认为切换案件需要多个国家的括号,并且由于倒下风格我已经包括休息。程序意外退出
我能做些什么有这个循环正常(对不起增加更多的填充物,因为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