即使满足并进入退出条件,错误消息仍在打印

Error message still printing even though exit condition met and entered

我在试图弄清楚为什么这些代码段打印错误消息时遇到了很大的问题,即使我在块中完成了 cout 语句应该 return true / 不打印错误消息。有任何想法吗?我是新来的,所以如果不允许这样做,请告诉我。谢谢!

函数使用:

case 'a':
        {
            // Format: a ID credits GPA
            // Adds a student with the given student ID (ID), number of 
            // credits (credits), and overall GPA (GPA) to the database. 
            // If the student is already in the database, an error 
            // message should be printed indicating this.

            int credits = 0;
            double gpa = 0;
            cin >> studentID;
            cin >> credits;  
            cin >> gpa;

            // Adds the student and checks to see if the student was actually added
            // or if there was an existing student with the specified ID

            bool added = addStudent(studentID, credits, gpa);
            if(added == false);
            {
                cout << "Student already exists in database, nothing changed." << endl;
                // Still prints this when executed with valid 
            }
            break;
        }

将学生添加到数组的函数:

bool addStudent (int id, int numCredits, double gpa) {

// Check to see if student exists

if (nextEntry != 0)
{
    for (int x = 0; x < 7000; x++) {
        Student tmp = studentRecords[x];
        if (tmp.studentId == id)
        {
            return false;
            cout << "hey" << endl;
        }
    }
}

// If student does not exist, add to records database
if (nextEntry != 7000)
{
    studentRecords[nextEntry].studentId = id;
    studentRecords[nextEntry].numCredits = numCredits;
    studentRecords[nextEntry].gpa = gpa;
    nextEntry++;
    return true;
    // confirmed I can get here
}

return false;
}

您的 if (added==false) 语句后有一个额外的 ;。这将终止 if 语句,之后的代码将 运行 无论检查如何。