C++程序函数的一些问题
C++ Some issues with functions for a program
所以我的Void printresult函数有问题
我无法让它工作,因为我得到的编译器错误是outFile没有声明,并且最高分数"明显调用的括号前面的表达式必须具有(指针到-)函数类型。如有指示,不胜感激。
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int NO_OR_STUDENTS = 20;
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(ofstream& outFile, const studentType sList[], int listSize);
int main()
{
ifstream inData;
ofstream outData;
studentType studentList[NO_OR_STUDENTS];
inData.open("Ch9_Ex2Data.txt");
if(!inData)
{
cout << "The input file does not exist. Program terminates!"
<< endl;
system("PAUSE");
return 1;
}//endif
outData.open("Ch11_Ex1Out.txt");
if(!outData)
{
cout << "Cannot open the output file. Program terminates!"
<< endl;
system("PAUSE");
return 1;
}//endif
getData(inData, studentList, NO_OR_STUDENTS);
calculateGrade(studentList, NO_OR_STUDENTS);
printResult(outData, studentList, NO_OR_STUDENTS);
system("PAUSE");
return 0;
}//endmain
void getData(ifstream& inFile, studentType sList[], int listSize)
{
for(int i = 0; i < listSize; i++)
inFile >> sList[i].studentFName >> sList[i].studentLName
>> sList[i].testScore;
}//endgetData
void calculateGrade(studentType sList[], int listSize)
{
int score;
for(int i = 0; i < listSize; i++)
if(score >= 90)
sList[i].testScore = 'A';
else if(score >= 80)
sList[i].testScore = 'B';
else if(score >= 70)
sList[i].testScore = 'C';
else if(score >= 60)
sList[i].testScore = 'D';
else
sList[i].testScore = 'F';
//student writes code for this function
}//endcalculateGrade
int highestScore(const studentType sList[], int scores[], int listSize) //orignal parameters only had one array and an int
//must pass in the scores array otherwise the program has no idea what it is inside the function
{
int highestScore = scores[0];
for(int i = 0; i < listSize; i++)
{
if(scores[i] > highestScore)
{
highestScore = scores[i];
}
}
}
void printResult(ofstream& outFile, const studentType sList[], int listSize)
{
int maxScore;
int maxScore = highestScore(sList, listSize);
int i;
outFile << setw(15) << "Student Name "
<< setw(10) << "Test Score"
<< setw(7) << "Grade" << endl;
for(i = 1; i < listSize; i++)
outFile << left << setw(25)
<< sList[i].studentLName + ", " + sList[i].studentFName
<< right << " " << setw(5) << sList[i].testScore
<< setw(6) << " " << sList[i].grade << endl;
outFile << endl << "Highest Test Score: " << maxScore << endl;
outFile << "Students having the highest test score:" << endl;
for(i = 1; i < listSize; i++)
if(sList[i].testScore == maxScore)
outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
}//endprintResult
最有可能的罪魁祸首是这两个函数的函数定义末尾的分号。将void printResult(ofstream& outFile, const studentType sList[], int listSize);
更改为void printResult(ofstream& outFile, const studentType sList[], int listSize)
,并类似地更改为highestScore,看看这是否会有所改善。
编辑:还有两个问题(如注释中所示):您忘记了highestScore()
函数中for循环中的右大括号,之后需要return highScore;
。
此外,你已经声明了该函数的两个版本——我不确定这是复制粘贴错误,还是你的代码中确实有两个版本,但不管是什么,都要去掉其中一个!
highestScore
的第一个版本没有正确地以大括号}
结束。
当我尝试自己编译您的代码时,我只发现了两个语法错误:
-
您已经在同一范围内声明了两次"maxscore"(在"printResult()"中)。删除未初始化的第一个声明。
-
"highestScore"不返回任何内容。
修复这些问题后,您的代码应该进行编译,尽管"calculateGrade()"中的"score"没有初始化,因此它不会像您预期的那样运行。
注意生成的任何警告,大多数警告最终都会导致代码中的错误,因此修复它们将有助于最大限度地减少错误。
- 基于boost的程序的静态链接——zlib问题
- 使用mongocxx驱动程序时包含头文件问题
- 运行程序时出现问题
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 将应用程序从32位移植到64位时出现问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- C++ 每次运行程序时我都会"nan"输出的问题
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 如何解决在负数的情况下程序以相同方式工作的问题?
- 在树莓派上用libtorch构建程序时的链接问题
- 覆盖应用程序的低级别键盘挂钩问题
- 运行长度编码程序 c++ 的问题
- 编译 Cilk Plus 程序时遇到问题
- 我的堆栈和库存清单程序的结构有什么问题?
- 系统错误:程序无法启动,因为您的计算机中缺少MSVCP140D.DLL。尝试重新安装该程序以解决此问题
- wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
- 桌面计算器出现问题.C++程序设计语言
- 内存泄漏问题c++程序
- 写函数的基本问题.程序随机跳到最后