c++ - 无法使用 InFile 将文件正确读入结构成员

标签 c++ string struct file-io

我有一个程序作业要编写一个程序来读取学生的名字,然后是他们的考试成绩。该程序应输出每个学生的姓名,后跟考试成绩和相关成绩。它还应该找到并打印最高考试成绩和考试成绩最高的学生的姓名。我使用函数和结构以及数组来完成此操作,当然还有预期的 InFile 机制。但是,我在将文件内容存储到适当的成员时遇到问题,并且根本无法存储任何内容。

我曾尝试重写该函数并将其放置在主要函数之前和之后,但无济于事,并重命名和重构我的结构。

我的代码如下。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

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() {
    //Variable Declaration
    const int STUDENTCOUNT = 20;
    ifstream inData;
    ofstream outData;
    studentType studentList[STUDENTCOUNT];

    //Open file
    inData.open("Ch9_Ex2Data.txt");

    //Call functions
    getData(inData, studentList, STUDENTCOUNT);
    calculateGrade(studentList, STUDENTCOUNT);
    printResult(outData, studentList, STUDENTCOUNT);
    system("PAUSE");
    return 0;
}

 //Data from Ch9_Ex2Data.txt function
 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;
 }

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

int highestScore(const studentType sList[], int listSize)
    {
        int score[100];
        int highscore = score[0];
        for (int i = 0; i < listSize; i++) 
        {
            if (score[i] > highscore)
                {
                    highscore = score[i];
                }
        }
    }


    void printResult(ofstream& outFile, const studentType sList[], 
    int listSize)
    {
        int maxScore = highestScore(sList, listSize);
        int i;
        outFile << "Student Name " << "Test Score" << "Grade" << 
        endl;
        for (i = 1; i < listSize; i++)
        outFile << left << sList[i].studentLName() + ", " + 
        sList[i].studentFName <<                 right << " " << s 
        List[i].testScore << " " << 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;
        }
        i = 1; i < listSize; i++)
            if (sList[i].testScore() == maxScore)
                outFile << sList[i].studentLName() + ", " + 
        sList[i].studentFName << endl;
        }

这些是我收到的错误代码

main.cpp: In function ‘void getData(std::ifstream&, studentType*, int)’:
main.cpp:42:10: error: invalid use of non-static member function 
‘std::__cxx11::string studentType::studentFName()’
   inFile >> sList[i].studentFName >> sList[i].studentLName
main.cpp:9:16: note: declared here
      string studentFName();
             ^~~~~~~~~~~~
main.cpp: In function ‘void calculateGrade(studentType*, int)’:
main.cpp:51:27: error: lvalue required as left operand of assignment
     sList[i].grade() = 'A';
                        ^~~
main.cpp:53:27: error: lvalue required as left operand of assignment
     sList[i].grade() = 'B';
                        ^~~
main.cpp:55:27: error: lvalue required as left operand of assignment
     sList[i].grade() = 'C';
                        ^~~
main.cpp:57:27: error: lvalue required as left operand of assignment
     sList[i].grade() = 'D';
                        ^~~
main.cpp:59:27: error: lvalue required as left operand of assignment
     sList[i].grade() = 'F';
                        ^~~
main.cpp: In function ‘int highestScore(const studentType*, int)’:
main.cpp:73:5: warning: no return statement in function returning non-void [-Wreturn-type]
  }
  ^
main.cpp: In function ‘void printResult(std::ofstream&, const studentType*, int)’:
main.cpp:82:45: error: passing ‘const studentType’ as ‘this’ argument discards qualifiers [-fpermissive]
 outFile << left << sList[i].studentLName() + ", " + sList[i].studentFName << right << " " << sList[i].testScore << " " << sList[i].grade << endl;
                                          ^
main.cpp:10:16: note:   in call to ‘std::__cxx11::string studentType::studentLName()’
      string studentLName();
             ^~~~~~~~~~~~
main.cpp:82:54: error: invalid use of non-static member function ‘std::__cxx11::string studentType::studentFName()’
 outFile << left << sList[i].studentLName() + ", " + sList[i].studentFName << right << " " << sList[i].testScore << " " << sList[i].grade << endl;
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:9:16: note: declared here
      string studentFName();
             ^~~~~~~~~~~~
main.cpp:86:27: error: passing ‘const studentType’ as ‘this’ argument discards qualifiers [-fpermissive]
 if (sList[i].testScore() == maxScore)
                        ^
main.cpp:11:13: note:   in call to ‘int studentType::testScore()’
      int testScore();
          ^~~~~~~~~
main.cpp:87:38: error: passing ‘const studentType’ as ‘this’ argument discards qualifiers [-fpermissive]
  outFile << sList[i].studentLName() + ", " + sList[i].studentFName << endl;
                                   ^
main.cpp:10:16: note:   in call to ‘std::__cxx11::string studentType::studentLName()’
      string studentLName();
             ^~~~~~~~~~~~
main.cpp:87:47: error: invalid use of non-static member function ‘std::__cxx11::string studentType::studentFName()’
  outFile << sList[i].studentLName() + ", " + sList[i].studentFName << endl;
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:9:16: note: declared here
      string studentFName();
             ^~~~~~~~~~~~
/bin/bash: line 4: ./a.out: No such file or directory

最佳答案

struct studentType
    {
        string studentFName(); // function declaration
        string studentLName();// function declaration
        int testScore();// function declaration
        char grade();// function declaration
    };

您刚刚在结构中声明了 4 个方法。应该是:

struct studentType
        {
            string studentFName;
            string studentLName;
            int testScore;
            char grade;
        };

关于c++ - 无法使用 InFile 将文件正确读入结构成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57046022/

相关文章:

c++ - 如何将字符串/数字排序添加到 QSortFilterProxyModel 派生类

c++ - C++ 中有什么支持序列化的映射?

c++ - 在 C++ 中计算数组中不相等的字符串

c - C 结构体中的指针地址

swift - 在闭包中捕获结构引用不允许发生突变

c++ - 重载赋值运算符,用于将 sql::ResultSet 赋值给 struct tm

java - 不兼容的类型 : void cannot be converted to string

python - 将纪元时间转换为 Pandas 数据框中的格式化日期字符串

c - C索引中的字符串指针

c++ - vector 和结构错误