C++函数运行,即使它不应该运行?
C++ function runs even if it shouldn't?
我是C++新手,但我对它有很好的处理。我正在尝试列出姓名和等级并找到最大的。一切都在工作,除了我的函数在每次我检查最高等级时运行。
//Compares to find highest grade
void highestTest(studentType info[]){
int testValue, i;
testValue = 0;
i = 0;
for(i; i < 20; i++){
if(info[testValue].testScore < info[i].testScore){
testValue = i;
}
}
//Should run at the very end of the function
if(i == 20)
outPut(testValue, info);
}
void outPut(int highTest, studentType info[]){
cout << "The highest test score goes to " << info[highTest].studentFName << " " << info[highTest].studetnLName << " with a grade of " << info[highTest].testScore << endl;
cout << "~~~~~~~~~~~~~~~~~~Students~~~~~~~~~~~~~~~~~~~~~~~" << endl;
for(int i = 0; i < 20; i++){
cout << info[i].studetnLName << ", " << info[i].studentFName << info[i].grade << endl;
}
}
代码的其余部分如下,我知道这不是最好的方法,但我不允许在 main 中包含任何其他内容,并且我们对需要什么类型的函数有具体要求。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct studentType{
string studentFName;
string studetnLName;
int testScore;
char grade;
};
void fillArray(studentType info[]);
void labelGrades(studentType info[]);
void highestTest(studentType info[]);
void outPut(int highTest, studentType info[]);
string first[20] = {"student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student"};
string last[20] = {"student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student", "student"};
int grades[20] = {97, 98, 96, 99, 86, 94, 88, 84, 85, 86, 89, 100, 84, 97, 91, 82, 92, 83, 89, 95};
studentType info[20];
int main() {
fillArray(info);
return 0;
}
void fillArray(studentType info[]){
for(int i = 0; i < 20; i++){
info[i].studentFName = first[i];
info[i].studetnLName = last[i];
info[i].testScore = grades[i];
}
labelGrades(info);
}
void labelGrades(studentType info[]){
int i = 0;
for(i; i < 20; i++){
if(info[i].testScore >= 90 && info[i].testScore <= 100){
info[i].grade = 'A';
highestTest(info);
}else if(info[i].testScore >= 80 && info[i].testScore <= 89 )
info[i].grade = 'B';
else if(info[i].testScore >= 70 && info[i].testScore <= 79 )
info[i].grade = 'C';
else if(info[i].testScore >= 60 && info[i].testScore <= 69 )
info[i].grade = 'D';
else if(info[i].testScore <= 59)
info[i].grade = 'F';
else
cout << "An error occured please let the developer know" << endl;
}
}
我确实将所有名字(名字/姓氏)都改为学生,以免透露同学。如果有人知道为什么每次出现"A"时都会运行 void output 以及任何可能的修复程序,那就太好了!
退出 for 循环for(i; i < 20; i++){
时,i
的值将为 i == 20。 因此,测试if (i == 20)
将始终通过,并且每次都会调用outPut()
。
当
i = 20 时,这 for 循环结束
for(i; i < 20; i++){
if(info[testValue].testScore < info[i].testScore){
testValue = i;
}
}
然后你的下一行是一个测试 i == 20 的 if 语句,所以每次你调用这个函数时,它都会调用 outPut。
相关文章:
- 条件断点在不应该触发时触发
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 为什么我不应该把所有东西都放在标题中?
- 找不到 QRegularExpression 行为的任何解释。它有效,但不应该
- 在清除 istream 之前,我不应该需要取消获取它吗?
- c++ 为什么我不应该从不同的线程解锁互斥锁
- "typename"不应该只在模板函数或模板类中使用吗?
- GDB:运行不带符号的 cpp 进程调试
- 为不应该获得未定义行为的内容获取未定义的行为
- 两种情况下的输出不应该相同吗?
- 默认情况下,"std::shared_ptr"不应该使用"std::d efault_delete"吗?
- 错误代码 E0065 和 E0169 不应该有
- 矢量的值在不应该更改时更改
- 保证复制省略不应该适用吗?
- 不应该禁止访问私有类型吗?
- 使用匿名命名空间中的函数或另一个文件中的静态函数不应该出错吗?
- NRVO不应该保证局部命名变量和调用站点变量采用相同的地址吗?
- 为什么我的程序在运行时输出"0",即使它不应该也输出?
- C++函数运行,即使它不应该运行?
- overflow_error在运行时捕获 C++ 当整数变量无法保存它不应该保存的值时