0

I have a program assignment to write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. I use functions and structs, with arrays to accomplish this, and of course the expected InFile mechanics. However, I'm having issues storing the content from the file into the appropriate member, and cannot get anything stored at all.

I've tried rewriting the function and placing it before and after the main function, to no avail, and renaming and restructuring my struct.

My code is as follows.

#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;
        }

These are the error codes I'm receiving

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
SHaD0S
  • 5
  • 3
  • Your `studentType` doesn't contain any member variables. – NathanOliver Jul 15 '19 at 19:25
  • It seems you could use [a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). to read. Or (being a little rude) follow along better in the classroom. – Some programmer dude Jul 15 '19 at 19:44
  • Some programmer dude, I won't pretend you're wrong. I need to dedicate more time to programming, you and NathanOliver and Roout have pointed out I've made a very beginner yet important mistake. Thank you for the recommended books. I'll definitely check them out. – SHaD0S Jul 15 '19 at 20:17
  • Just remember, books and classes alone won't make you a good programmer (especially classes, I left high school with barely a passing grade, never finished college, and have worked for over 20 years with programming), it also takes determination and practice. A lot of it. But at least you've started, and you realize and (more importantly) acknowledge your mistakes when pointed out. Making mistakes is a good way to learn besides the books, classes and practice (we all still makes them). Continue on that road and you might just become the next great programmer. :) – Some programmer dude Jul 15 '19 at 20:32

1 Answers1

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

You've just declared 4 methods in your struct. It should be:

struct studentType
        {
            string studentFName;
            string studentLName;
            int testScore;
            char grade;
        };
Roout
  • 56
  • 1
  • 5
  • Code needs a lot more changes than just this. – NathanOliver Jul 15 '19 at 19:32
  • 1
    The OP is making the common newbie mistake of writing way too much code in one go. Write a few lines of code, make sure they are working, then write a few more lines of code. It's the fastest way to make progress. – john Jul 15 '19 at 19:51