0
votes

I am having a lot of trouble being able to get data from a file and input it into given structs and arrays of structs and then outputting the file. We are given a file that contains 96 lines and looks like this:

Arzin, Neil
2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
Babbage, Charles
2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5

This file continues for 24 different people and then repeats with different scores (the second line). The first number, in this case is 2.3 for both people is a difficulty rating. The next 6 numbers are scores.

We are given this data in order to set up our structs and arrays and my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main ()
{

  ifstream inFile;
  inFile.open("C://diveData.txt");

  // checking to see if file opens correctly
  if (!inFile)
  {
      cout << "Error opening the file!" << endl;
  }

  const int numRounds = 2;              // variable to hold the number of rounds
  const int numScores = 7;              // variable to hold the number of rounds
  const int numDivers = 24;             // variable to hold the number of divers
  typedef double DifficultyList[numRounds];   // 1D array for storing difficulty                of dives on each round 
  typedef double ScoreTable [numRounds] [numScores]; // 2D array of dive scores

// struct to store information for one diver
  struct DiverRecord
{
   string name;
   double totalScore;
  double diveTotal;
  DifficultyList diff;
  ScoreTable scores;
};

DiverRecord DiverList[numDivers];

// my attempt at printing out the contents of the file
  while (!EOF)
 {
    for (int x = 0; x < 25; x++)
    { 
       infile >> DiverList[x].name;
       inFile >> DiverList[x].totalScore;
       inFile >> DiverList[x].diveTotal;

       cout << DiverList.[x].name << endl;
       cout << DiverList.[x].totalScore << endl;
       cout << DiverList.[x].diveTotal << endl;
    }
  }

return 0;
}
3
For starters, this won't read a thing. EOF is typically (-1) which will always evaluate to non-zero therefore while (!EOF) will break immediately. - WhozCraig

3 Answers

0
votes

First of all the >> operator ends input at the first whitespace character so when you read in the name you only get the last name and the comma it will try to put the first name into totalScore. To get the full name do the following.

  string temp;
  infile >> DiverList[x].name; 
  infile >> temp;
  DiverList[x].name + " ";
  DiverList[x].name + temp;

Also when outputting you don't need that extra '.'

  cout << DiverList[x].name << endl;

and so on should work just fine.

0
votes

Couple of questions:

  1. decide whether ifstream is open or not, use ifstream::is_open;
  2. decide whether end of file is encountered, try code below;
  3. If I get it right, your input file format should be:

    name1,name2
    difficulty score1 score2 ... score7

    In this sense, the totalScore should not be input from the stream, but calculated instead.

  4. you have two names for one record, so the definition of your record structure seems fuzzy.

Here is a revised version:

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

struct DiverRecord
{
    string a, b;
    double totalScore;
    double difficulty;
    double scores[7];
};

int main ()
{

    ifstream inFile("C://diveData.txt");

    // checking to see if file opens correctly
    if (!inFile.is_open()) {
        cout << "Error opening the file!" << endl;
    }

    vector<DiverRecord> DiverList;

    DiverRecord record;
    char ch;
    while (inFile) {
        inFile >> record.a >> ch >> record.b >> record.difficulty;
        record.totalScore = 0;
        for (int i = 0; i < 7; ++i) {
            inFile >> record.scores[i];
            record.totalScore += record.scores[i];
        }

        DiverList.push_back(record);
    }

    // output your DiverList here.

    return 0;
}
0
votes

As I said in my earlier comment I am now trying to concentrate on not using the structs and arrays of structs and am trying to use other functions in order to read the data from the file. I want to put the data in a logical form such as:

Name Of Person Round 1: Difficulty Scores Round 2: Difficulty Scores

But I am having trouble accessing specific elements from the file.

The code I am using is:

while(inFile)
{
   string name;
   getline(inFile, name);
   cout << name << endl;
}

This outputs the data file as is, but when I try to declare different variables for the difficulty and the seven scores it does not output correctly at all.