1 Replies - 179 Views - Last Post: 15 April 2013 - 11:45 AM Rate Topic: -----

#1 janebartley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-April 13

Help Outputting Data From A File Into Structs And Arrays Of Structs

Posted 15 April 2013 - 11:38 AM

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



If anyone could provide some insight on this it would be really appreciated! I have been searching online and looking in my book frantically but I can not find anything that is helpful.

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Help Outputting Data From A File Into Structs And Arrays Of Structs

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8894
  • View blog
  • Posts: 33,361
  • Joined: 12-June 08

Re: Help Outputting Data From A File Into Structs And Arrays Of Structs

Posted 15 April 2013 - 11:45 AM

Duplicate topic.. closing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1