structures

for some reason im getting the total by each player only and not adding the player scored for a total in (line 48 - 53)

also, trying to get the highest points scored by the player and display the player name and jersey number

any help would be thoughtful


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 "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

struct PlayerInfo
{
	char name[10];
	double playerID;
	double points;
};

int main()
{
	const int SIZE = 3;
	PlayerInfo soccer[SIZE];

	cout << "Enter the player information below\n";
	cout << "----------------------------------\n";
	
	for( int i = 0; i < SIZE; i++)
	{
		cout << "\nEnter the player #" << ( i + 1 ) << " name: ";
		cin >> soccer[i].name;
		
		cout << "Enter the player jersey number: ";
		cin >> soccer[i].playerID;

		while ( soccer[i].playerID < 0 )
		{
			cout << "Please enter a number greater than zero: ";
			cin >> soccer[i].playerID;
		}
		
		cout << "Enter the points scored by player #" << ( i + 1 ) << ": ";
		cin >> soccer[i].points;

		while ( soccer[i].points < 0 )
		{
			cout << "Please enter a number greater than zero: ";
			cin >> soccer[i].points;
		}
	}
		
	//cout << "Total points combine is: \n";
	//cout << fixed << showpoint << setprecision(2);

	for ( int i = 0; i < SIZE; i++ )
	{
		double total = 0;
		total = soccer[i].points + total;
		
		cout << "Total points combine for the team: " << total << endl;
	
		// this is where i try to find the highest points scored by a player
		// and try to display his jersey number and name
		int max;
		max = soccer[i].points;
		for ( int j = 0; j < SIZE; j++ )
		{
			if (soccer[i].points > max )
			max = soccer[i].points;
			cout << "The player that scored that highest points is: " << max << endl;
		}
	}
	return 0;
}
Last edited on
I think you should declare total outside the loop or declare it as static double
jrock,

This should work, though it's not great. If you have two players with the same score, both names will come up.

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
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>

struct PlayerInfo
{
	char name[10];
	double playerID;
	double points;
};

double total = 0;

int main()
{
	const int SIZE = 3;
	PlayerInfo soccer[SIZE];

	std::cout << "Enter the player information below\n";
	std::cout << "----------------------------------\n";

	for(int i = 0; i < SIZE; i++)
	{
		std::cout << "\nEnter player number " << (i + 1) << " name: ";
		std::cin >> soccer[i].name;

		std::cout << "\nEnter the player jersey number: ";
		std::cin >> soccer[i].playerID;

		while( soccer[i].playerID < 0 )
		{
			std::cout << "Please enter a number greater than zero: ";
			std::cin >> soccer[i].playerID;
		}

		std::cout << "\nEnter the points scored by player #" << (i+1) << ": ";
		std::cin >> soccer[i].points;

		while(soccer[i].points < 0)
		{
			std::cout << "Please enter a number greater than zero: ";
			std::cin >> soccer[i].points;
		}

	}

	//std::cout << "Total points combined are: \n";
	//std::cout << fixed << showpoint << setprecision(2);

	for(int i = 0; i < SIZE; i++)
	{
		total = soccer[i].points + total;

	}

	std::cout << "The total points combined for the team: " << total << std::endl;

	int max = 0;
	for(int i = 0; i < SIZE; i++)
	{
		if(soccer[i].points > max)
		{
			max = soccer[i].points;
		}
	}

	for(int i = 0; i < SIZE; i++)
	{
		if(soccer[i].points == max)
		{
			std::cout << "The player that scored the highest points is: " << soccer[i].name << std::endl;
		}
	}
	return 0;
}
Topic archived. No new replies allowed.