C ++无效转换int *到int

时间:2016-03-11 19:45:01

标签: c++

我正在进行一项任务,要求分别创建一些函数来读取数据,计算分数和输出数据。

我觉得我非常接近完成这项工作,但仍然遇到以下错误:

  

66 [错误]无效转换为int (*)(studentType*, int)int [-fpermissive]

     

30 [错误]初始化void printResults(std::ofstream&, studentType*, int, int) [-fpermissive]

的参数3

我搜索了stackoverflow和其他论坛,但无法完全了解它。有谁知道我的代码中可能会导致这些错误?

我也有一种潜在的怀疑,即我传递价值观的方式并不完全正确。我知道这是一个开放式的问题,但任何反馈都会受到赞赏。

以下是代码:

#include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>

    using namespace std;

    //sets limit for students
    const int max_students = 20;

    //student info struct
    struct studentType
    {
        string firstName;
        string lastName;
        int score;
        char grade;
    };

    //function declarations
    void dataInput(ifstream& inFile, studentType slist[], int listSize);
    void calcGrade(studentType sList[], int listSize);
    void printResults(ofstream& outFile, studentType sList[], int highScore, int listSize);
    int highScore(studentType sList[], int listSize);

    int main()
    {

        ifstream inFile;
        ofstream outFile;
        int listSize = max_students;
        studentType sList[max_students];

        //checks for input file
        inFile.open("scores.txt");

            if (!inFile)
            {
                cout << "No input file detected.. please try again." << endl;

                return 0;   
            }

        //checks for output file    
        outFile.open("results.txt");

            if (!outFile)
            {
                cout << "Cannot locate output file.. please try again" << endl;

                return 0;   
            }

        //calls functions
        dataInput(inFile, sList, listSize);

        calcGrade(sList, listSize);

        printResults(outFile, sList, highScore, listSize);

        return 0;

    }

    //function to collect names and scores from each student
    void dataInput(ifstream& inFile, studentType sList[], int listSize)
    {
        for(int i = 0; i < listSize; i++)
        {

            inFile >> sList[i].firstName >> sList[i].lastName >> sList[i].score;

        }

    }

    //calculates grades based on input scores
    void calcGrade(studentType sList[], int score, int listSize)
    {

        for(int i = 0; i < listSize; i++)
        {
            if (score >= 90)
                sList[i].score = 'A';
            else if (score >= 80)
                sList[i].score = 'B';
            else if (score >= 70)
                sList[i].score = 'C';
            else if (score >= 60)
                sList[i].score = 'D';
            else 
                sList[i].score = 'F';

        }

    }

    //calculates highest score(s)
    int highScore(studentType sList[], int listSize)
    {

        int i = 0;

        int highScore = sList[i].score;

        for(i; i < listSize; i++)
        {
            if(sList[i].score > highScore)
            {
                highScore = sList[i].score;
            }

        }


        return highScore;
    }

    //prints results into output file
    void printResults(ofstream& outFile, studentType sList[], int highScore, int listSize)
    {

        int topScore = highScore;
        int i = 0;

        //header
        outFile << setw(10) << "Name " << setw(10) << "Score " << setw(10) << "Grade " << endl;

        for (int i = 0; i < listSize; i++)
        {
            //content
            outFile << left << setw(25) << sList[i].lastName << ", " << sList[i].firstName <<
            right << " " << setw(5) << sList[i].score << setw(5) << " " << sList[i].grade << endl;

            outFile << " " << endl;

            outFile << "Highest test score: " << topScore << endl;

            outFile << "Students with the highest score: " << endl;

            for(int i = 0; i < listSize; i++)
            {
                if (sList[i].score == topScore)
                {
                    outFile << sList[i].lastName << ", " << sList[i].firstName << endl;
                }

            }

        }

    }

2 个答案:

答案 0 :(得分:1)

两个主要问题:

  1. 您定义了void calcGrade(studentType sList[], int listSize);但后来将其实施为void calcGrade(studentType sList[], int score, int listSize)

  2. 在main中你使用的是未定义的变量highScore,只定义了具有此类名称的函数,这就是为什么你得到奇怪的错误,指向函数的指针无法转换为int < / p>

答案 1 :(得分:0)

问题在于:

int main()
    ...
    printResults(outFile, sList, highScore, listSize);
    return 0;
}

您放置highScore的第三个参数,但它是您声明的函数!它应该是int个数字。