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>
usingnamespace 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;
}
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.
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:
#include <iostream>
usingnamespace 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";
}
elseif ( playerTwo > playerOne && playerTwo > playerThree )
{
cout<<"PlayerTwo has the highest score\n";
}
else
{
cout<<"PlayerThree has the highest score\n";
}
return 0;
}
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?