win counter and list every winner in each game
May 12, 2022 at 2:10pm UTC
I'm creating a program that has four games. Each game will have two team. In each game, there would be 3 rounds. Should there be a tie, there would be round 4 where the first team to get 10 points will be the winner.
this is my code:
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
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
constexpr size_t games {4};
constexpr size_t round {3};
string winners[games];
for (size_t j {}; j < games; ++j) {
string t1, t2;
int p1 {}, p2 {};
size_t t1abc {}, t2abc {};
cout << "GAME " << j + 1 << '\n' ;
cout << "Input Team 1 name: " ;
cin >> t1;
cout << "Input Team 2 name: " ;
cin >> t2;
cout <<'\n' ;
for (size_t v {1}; v <= round; ++v) {
cout << "Round " << v << ":\n" ;
cout << "\tTeam " << t1 << " Points: " ;
cin >> p1;
cout << "\tTeam " << t2 << " Points: " ;
cin >> p2;
cout << "\t________________________\n" ;
if (p1 > p2) {
cout << "Winner: Team " << t1 << '\n' << endl;
++t1abc;
} else if (p1 < p2) {
cout << "Winner: Team " << t2 << '\n' << endl;
++t2abc;
} else
cout << "NO WINNER!\n" ;
}
cout << "Team " << t1 << ": " << t1abc << " WIN(S)\n" ;
cout << "Team " << t2 << ": " << t2abc << " WIN(S)\n" ;
cout << "\nGame " << j + 1;
if (t1abc > t2abc) {
cout << " Winner Team " << t1 << '\n' ;
winners[j] = t1;
} else if (t2abc > t1abc) {
cout << " Winner Team " << t2 << '\n' ;
winners[j] = t2;
} else {
cout << " Draw!\n" ;
winners[j] = "Draw" ;
}
cout << "================================================================\n" << endl;
}
cout << "\nSUMMARY OF WINNERS:\n" ;
for (size_t i {}; i < games; ++i)
cout << "Game " << i + 1 << ": " << winners[i] << '\n' ;
}
Last edited on May 13, 2022 at 5:06am UTC
May 12, 2022 at 2:25pm UTC
Is there a question or would just like your code reviewed?
May 12, 2022 at 2:48pm UTC
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
1) I don't see any games to play.
2) Can the team names change for each game? If not, then prompting for the team names should be outside your loop.
3) If a game is a tie, you declare "NO WINNER" instead of playing another round.
Last edited on May 12, 2022 at 3:08pm UTC
May 12, 2022 at 4:08pm UTC
As formatted:
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
#include <iostream>
using namespace std;
int main() {
constexpr size_t games {4};
constexpr size_t round {3};
std::string winners[games];
for (size_t j {}; j < games; ++j) {
string t1, t2;
int p1 {}, p2 {};
size_t t1abc {}, t2abc {};
cout << "GAME " << j + 1 << '\n' ;
cout << "Input Team 1 name: " ;
cin >> t1;
cout << "Input Team 2 name: " ;
cin >> t2;
cout << '\n' ;
for (size_t v {1}; v <= round; ++v) {
cout << "Round " << v << ":\n" ;
cout << "\tTeam " << t1 << " Points: " ;
cin >> p1;
cout << "\tTeam " << t2 << " Points: " ;
cin >> p2;
cout << "\t________________________\n" ;
if (p1 > p2) {
cout << "Winner: Team " << t1 << "\n\n" ;
++t1abc;
} else if (p1 < p2) {
cout << "Winner: Team " << t2 << "\n\n" ;
++t2abc;
} else
cout << "NO WINNER!\n" ;
}
cout << "Team " << t1 << ": " << t1abc << " WIN(S)\n" ;
cout << "Team " << t2 << ": " << t2abc << " WIN(S)\n" ;
cout << "\nGame " << j + 1;
if (t1abc > t2abc) {
cout << " Winner Team " << t1 << '\n' ;
winners[j] = t1;
} else if (t2abc > t1abc) {
cout << " Winner Team " << t2 << '\n' ;
winners[j] = t2;
} else {
cout << " Draw!\n" ;
winners[j] = "Draw" ;
}
cout << "================================================================\n\n" ;
}
cout << "\nSUMMARY OF WINNERS:\n" ;
for (size_t i {}; i < games; ++i)
cout << "Game " << i + 1 << ": " << winners[i] << '\n' ;
}
May 12, 2022 at 4:19pm UTC
Should there be a tie, there would be round 4 where the first team to get 10 points will be the winner
As the points are entered and not obtained from play, this is pretty meaningless. What if points entered for both teams are both the same and at least 10? That is a draw again. What if both points entered are less than 10?
Wouldn't it be better to have additional rounds until there is a round winner?
May 13, 2022 at 5:12am UTC
thank you!
Last edited on May 17, 2022 at 7:02am UTC
May 13, 2022 at 5:24am UTC
How do put the round 4 when there's a tie between 2 teams?
May 13, 2022 at 9:00am UTC
Move lines 24-41 to a function, then call that function if you need to for round 4.
May 13, 2022 at 9:15am UTC
What if round 4 is also a draw - or no score entered is at least 10? Is then then a round 5, 6 ... etc until a winner emerges with at least 10 points?
May 13, 2022 at 11:29am UTC
As a first refactor, consider something like this which has extra rounds following round 3 if a draw until there is a winner. Note that it ignore the 'at least 10 points' criteria for the extra round(s) and just goes on a straight win:
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 75 76 77 78
#include <iostream>
enum PLAYERS: size_t { FIRST, SECOND, NOPLAYS };
bool doround(size_t round, const std::string t[2], size_t tabc[2]) {
int p[NOPLAYS] {};
std::cout << "Round " << round << ":\n" ;
for (size_t ply {}; ply < NOPLAYS; ++ply) {
std::cout << "\tTeam " << t[ply] << " Points: " ;
std::cin >> p[ply];
}
std::cout << "\t________________________\n" ;
if (p[FIRST] == p[SECOND])
return (std::cout << "NO WINNER!\n" ), false ;
const auto win {(p[SECOND] > p[FIRST])};
std::cout << "Winner: Team " << t[win] << "\n\n" ;
++tabc[win];
return true ;
}
int main() {
constexpr size_t games {4};
constexpr size_t round {3};
std::string winners[games];
for (size_t j {}; j < games; ++j) {
std::string t[NOPLAYS];
size_t tabc[NOPLAYS] {};
std::cout << "GAME " << j + 1 << '\n' ;
for (size_t n {}; n < NOPLAYS; ++n) {
std::cout << "Input Team " << n + 1 << " name: " ;
std::cin >> t[n];
}
std::cout << '\n' ;
for (size_t v {1}; v <= round; ++v)
doround(v, t, tabc);
for (bool again {true }; again; ) {
again = false ;
for (size_t n {}; n < NOPLAYS; ++n)
std::cout << "Team " << t[n] << ": " << tabc[n] << " WIN(S)\n" ;
std::cout << "\nGame " << j + 1;
if (tabc[FIRST] == tabc[SECOND]) {
std::cout << " Draw!\n" ;
again = true ;
size_t extra {round + 1};
do {
std::cout << "Team " << t[0] << " and team " << t[1] << " will have round " << extra << '\n' ;
} while (!doround(extra++, t, tabc));
} else {
const auto wint {tabc[SECOND] > tabc[FIRST]};
std::cout << " Winner Team " << t[wint] << '\n' ;
winners[j] = t[wint];
}
}
std::cout << "================================================================\n\n" ;
}
std::cout << "\nSUMMARY OF WINNERS:\n" ;
for (size_t i {}; i < games; ++i)
std::cout << "Game " << i + 1 << ": " << winners[i] << '\n' ;
}
Topic archived. No new replies allowed.