C++程序函数的一些问题

C++ Some issues with functions for a program

本文关键字:问题 程序 函数 C++      更新时间:2023-10-16

所以我的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的第一个版本没有正确地以大括号}结束。

当我尝试自己编译您的代码时,我只发现了两个语法错误:

  1. 您已经在同一范围内声明了两次"maxscore"(在"printResult()"中)。删除未初始化的第一个声明。

  2. "highestScore"不返回任何内容。

修复这些问题后,您的代码应该进行编译,尽管"calculateGrade()"中的"score"没有初始化,因此它不会像您预期的那样运行。

注意生成的任何警告,大多数警告最终都会导致代码中的错误,因此修复它们将有助于最大限度地减少错误。