0
    // Proj7.cpp
    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;

    #include "Proj7.h"

    int main()
    {
        //Student *records = new Student [15];
        Student *records [15];
        Student *temp;
        string ID;
        string name;
        int average;
        double testScores;
        double Average(int, double);

        ifstream inFile;
        ofstream outFile;

        inFile.open("Proj7_input.txt");
        outFile.open("Proj7_output.txt");

        if(inFile.is_open())
        {
            while(!inFile.eof())
            {
                for(int i = 0; i < 15; i++)
                {
                    temp = new Student();

                    //ID
                    inFile >> ID; //read from file
                    temp ->setID(ID); //set ID
                    outFile << "Student's ID: " << temp ->getID(); //get and display

                    //NAME
                    getline(inFile, name);//read entire line including spaces
                    outFile << endl;
                    //temp ->setName(name);
                    outFile << "Student's name: " << name << endl;
                    cout << name;
                    //outFile << endl;

                    //TEST SCORES
                    for(int j = 0; j < 10; j++)
                    {
                        inFile >> testScores;
                        temp ->setScores(testScores);
                        outFile << "Test score " << j + 1 << ": " << testScores;
                    }

                    //AVERAGE
                    outFile << temp ->getName() << "'s average is: " << temp ->getAverage();
                    outFile << endl;
                    outFile << endl;
                    records[i] = temp;
                }
            }
        }

    }

    ===========================================================================

    #ifndef PROJ7_H_INCLUDED
    #define PROJ7_H_INCLUDED
    //Proj7.h
    using namespace std;
    class Student
    {
    private:
        string ID;
        string name;
        double testScores;
        double average;
    public:
        void setID(string i_d) { ID = i_d; }
        void setName(string n) { name = n; }
        void setScores(double s) { testScores = s; }
        void setAverage(double a) { average = a; }
        double Average(double testScores[][10],double avg[]);
        double getScores() const { return testScores; }
        double getAverage() const { return average; }
        string getID() const { return ID; }
        string getName() const { return name; }
        string display() const;
    };
    double Student::Average(double testScores[][10],double average[])
    {
        for(int i = 0; i < 15; i++)
        {
            double sum = 0;
            for(int j = 0; j < 10; j++)
                sum += testScores[i][j];

            average[i] = sum/ 10;
        }
        return 0;
    }
    #endif // PROJ7_H_INCLUDED

    ===========================================================================
A0375
Daenerys Targaryen
90
88
79
95
79
80
88
76
79
99

A0376
Tyrion Lannister
77
86
80
81
83
85
90
94
95
87

A0377
Brienne of Tarth
99
98
97
79
80
89
85
96
97
100

I've been having the most trouble working with the for loop to print out all 10 of the test scores for each of the 15 students, while also adding them up to calculate the average. Also, I'm lost with how to use the pointer. This is what continues to print out almost infinitely:

Student's ID: A0375 Student's name: Test score 1: 0Test score 2: 0Test score 3: 0Test score 4: 0Test score 5: 0Test score 6: 0Test score 7: 0Test score 8: 0Test score 9: 0Test score 10: 0's average is: 0

and ends with the same student ID..

1 Answers1

0

you need to flush the input stream before getline() I think its skipping getline() which would make it print out infinitely.

inFile.ignore();
getline(inFile, name);
Manvir
  • 769
  • 1
  • 7
  • 15
  • This definitely solved a lot of the problems I was facing, but now I am struggling to print out the average. I keep trying to use a pointer to display the average, I also thought I should try using a getter function to somehow make it easier... – user2495587 Apr 16 '17 at 06:59
  • What does the program print out for average? – Manvir Apr 16 '17 at 07:48
  • I've tried different ways of "outFile << name << "'s average is: " << average;" and it either doesn't run, prints 0, or prints 0x0. I'm having trouble understanding how to print it. How do I use a pointer here? – user2495587 Apr 16 '17 at 07:56
  • I think the problem is cause by the double Student::Average function. (https://stackoverflow.com/questions/3473438/return-array-in-a-function) and did you use the setAverage function? – Manvir Apr 16 '17 at 07:57