我在从文件中获取数据并将其输入到给定的结构和结构数组然后输出文件时遇到了很多麻烦。我们得到一个包含 96 行的文件,如下所示:
尼尔·阿尔津
2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
查尔斯·巴贝奇
2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5
该文件针对 24 个不同的人继续,然后以不同的分数重复(第二行)。第一个数字,在这种情况下,两个人都是 2.3,是难度等级。接下来的6个数字是分数。
我们得到这些数据是为了设置我们的结构和数组以及我的代码:
#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;
}