gpt4 book ai didi

c++ - 即使满足并输入退出条件,仍然打印错误消息

转载 作者:行者123 更新时间:2023-11-30 17:21:07 25 4
gpt4 key购买 nike

我在尝试找出为什么这些代码段打印错误消息时遇到了很大的问题,即使我已经在应该返回 true 的 block 中执行了 cout 语句/不打印该错误消息。有任何想法吗?我是新来的,所以如果不允许这样做,请告诉我。谢谢!

函数的使用:

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 语句,并且无论检查如何,后面的代码都将运行。

关于c++ - 即使满足并输入退出条件,仍然打印错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28387008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com