1
votes

I am given a data file that contains sports data for 24 people. There are two rounds of the event. The first 48 lines of the file give the data for the 1st round and the second 48 lines give data for the 2nd round. The file looks like this:

Azrin, 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

Burks, Arthur 2.8 4.5 5.4 6.6 7.7 8.8 5.6 6.5

Crick, Francis 1.6 6.5 6.5 6.5 7.3 7.5 7.8 5.6

Curie, Marie 2.9 9.4 9.5 9.2 8.0 5.6 9.0 9.8

Dale, Nell 1.0 2.3 2.3 2.3 2.3 3.4 3.4 3.4

Einstein, Albert 1.5 5.0 5.0 5.0 5.0 5.0 5.0 5.0

Franklin, Rosalyn 2.0 7.8 7.8 7.6 7.5 8.0 6.7 7.6

Heisenberg, Werner 2.2 8.2 8.1 8.0 8.5 8.6 8.3 9.0

Hofstadter, Douglas 1.8 9.3 9.0 9.2 8.4 7.8 8.0 5.6

Krieger, Dorothy 3.0 7.6 7.8 8.7 5.9 6.4 7.1 8.0

Mead, Margaret 2.7 8.0 8.0 7.8 6.5 7.4 8.2 5.9

Minsky, Marvin 2.7 7.8 6.8 7.1 7.4 5.6 4.9 5.7

Newton, Isaac 2.3 9.0 9.1 9.0 9.2 9.2 9.0 8.9

Olsen, Catherine 2.6 7.8 9.1 8.4 8.4 8.2 8.4 7.6

Pascal, Blaise 3.0 4.5 6.4 8.0 5.6 7.5 6.5 8.0

Pylyshyn, Zenon 2.8 8.6 7.9 9.0 6.9 7.3 6.9 7.0

Sagan, Carl 2.5 7.8 9.0 8.7 8.6 4.6 7.6 8.0

Schroedinger, Erwin 2.7 8.0 7.8 8.0 9.0 6.7 8.7 6.5

Turing, Alan 2.5 7.8 8.0 8.0 7.9 7.3 7.6 7.6

Van Allen, James 2.7 6.9 7.0 7.0 7.2 6.7 7.0 8.0

Von Neumann, John 3.0 7.8 8.9 9.0 8.7 9.0 7.9 9.2

Watson, James 2.8 9.6 9.7 9.7 9.0 9.5 9.7 9.0

Weems, Chip 2.5 7.8 7.8 7.6 7.5 7.8 8.0 6.7

Azrin, Neil 2.1 6.0 5.0 6.5 7.8 5.6 8.8 7.6

Babbage, Charles 2.3 5.4 6.5 7.6 8.7 7.2 5.4 5.5

Burks, Arthur 2.7 4.1 5.4 5.6 7.8 8.2 5.6 6.5

Crick, Francis 1.5 7.5 6.4 6.5 7.2 7.5 7.5 6.6

Curie, Marie 2.8 9.5 9.1 9.2 8.4 5.6 9.3 9.7

Dale, Nell 2.0 3.3 3.3 3.3 2.3 3.1 3.0 4.4

Einstein, Albert 2.5 5.1 5.2 5.3 5.4 5.5 5.6 5.7

Franklin, Rosalyn 3.0 7.8 7.7 7.1 7.5 8.2 6.7 7.6

Heisenberg, Werner 2.5 8.2 8.4 8.0 8.7 8.7 8.1 9.0

Hofstadter, Douglas 2.8 9.1 9.0 9.1 8.4 6.8 8.0 6.6

Krieger, Dorothy 3.0 7.4 7.1 7.7 6.9 6.4 7.1 8.0

Mead, Margaret 2.8 8.1 8.0 7.1 6.5 6.4 8.2 5.9

Minsky, Marvin 2.1 7.8 7.8 7.1 7.4 7.6 5.9 5.7

Newton, Isaac 2.3 8.0 9.0 9.0 9.2 8.2 9.0 8.9

Olsen, Catherine 2.0 6.8 9.1 8.4 8.0 8.2 8.4 7.6

Pascal, Blaise 3.0 5.5 6.4 8.1 5.6 7.4 6.5 7.0

Pylyshyn, Zenon 2.7 8.6 6.9 9.0 6.5 7.3 8.9 7.0

Sagan, Carl 3.0 6.8 9.0 8.2 8.6 5.6 7.6 8.0

Schroedinger, Erwin 1.7 8.0 6.8 8.5 9.0 7.7 8.7 6.5

Turing, Alan 2.4 7.7 8.1 8.2 7.3 7.3 7.6 7.6

Van Allen, James 2.7 7.9 7.0 6.0 7.2 8.7 7.0 8.0

Von Neumann, John 3.0 7.1 8.9 9.0 7.7 9.0 8.9 9.2

Watson, James 2.1 8.6 9.8 9.8 9.0 9.1 9.7 9.0

Weems, Chip 2.7 6.8 7.3 7.6 6.5 7.8 8.0 6.2

This data continues for all 24 people and then repeats with different score values for the second round. The first number after the persons name is the level of difficulty and then next 7 numbers are the scores. I am trying to get this data out of the file and set it up in the following format:

Name Of Person Round 1: level of difficulty scores Round 2: level of difficulty scores

So the table for the above data would look something like this:

Arzin, Neil

Round 1: 2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6

Round 2: 2.1 6.0 5.0 6.5 7.8 5.6 8.8 7.6

Babbage, Charles

Round 1: 2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5

Round 2: 2.3 5.4 6.5 7.6 8.7 7.2 5.4 5.5

And so on for all 24 people. This is the code I have for it, but it just reads the file directly:

// I have already opened the file!
while (inFile)
{
   string name;
   getline(inFile,name);
   cout << name << endl;
}

Could anyone provide some insight as to how I can do some sort of formatting on this data?

EDIT

I was given the following data structures to use

const int numRounds = 2;
const int numScores = 7;
const int numDivers = 24;

// 1D array for storing difficulty of dives on each round
typedef double DifficultyList[numRounds];

// 2D array of dive scores
typedef double ScoreTable[numRounds][numScores];

// struct to store info for one diver 
struct DiverRecord
{
    string name; // persons name
    double totalScore, // total score for both rounds
        diveTotal; // total on current round
    DifficultyList diff; // difficulty on all dives (1X2 array)
    ScoreTable scores; // scores for all rounds (2X7 array)
};

// array of structs of diver records
typedef DiverRecord DiverList [numDivers]; 

Thank you!

2
Start at the beginning, don't worry yet about how you are going to read the file. Just setup the variables you are going to read the file into. When you've had a go at that, post what you wrote here. You're going to need things like structs, strings, numbers, vectors and such. Get that sorted first.john
John, here is what I have as far as variables and structs. We were given structs and arrays of structs to use, but when I went to recitation I was told we dont have to use those to just print out the data. Thats when I became confused. Here is what I have:Jane
const int numRounds = 2; const int numScores = 7; const int numDivers = 24; // 1D array for storing difficulty of dives on each round typedef double DifficultyList[numRounds]; // 2D array of dive scores typedef double ScoreTable[numRounds][numScores]// struct to store info for one diverJane
// struct to store info for one diver struct DiverRecord { string name; // persons name double totalScore, // total score for both rounds diveTotal; // total on current round DifficultyList diff; // difficulty on all dives (1X2 array) ScoreTable scores; // scores for all rounds (2X7 array) {; // array of structs of diver records typedef DiverRecord DiverList [numDivers];Jane
This is very frustrating to me because I am trying very hard to do well in the class. I do not expect anyone to do my assignment for me, just some guidance into what I am doing wrong or what I can do. I really do want to understand C++ because it is not only enjoyable to do, but also that it will be a great asset to me in my academic career.Jane

2 Answers

0
votes

This is about formatted input.

If you really prefer C++, look into the direction of std::ifstream. See this tutorial for general streams information and this one for formatting.

Also here is question similar to your with lot of useful posts.

I usually use C standard file input and fscanf() function (man 3 fscanf) for such tasks. I code this faster.

0
votes

Your format is line based, so the first task is to read lines

string line;
// read lines until EOF
while (getline(inFile, line))
{
}

Once you have the line the next task is to parse each line to extract the data you want, the simple way to do that (which should be good enough for this task) is to use an istringstream.

#include <string>
#include <fstream>
#include <sstream>

string line;
// read lines until EOF
while (getline(inFile, line))
{
    // put the line into an istringstream
    istringstream buf(line);
    // read the surname and first name from the istringstream
    string surname, first_name;
    buf >> surname >> first_name;
    // read the scores until end of the line
    double score;
    while (buf >> score)
    {
        ...
    }
}

Something like that anyway, I'm not completely certain of your file format.

Hope this helps.