1

So I need help creating a program that will open a file and read the data from the file into an array of structs and then calculate a variety of things like highest, lowest, average and standard deviation. Right now, I am more concerned about how to read the actual file and place it into an array of structs.

Here are the instructions for the assignment:

-You will read the input data from an input file scores.txt (will be posted in Etudes); and data is in the format (studentID, first name, last name, exam1, exam2 and exam3).

-Each line of data for one student will be read from file and then assigned to a struct variable. Consequently, you will need an array of structs to store all the data read from the input file. This will be a 1-dimensional array.

-Once you read data from the file to your array, you are required to calculate and display the following statistics for each exam.

Here is the data file:

1234 David Dalton 82 86 80
9138 Shirley Gross 90 98 94
3124 Cynthia Morley 87 84 82
4532 Albert Roberts 56 89 78
5678 Amelia Pauls 90 87 65
6134 Samson Smith 29 65 33
7874 Michael Garett 91 92 92
8026 Melissa Downey 74 75 89
9893 Gabe Yu 69 66 68

#include "stdafx.h"
#include <iostream> 
#include <string> 
#include <fstream>
#include <iomanip> 

using namespace std; 

struct StudentData
{
    int studentID; 
    string first_name; 
    string last_name; 
    int exam1; 
    int exam2; 
    int exam3; 
}; 

const int SIZE = 20; 

// Function prototypes
void openInputFile(ifstream &, string); 

int main()
{
    // Variables
    //int lowest, highest; 
    //double average, standardDeviation; 
    StudentData arr[SIZE]; 

    ifstream inFile; 
    string inFileName = "scores.txt"; 

    // Call function to read data in file
    openInputFile(inFile, inFileName);

    //Close input file
    inFile.close(); 

    system("PAUSE"); 

    return 0; 
}

/**
* Pre-condition: 
* Post-condition: 
*/
void openInputFile(ifstream &inFile, string inFileName)
{
    //Open the file
    inFile.open(inFileName);

    //Input validation
    if (!inFile)
    {
        cout << "Error to open file." << endl;
        cout << endl;
        return;
    }
}

For the moment, I am ignoring the variables that I put into comments. I was thinking about forgoing an openFile function and just doing it in the main function but I decided against that to make my main look a bit "cleaner". I considered just doing inFile >> arr[] after I called the openFile function but then it seemed unlikely that it would work or make sense.

Mary
  • 221
  • 1
  • 6
  • 17
  • For that you can simply use the normal input operator `>>` in a loop. You might want to learn about `operator>>` overloading. Also read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude Apr 23 '15 at 06:03
  • FWIW, I answered a similar question - showing how to write a custom `operator>>` - [here](http://stackoverflow.com/questions/20623506/storing-text-file-into-a-class/20623934?s=5|0.2236#20623934). – Tony Delroy Apr 23 '15 at 06:19

2 Answers2

3

My suggestion:

  1. Add an operator function to read one StudentData object from a stream.
  2. Add a while loop in main. In each iteration of the loop, read a StudentData
std::istream& operator>>(std::istream& in, StudentData& st)
{
    return (in >> st.studentID
               >> st.first_name
               >> st.last_name
               >> st.exam1
               >> st.exam2
               >> st.exam3);
}

and in main:

openInputFile(inFile, inFileName);

size_t numItems = 0;
while ( inFile >> arr[numItems] )
   ++numItems;

At the end of that, you would have successfully read numItems items into arr.

R Sahu
  • 196,807
  • 13
  • 136
  • 247
0

This should read all your data into the array, you'll want an incrementor to

ifstream inStream; inStream.open("scores.txt");

 while (!inStream.eof())
 {

       inStream >> StudentData arr[SIZE];

 };
 inStream.close();
dczx
  • 97
  • 1
  • 2
  • 8
  • 3
    I reiterate part of my comment to the original question: Please read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Also, what you've written will not even compile. – Some programmer dude Apr 23 '15 at 06:18