简体   繁体   English

将数据读入结构数组的指针c ++

[英]pointer to read data into an array of structs c++

I have a text file that looks like: 我有一个文本文件,看起来像:

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

It continues for 24 different names. 它继续使用24个不同的名称。 The first name is on line one and the numbers are on the second line in the file. 名字在文件的第一行,数字在文件的第二行。 The first number is the difficulty level of the dive and the follow 7 numbers are the scores given by 7 different judges. 第一个数字是潜水的难度级别,第二个数字是7位不同裁判给出的分数。

I have been able to read in the divers name and difficulty level but it will not read in the scores that follow the difficulty level. 我已经能够阅读潜水员的姓名和难度级别,但不会阅读难度级别后面的分数。

I have tried just about everything I can think of to get it to work correctly, can anyone please help out? 为了使它正常工作,我已经尝试了几乎所有我能想到的东西,任何人都可以帮忙吗?

My code so far to read the data into the struct looks like this 到目前为止,我的代码已将数据读取到struct中,如下所示

const int NUM_NAMES = 24;
const int NUM_SCORES = 7;
const int NUM_ROUNDS = 2;


typedef double scoreTable[NUM_ROUNDS] [NUM_SCORES];
typedef double difficultyList[NUM_ROUNDS];

struct diveInfo
{
string diversName;
double totalScore;
double diveTotal;
difficultyList diff;
scoreTable scores;

};


typedef diveInfo diverList[NUM_NAMES];


int main()
{
diveInfo record;
diveInfo * ptr;
ptr = &record;
int lcv;

ifstream inFile;
inFile.open ("diveData.txt");

if (!inFile)
{
    cout << "ERROR: File could not be opened" << endl << endl;

    exit (EXIT_FAILURE);
}


for (lcv = 0; lcv < NUM_NAMES; lcv++)

getline(inFile, ptr[lcv].diversName);

delete ptr;
ptr = new diveInfo;

inFile >> *ptr[lcv].diff;

delete ptr;
ptr = new diveInfo[NUM_SCORES];

inFile >> *ptr[lcv].scores;   // here is the problem 

return (EXIT_SUCCESS);
}

Your code is horribly confused. 您的代码非常混乱。 I guess it started out simple but when that didn't work it got more and more complicated. 我猜它起初很简单,但是如果不起作用,它就会变得越来越复杂。 But this is quite easy, you don't need anything complicated. 但这很容易,您不需要任何复杂的事情。 The first thing is that you don't need pointers and you don't need new . 第一件事是,您不需要指针 ,也不需要new

Let's start at the beginning, your structs are wrong, and you cannot get anywhere until you get those right. 让我们从头开始,您的结构是错误的,除非正确,否则您将无所适从。 Think about your data, you have one name, one difficulty score, and seven judges scores. 考虑一下您的数据,您有一个名字,一个难度分数和七个法官分数。 Put that in a struct 把它放在一个结构中

struct DiveInfo
{
    string diversName;
    double diff;
    double scores[7];
};

Now we have 24 different dives, so we need an array of 24 DiveInfo structs 现在我们有24种不同的潜水,所以我们需要24种DiveInfo结构的数组

DiveInfo diveList[24];

Now we can start to do some reading, remember no pointers and no new . 现在,我们可以开始阅读一些内容,记住没有指针 ,也没有新知识 This is simple not complicated. 这简单而不复杂。

for (int i = 0; i < 24; ++i)
{
    getline(inFile, diveList[i].diversName);
    inFile >> diveList[i].diff;
    for (int j = 0; j < 7; ++j)
        inFile >> diveList[i].score[j];
    string garbage;
    getline(inFile, garbage); // move to next line, ready to read next name
}

See? 看到? Each time round the loop we read a single DiveInfo (two lines in your file). 每次循环时,我们都会读取一个DiveInfo(文件中的两行)。 Each DiveInfo is a name followed by the difficulty score, followed by the 7 judges scores. 每个DiveInfo是一个名称,后跟难度分数,然后是7个评委分数。 The only slightly complicated thing is the getline(..., garbage); 唯一稍微复杂的是getline(..., garbage); bit at the end. 最后一点。 That is necessary to move the input onto the next line when you have read the last score. 阅读完最后一个乐谱后,将输入移至下一行很有必要。

Try changing 尝试改变

 typedef double scoreTable[NUM_ROUNDS] [NUM_SCORES];

to

 typedef double scoreTable[NUM_SCORES];

You are not using the second dimension of the array in your program and the data file does not appear to be using it either. 您没有在程序中使用数组的第二维,并且数据文件似乎也没有在使用它。

Also why are you deleting the data structure you created after reading each item? 另外,为什么要删除读取每个项目后创建的数据结构? If you want an array of diveInfo object you should create it, then iterate through it for each record you want to read. 如果您需要一个数组diveInfo对象,则应创建它,然后针对要读取的每条记录对其进行迭代。 Within each record you will need to iterate through each score reading them individually. 在每个记录中,您将需要遍历每个分数,分别阅读它们。

This should get you in the right direction: 这应该为您指明正确的方向:

diveInfo theDiveInfo[40]; // This array of diveInfo will hold all of the information for 40 divers

for(int i = 0; i < NUM_RECORD; ++i)
{
  getline(inFile, theDiveInfo[i].diversName);

  for(int j = 0; j < NUM_SCORES; ++j)
    inFile >> theDiveInfo[i].scores[j];
}

You are using C++ like C. It would be easier, and clearer to create classes instead of structures. 您正在像C一样使用C ++。创建类而不是结构将更容易,更清晰。

You should create a class Parser which could handle your file and a class Person which could store the name, difficulty and an array of scores. 您应该创建一个可以处理文件的Parser类,以及一个可以存储名称,难度和分数数组的Person类。

This way, your parser could create Person instances and it could store them in a container. 这样,您的解析器可以创建Person实例,并将其存储在容器中。

Also, you could use the STL to store your data. 另外,您可以使用STL存储数据。

Tell me if you need more explanations. 告诉我是否需要更多说明。

Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM