C ++从文件中读取

雅虎5000

您好,我是C ++的初学者,现在只需参加一些测验就可以学习,但是现在我面临的问题是缺少C ++的知识,所以我有一个文件,含u2.txt,其内容是

 3 5
Petras Rasa // name of dancer 
3 1 5 8 10 // scores for a technique 
5 6 7 8 9 // scores for art 
Rita Jurgis
6 5 8 5 8
9 8 7 6 5
Rasa Linas
10 10 10 10 10
8 8 8 8 8

因此任务说前两个数字显示舞者和评委的编号,第二行显示舞者的名字,下面的行显示
技巧得分,下一行显示艺术性得分,因此它的3个舞者得分低于分数。

因此,现在我需要编写一个函数来读取该文件,并将分数和名称存储到变量中,我正在使用类和数组系统,因此我将它们存储在此处,问题是我真的不知道如何读取此类文件因为直到现在我仍在学习实现像Name,54这样的真实文件,所以我使用的是getline并以“'”结束行,但是没有逗号了,所以我遇到了一个问题,直到现在这是我的功能:

const string U2 = "U2.txt";
const string U2rez = "rez.txt";

class Results
{
public:
    string name;
    int scorestech;
    int scoresart;
};


int main()
{
    Results v[5]
    return 0;
}

void Freading(const string fn,Results v[])
{
    int alldancers;
    int alljudge;
    ifstream fin(fn.c_str());
    fin >> alldancers >> alljudge;
    for(int a =0; a < 3; a++){


    }

}

所以现在我需要阅读此文件,并计算每位舞者的艺术和技艺得分

雅虎5000
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const string U2 = "U2.txt";
const string U2rez = "rez.txt";

class Results
{
public:
    string name;
    int scorestech;
    int scoresart;
};

void Freading(const string fn,Results v[]);

int main()
{
    Results v[5];
    Freading(U2,v);
    return 0;
}

void Freading(const string fn,Results v[])
{
    int alldancers;
    int alljudges;
    int biggest = 0;
    int smallest = 99;
    ifstream fin(fn.c_str());
    fin >> alldancers >> alljudges;
    fin.ignore();
    for(int a =0; a < alldancers; a++){
        int biggest = 0;
        int smallest = 99;
        int totaltech = 0;
        int totalart = 0;

        getline(fin, v[a].name);

        for(int b = 0; b < alljudges; b++)
        {
            int scores;
            fin >> scores;
            totaltech += scores;
        }
        for(int b =0; b < alljudges; b++)
        {
            int scores;
            fin >> scores;
            totalart += scores;
        }
        v[a].scorestech = totaltech;
        v[a].scoresart = totalart;
        cout << v[a].name << endl;
        cout << v[a].scorestech <<endl;
        cout << v[a].scoresart <<endl;

    }
    fin.close();
}

这是我到目前为止所做的,但是我得到的结果是:

Petras Rasa
27
35

0
45

0
45

知道我做错了或做错了什么,因为不知何故,当循环第二次执行时,它没有捕获第二个舞者的名字

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章