Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;将int*转换为int无效_C++ - Fatal编程技术网

C++ C++;将int*转换为int无效

C++ C++;将int*转换为int无效,c++,C++,我正在做一个作业,要求创建一些函数来分别读取数据、计算分数和输出数据 我觉得我很快就要完成了,但仍会遇到以下错误: 66[错误]从int(*)(studentType*,int)到int[-fppermissive]的转换无效 30[错误]初始化无效打印结果的参数3(std::of stream&,studentType*,int,int)[-fppermissive] 我在stackoverflow和其他论坛上搜索过,但还没有找到它的底部。有人知道我的代码中有什么可能导致这些错误吗 我还暗自怀

我正在做一个作业,要求创建一些函数来分别读取数据、计算分数和输出数据

我觉得我很快就要完成了,但仍会遇到以下错误:

66[错误]从
int(*)(studentType*,int)
int
[-fppermissive]的转换无效

30[错误]初始化
无效打印结果的参数3(std::of stream&,studentType*,int,int)
[-fppermissive]

我在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;
                }
                
            }
            
        }
        
    }
    
#包括
#包括
#包括
#包括
使用名称空间std;
//为学生设置限制
const int max_学生=20;
//学生信息结构
结构studentType
{
字符串名;
字符串lastName;
智力得分;
煤焦品位;
};
//函数声明
void数据输入(ifstream&infle,studentType slist[],int listSize);
void calcGrade(studentType sList[],int listSize);
无效打印结果(流文件和输出文件、studentType sList[]、int highScore、int listSize);
int高分(studentType sList[],int listSize);
int main()
{
河流充填;
出流孔的直径;
int listSize=最大学生数;
studentType sList[max_students];
//检查输入文件
infle.open(“scores.txt”);
如果(!infle)
{
cout sList[i].lastName>>sList[i].score;
}
}
//根据输入分数计算分数
无效计算等级(studentType sList[],整数分数,整数列表大小)
{
for(int i=0;i=90)
sList[i]。分数='A';
否则如果(分数>=80)
sList[i]。分数='B';
否则如果(分数>=70)
sList[i]。分数='C';
否则如果(分数>=60)
sList[i]。分数='D';
其他的
sList[i]。分数='F';
}
}
//计算最高分数
int highScore(studentType sList[],int listSize)
{
int i=0;
int highScore=sList[i]。分数;
对于(i;i高分)
{
highScore=sList[i]。分数;
}
}
返回高分;
}
//将结果打印到输出文件中
无效打印结果(流文件和输出文件、studentType sList[]、int highScore、int listSize)
{
int topScore=高分;
int i=0;
//标题
outFile两个主要问题:

  • 您定义了
    void-calcGrade(studentType-sList[],int-listSize);
    但随后将其实现为
    void-calcGrade(studentType-sList[],int-score,int-listSize)

  • 主要是使用未定义的变量
    highScore
    ,只定义了具有此名称的函数,这就是为什么会出现奇怪的错误,即指向函数的指针无法转换为int

  • 问题在于:

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

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

    你真的认为发布所有代码有用吗?!@Bathsheba我是这个网站的新手。我不记得在推荐中看到过不这样做,但谢谢你的挑剔。我想这会有帮助的o建立上下文,但我现在知道不能这样做。@elpremotio:如果你感到被侮辱,很抱歉;我们的社区(特别是C++)往往会变得非常厌倦。请参考a的描述来帮助我们。如果可以,我建议使用
    std::vector sList
    和使用STL算法,这样可以避免这种不可读的循环。