0

我在从文件中获取数据并将其输入到给定的结构和结构数组然后输出文件时遇到了很多麻烦。我们得到一个包含 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;
}
4

3 回答 3

0

正如我在之前的评论中所说,我现在正试图专注于不使用结构和结构数组,并尝试使用其他函数来从文件中读取数据。我想将数据以逻辑形式放置,例如:

人名 第 1 轮:难度分数 第 2 轮:难度分数

但我无法从文件中访问特定元素。

我正在使用的代码是:

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

这会按原样输出数据文件,但是当我尝试为难度和七个分数声明不同的变量时,它根本无法正确输出。

于 2013-04-17T16:14:04.533 回答
0

首先,>> 运算符在第一个空格字符处结束输入,因此当您读取名称时,您只会得到姓氏和逗号,它将尝试将名字放入 totalScore。要获得全名,请执行以下操作。

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

此外,在输出时,您不需要额外的 '.'

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

等等应该可以正常工作。

于 2013-04-16T17:33:45.917 回答
0

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;
}
于 2013-04-16T17:29:36.587 回答