我正在做一個任務,要求創建一些函數分別讀取數據,計算分數和輸出數據。C++無效轉換int *到int
我覺得我很接近有這個完整的,但繼續運行到以下錯誤:
66 [錯誤]無效的轉換,從
int (*)(studentType*, int)
到int
[-fpermissive]30 [錯誤]初始化的
void printResults(std::ofstream&, studentType*, int, int)
參數3 -fpermissive]
我搜索通過計算器和其他論壇,但不能完全得到它的底部。有誰知道我的代碼可能會導致這些錯誤?
我也有一個偷偷摸摸的懷疑,我傳遞價值的方式並不完全是現貨。我知道這是一個開放式的問題,但任何反饋將不勝感激。
下面是代碼:
#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;
}
}
}
}
你真的認爲這有助於發佈所有的代碼?! – Bathsheba
@Bathsheba我是這個網站的新手。我不記得在建議中沒有這樣做,但是謝謝你的發言。 – elpretentio
我認爲這將有助於建立背景,但我現在不知道。 – elpretentio