C ++ OOP,读取文件有问题,EOF使用了两次,排行榜

时间:2019-03-15 20:44:44

标签: c++

我正在做一个小游戏,我想做一个排行榜。我有排行榜类,并且正在根据leaderboard.txt中有多少名玩家创建动态表。因此,这是一个eof循环。然后,我想分配名称并指向排行榜类中的这些动态表。问题是我得到的是随机数字,而不是名称和分数。对我来说,代码看起来不错。有帮助吗?

class Leaderboard
{
    int max_counter;
    int counter;
    int *points;
    string *name;
    string filename;

public:
    Leaderboard(string n_file)
    {
        counter = 0;
        filename = n_file;
    }

string get_file(){return filename;}

void set_counter(int n_counter)
{
    max_counter = n_counter;
    points = new int[n_counter];
    name = new string[n_counter];
}

void add_value(string n_name, int n_points)
{
    name[counter] = n_name;
    points[counter] = n_points;
    counter++;
}

void show()
{
    for(int i=0;i<max_counter;i++)
    {
        cout << name[i] << " " << points[i] << endl;
    }
}

};

主要:

Leaderboard *top = new Leaderboard("leaderboard.txt");
            fstream file;
            file.open(top->get_file(), ios::in);
            if(file.good())
            {
                string name;
                int points;
                int counter = 0;

                while(!(file.eof()))
                {
                    file >> name >> points;
                    counter++;
                }
                counter--;
                top->set_counter(counter);
                while(!(file.eof()))
                {
                    file >> name >> points;
                    top->add_value(name,points);
                }

                cout << "Dodano pomyslnie" << endl;
                system("pause");
                top->show();

                file.close();
            }
            else cout << "Blad z plikiem!" << endl;

            delete top;

            break;

2 个答案:

答案 0 :(得分:1)

几个错误

with cte as (
  select `Order`
  , row_number() over (order by `Order` asc)  as rn_asc
  , row_number() over (order by `Order` desc) as rn_desc
  from mytable
)
update mytable t
join cte a on a.Order = t.Order
join cte d on d.rn_desc = a.rn_asc
set t.Order = d.Order;

应该是

            while(!(file.eof()))
            {
                file >> name >> points;
                counter++;
            }

第二个错误,您不能指望文件仅仅因为您想要就神奇地回到了开头。你必须告诉它。

            while (file >> name >> points)
            {
                counter++;
            }

答案 1 :(得分:0)

允许我建议您在此处使用的一般方法可以带来很大的改进。

现在,我们的main知道(并且必须知道)Leaderboard的内部知识以完成其工作。

最好不要这样做。排行榜本身应该是唯一了解内部结构的部分。

不过,让我走得更远:排行榜基本上只是分数的集合。它也不应该也不关心单个分数的内部细节。

最后,让我建议您考虑使用标准库中的容器。对于您而言,看来std::vector可以很好地工作。

#include <iostream>
#include <vector>
#include <iterator>
#include <vector>
#include <fstream>
#include <algorithm>

class score {
    std::string name;
    int points;
public:

    friend std::istream& operator>>(std::istream& is, score& s) {
        return is >> s.name >> s.points;
    }

    friend std::ostream& operator<<(std::ostream& os, score const& s) {
        return os << s.name << ": " << s.points;
    }
};

class leaderboard {
    std::vector<score> scores;
public:
    friend std::istream& operator>>(std::istream& is, leaderboard& l) {
        std::copy(
            std::istream_iterator<score>(is), std::istream_iterator<score>(),
            std::back_inserter(l.scores));
        return is;
    }

    friend std::ostream& operator<<(std::ostream& os, leaderboard const& l) {
        for (auto const& s : l.scores)
            os << s << "\n";
        return os;
    }
};

int main() {
    leaderboard scores;
    std::ifstream in("leaderboard.txt");

    in >> scores;

    std::cout << "Top scores\n";
    std::cout << scores;
}

当然,还有更多几乎可以肯定的事情要做,例如按照得分的降序对得分进行排序,因此得分最高的人首先出现-但这是一个单独的问题。