如何摆脱循环

时间:2017-02-26 18:29:20

标签: c++ while-loop

我创建了LinkedList,我试图让用户修改其中一个。到目前为止,while循环搜索一个按课程ID然后如果发现用户将被要求编辑。但问题是它要求用户两次编辑。

我尝试在if语句下使用“break”,甚至exit(0),但它不起作用。在if语句执行完毕后,它应该让我出去一次,这使我在while循环中保持两次。

template <typename T> node<T>* ModifyByCourseId(node<T> *front, string className, string courseId)
{
    node<T> *curr = front;
    while (curr != NULL)
    {
        if (curr->classId == courseId) {
            cout << curr->classId << " " << curr->classDescrip << "." << curr->credit << ":" << curr->creditHr 
                 << curr->Prerequiste << ":" << curr->PreReq << endl; // print the current node's value to the screen
            cout << "Please Modify course title." << endl;          
            getline(cin, curr->classDescrip);
            cout << "Please enter Credit hour(s)." << endl;
            cin >> curr->creditHr;
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            cout << "Please enter class pre-requisite(s) if there are more than one separate them by a space." << endl;
            getline(cin, curr->PreReq);
            break;
        }
        curr = curr->next;
    }
    return NULL;
}

1 个答案:

答案 0 :(得分:0)

我弄明白了这个问题。我在if语句

中调用了两次函数