Totaling

Would appreciate help understanding how to get team totals from a function so that I may determine the winning team in a walking challenge.

I created a file consisting of team name followed by four team members per team (e.g., Ramblers Angie Peter Susan Fred, Trekkers John Adam Ann Sally). I then read the file and get each walker's steps for a specific # of days. I am able to total each walker's steps, but I want to total the steps for all walkers in a team to determine which team had the most. Everything I've tried gives me back 0 so nothing after that works. Any pointers would be much appreciated.

Just posted what I think is the relevant code.

Thank you.

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

int walkerSteps(string name, string member);

int main()
{
		
	ifstream inputFile; 
	inputFile.open("teams.txt"); 

	int totalSteps=0;
	while (inputFile >> teamName)  
	{
		cout << "\nTeam " << teamName << " enter your walking counts for: " << endl;
		int count = 0;
		int sum = 0;
		
		for (int i = 1; i < 3; i++) // Read the four team member's names
		{
			inputFile >> teamMember;
			cout << teamMember << endl;
			sum = walkerSteps(teamName, teamMember);
			
		}
	}
   
 cout << "The team with the most combined steps is " << teamName << " with "
		<< mostSteps << " total steps." << endl;
	
int teamAverage = totalSteps / 4;
cout << "Team " << teamName << " had an average of " << average << " steps per walker" << endl << endl;
	
system("pause");
	return 0;
}

/*  ************ Function Definitions ************** */

int walkerSteps(string name, string member)
{
	const int SIZE = 3; 
	int steps[SIZE]; 
	int total = 0; 
	int sum = 0;
	for (int count = 0; count < SIZE; count++)
	{
		cout << "Enter number of steps walked on day " << (count + 1) << ": ";
		cin >> steps[count];
		total += steps[count];
	}
	cout << "Total steps walked: " << total << endl;
	return sum = total;
}
You need a team method to call into the individual walker method. For example (I just substituted fstream with sstream for demo purposes), which you can test at https://repl.it/repls/WarpedTeemingGenres :

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
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int WalkerSteps(const string& name)
{
    const int SIZE = 3; 
    int sum = 0;
    int steps;
    for (int count = 0; count < SIZE; count++)
    {
        cout << "Enter number of steps "<< name << 
                " walked on day " << count + 1 << ": ";
        cin >> steps;
        sum += steps;
    }
    cout << "Total steps walked: " << sum << endl;
    return sum;
}

int TeamSteps(const string& members_line)
{
    string member;
    int team_total = 0;
    istringstream iss(members_line);
    while (iss >> member)
    {
        team_total += WalkerSteps(member);
    }
    return team_total;
}

int main() 
{
    const char* data_text = R"LITERAL(Ramblers Angie Peter Susan Fred
Trekkers John Adam Ann Sally
)LITERAL";

    // Imitate file stream
    istringstream iss(data_text);
    
    string members_line;
    int team_total;
    int best_team_total = -1;
    string team;
    string best_team = "";
    while (iss >> team)
    {
        cout << "Team "<< team << "!" << endl;
        getline(iss, members_line);
        team_total = TeamSteps(members_line);
        cout << "  total steps: " << team_total << endl;
        cout << "  average: " << team_total/4.0 << " per walker" << endl << endl;
        if (team_total > best_team_total)
        {
            best_team_total = team_total;
            best_team = team;
        }
    }
    
    cout << endl;
    cout << "Best team: " << best_team << " with total "<<best_team_total << endl;
    
    return 0;
}
Last edited on
Works like a charm. Thanks!
Topic archived. No new replies allowed.