Video Game Scores

I'm pretty new to c++ and yes this is an assignment but don't worry I am not asking you to do it for me. I just need help figuring out a few things. I was tasked with creating a program that asks for the scores of three gamers then takes these scores and outputs the highest. I THINK I have the other parts down but I don't know how to get the program to compare the scores and output the highest without having to write a long list of commands comparing playerone to playertwo, then player one to playerthree, then player two to playerone, etc.

Thanks for any help you can give.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <iostream>
using namespace std;

int main ()
{
	int playerOne, playerTwo, playerThree;
	cout << "Please enter score for Player One: ";
	cin >> playerOne;
	cout << "Please enter score for Player Two: ";
	cin >> playerTwo;
	cout << "Please enter score for Player Three: ";
	cin >> playerThree;
	if (
	cout << "" << ;
	cout << "" << ;
	return 0;
}
The way I immediately think of doing it I will only post here for completeness, NOT because you should do it this way (Your marker will probably know its not your code).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>

int main() {
    int players[3];
    std::cout << "Please enter score for Player One: ";
    std::cin >> players[0];
    std::cout << "Please enter score for Player Two: ";
    std::cin >> players[1];
    std::cout << "Please enter score for Player Three: ";
    std::cin >> players[2];

    std::cout << "The largest score was " << std::max_element(players, players+3) << std::endl;

    return 0;
}


EDIT: typo
Last edited on

I THINK I have the other parts down but I don't know how to get the program to compare the scores and output the highest without having to write a long list of commands comparing playerone to playertwo, then player one to playerthree, then player two to playerone, etc.

Be sure, it is not "long list of commands" - you need only two comparisons for two players:
compare first and second and store their max in temporary variable
compare their max with third player and print out which is bigger.

Here are few tasks at my problem collection which are dedicated to this problem:
http://codeabbey.com/index/task_view/min-of-two - minimum of two
http://codeabbey.com/index/task_view/min-of-three - minimum of three

When you have more players, you need the same algorithm for selecting maximum of the array or the sequence. Here you can have such practice on the set of few hundreds numbers:

http://codeabbey.com/index/task_view/maximum-of-array

Hope this could give you a clue...
you could use else - if statements to perform the comparison


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
#include <iostream>

using namespace std;

int main()
{
	int playerOne, playerTwo, playerThree;
	cout<<"Enter the score of playerOne\n";
	cin>>playerOne;
	cout<<"Enter the score of playerTwo\n";
	cin>>playerTwo;
	cout<<"Enter the score of playerThree\n";
	cin>>playerThree;

	if ( playerOne > playerTwo && playerOne > playerThree )
	{
		cout<<"PlayerOne has the highest score\n";
	}
	else if ( playerTwo > playerOne && playerTwo > playerThree )
	{
		cout<<"PlayerTwo has the highest score\n";
	}
	else
	{
		cout<<"PlayerThree has the highest score\n";
	}
	return 0;
}
Last edited on
closed account (D80DSL3A)
Recursive approach?
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
#include <iostream>

int findMax( int* pInt, unsigned sz )
{
    if( sz > 1 )
    {
        int maxOthers = findMax( pInt+1, sz-1 );
        return *pInt > maxOthers ? *pInt : maxOthers;
    }
    return *pInt;
}

int main() {
    int players[3];
    std::cout << "Please enter score for Player One: ";
    std::cin >> players[0];
    std::cout << "Please enter score for Player Two: ";
    std::cin >> players[1];
    std::cout << "Please enter score for Player Three: ";
    std::cin >> players[2];

    std::cout << "The largest score was " << findMax(players, 3) << std::endl;

    return 0;
}
Recursive approach?


Wise approach, but i highly doubt he heard of recursions yet.
closed account (D80DSL3A)
@Uk marine. I doubt it too. I just had an idle moment and thought I'd add to the growing list of solution methods here.

Your solution is probably the most appropriate for him.
Thank you so much for all the help! I can see now I was overthinking things a bit lol
Every time I run the code after having saved it and enter the information the screen autocloses after I enter the information and it displays the highest winner. How can I prevent that?
At the end of your program, just before the "return 0" statement, try putting this in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <limits>

using namespace std;

int main() {

    // ...

    cin.sync();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();

    return 0;
}
Last edited on
Cool thanks for everything guys you were a big help :)
Topic archived. No new replies allowed.