paulius juoz
paulius juoz

Reputation: 23

Trouble with for loop

I have problem with my program,I need to write to file how much games each player played in 5 days ,here is my txt file:first number in first line is how much days they played,first number from line 2 to line 5 is how much days they played,other numbers in each line is how much games they played in these days:

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

Here is my program:

 #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;
 } 
 }

Can you help me to write this program to the end,thanks.

Upvotes: -1

Views: 105

Answers (2)

HvS
HvS

Reputation: 514

I think this might be what you are looking for:

#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
    }
}

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

After reading the number of games played, you need to read the games themselves. Something like:

#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
    }
 } 
 }

Also use programuotojai instead of the constant 6(if I get the code correctly).

I will not write the complete program but it seems to me you have to sum the numbers on each line.

Upvotes: 1

Related Questions