for循环的问题

Trouble with for loop

本文关键字:问题 循环 for      更新时间:2023-10-16

我的程序有问题,我需要写文件每个玩家在5天内玩了多少场游戏,这是我的txt文件:第一行的第一个数字是他们玩了多少天,第2行到第5行的第一个数字是他们玩了多少天,每一行的其他数字是他们在这些天里玩了多少场游戏:

5
5 3 2 3 1 2
3 6 2 4
4 2 2 1 2
3 3 3 3
2 3 4

下面是我的程序:

 #include <iostream>
 #include <fstream>
 using namespace std;
 int main()
 {
     int programuotojai,programos;
     ifstream fin ("duomenys.txt");
     fin >> programuotojai; //players
     for(int i = 1; i <= 6; i++){
     fin >> programos; //games played
     cout << programos;
 } 
 }

你能帮我把这个程序写完吗,谢谢。

在阅读了游戏的数量之后,你需要阅读游戏本身。比如:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    int programuotojai,programos;
    ifstream fin ("duomenys.txt");
    fin >> programuotojai; //players
    for(int i = 1; i <= programuotojai; i++){
    fin >> programos; //games played
     cout << programos;
    for (int j=0;j<programos;++j) {
      int day;
      fin>>day;
      // ..... do some fancy stuff
    }
 } 
 }

也使用programuotojai代替常量6(如果我得到正确的代码)。

我不会写完整的程序,但在我看来,你必须把每一行的数字加起来。

我想这可能是你正在寻找的:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    int players, daysPlayed, totalPlayed; 
    ifstream fin ("duomenys.txt");
    fin >> players; //  number of players.
    for( int playerCount = 1; playerCount <= players; playerCount++ ) {
        totalPlayed = 0;
        fin >> daysPlayed; //   number of days a player played games.
        for ( int dayCount = 1; dayCount <= daysPlayed; dayCount++ ) {
            int daysGameCount;
            fin >> daysGameCount; // amount of games a player played on each day.
            totalPlayed += daysGameCount;
        }
        cout << totalPlayed << endl;
        //  ... do whatever you want with the result
    }
}