Help With Functions

Hello I am a newbie at c++ and I am struggling on this. I have a text file as so below:

Janet 90 23 Red
Bill 80 25 Blue
Greg 91 10 Red
Kyle 50 30 Yellow
Aya 97 56 Blue
Ro 82 45 Yellow
Addie 44 40 Blue
Quess 19 78 Red
Ella 78 64 Yellow

And I have a for loop created for it and was able to output a certain team as so

for (int i=0; i<9; i++)
{
inFile >> name >> react >> response >> team;

if (team == "Red")
{
cout << name << " " << react << " " << response << endl;
}
else if (team == "Yellow" && "Blue")
{
continue;
}
}
cout << endl;
inFile.close();

I got it to output my desired Red team but I also need to calculate the average for the react and response times. I do not know how to calculate them from the text file. Can someone please help?
Perhaps

cout << name << " " << (react + response)/2 << endl;
That wouldn't work because I'm trying to average them separately. Like from the Red team react, I want it to do (90+91+19)/3 to get the average and then do it the same for the response part.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    const std::string file_name = "teams.txt" ;

    {
        // create a test input file. since you already have
        // an input file, you can skip this step
        std::ofstream(file_name) << "Janet 90 23 Red\n"
                                    "Bill 80 25 Blue\n"
                                    "Greg 91 10 Red\n"
                                    "Kyle 50 30 Yellow\n"
                                    "Aya 97 56 Blue\n"
                                    "Ro 82 45 Yellow\n"
                                    "Addie 44 40 Blue\n"
                                    "Quess 19 78 Red\n"
                                    "Ella 78 64 Yellow\n" ;
    }

    if( std::ifstream infile{file_name} ) // if the file could be opened for input
    {
        long long sum_react = 0 ;
        long long sum_response = 0 ;
        int num_red_items = 0 ;

        std::string name ;
        int react ;
        int response ;
        std::string colour ;

        while( infile >> name >> react >> response >> colour )
        {
            if( colour == "Red" )
            {
                std::cout << name << ' ' << react << ' ' << response << ' ' << colour << '\n' ;

                ++num_red_items ;
                sum_react += react ;
                sum_response += response ;
            }
        }

        if( num_red_items > 0 )
        {
            std::cout << '\n' << std::fixed
                      << "   average react == " << sum_react / double(num_red_items) << '\n'
                      << "average response == " << sum_response / double(num_red_items) << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/7aece26528c953ec
Okay I tried it and it worked for color red however when I try for color yellow and blue I get the wrong averages calculated as so.


cout << "Yellow: " << endl;
inFile.open("file.txt");
for (int i=0; i<9; i++)
{
inFile >> name >> react >> response >> team;

if (team == "Yellow")
{
cout << name << " " << react << " " << response << endl;
++num_yellow;
sum_react += react;
sum_response += response;
}
else if (team == "Red" && "Blue")
{
continue;
}
}
{
if (num_yellow > 0)
{
cout << "Avg: " << sum_react / double(num_yellow) << " " << sum_response / double(num_yellow);
}
}
cout << '\n';
cout << endl;
inFile.close();

The average for the yellow comes out as 136.667 for the react and 83.333 for the response. What am I doing wrong that its not calculating right?

Also I have another question. I have two txt files in my project running and after I code this, I need to open the second txt file to run it the same thing with a different data set. However, I am not getting the second txt file open. I used inFile.close() and it doesn't work.
This makes multiple passes through the file, one for each colour:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <fstream>

void processs_colour( std::istream& stm, const std::string& team_colour )
{
    long long sum_react = 0 ;
    long long sum_response = 0 ;
    int num_items = 0 ;

    std::string name ;
    int react ;
    int response ;
    std::string colour ;

    while( stm >> name >> react >> response >> colour )
    {
        if( colour == team_colour )
        {
            ++num_items ;
            sum_react += react ;
            sum_response += response ;
        }
    }

    std::cout << "\ncolour: " << team_colour << '\n' ;

    if( num_items > 0 )
    {
        std::cout << std::fixed
                  << "\t   average react == " << sum_react / double(num_items) << '\n'
                  << "\taverage response == " << sum_response / double(num_items) << '\n' ;
    }

    else std::cout << "\tno records were found\n" ;
}

void process_file( const std::string& file_name )
{
    std::cout << "\n\nfile: " << file_name << "\n----------------\n" ;

    if( std::ifstream infile{file_name} ) // if the file could be opened for input
    {
        // for each team colour
        for( const std::string clr : { "Red", "Blue", "Yellow" } )
        {
            processs_colour( infile, clr ) ; // print averages
            infile.clear() ; // clear the failed, eof state
            infile.seekg(0) ; // seek to the beginning of the file
        }
    }

    else std::cout << "could not open file for input\n" ;
}

int main()
{
    // for each file that we want to process
    for( const std::string file_name : { "teams.txt" , "another_file.txt" } )
         process_file(file_name) ; // process the file
}


Teaser: How do we modify this, so that the program makes only one pass through each file?
Thank you to everyone who helped! I appreciate it very much.
Topic archived. No new replies allowed.