Answer:
The program is as follows:
#include<iostream>
using namespace std;
void weHaveAWinner(int A, int B){
if(A-2>=B){
cout<<"Team A won! ("<<A<<"-"<<B<<")"; }
else if(B-2>=A){
cout<<"Team B won! ("<<A<<"-"<<B<<")";}
else if(B == A){
cout<<"Game ended as a draw. ("<<A<<"-"<<B<<")";}
else{
cout<<"Team A: "<<A<<" - Team B: "<<B;}
}
int main() {
int teamA = 0; int teamB = 0;
string score;
cout<<"Score: "; cin>>score;
while(score != "end"){
if(score == "A"){
teamA++;}
else if(score == "B"){
teamB++;}
cout<<"Score: "; cin>>score;
}
weHaveAWinner(teamA,teamB);
return 0;
}
Explanation:
This defines the weHaveAWinner function, which receives the scores of both teams as its parameters
void weHaveAWinner(int A, int B){
This checks if team A score at least 2 more than team B. If yes, it declares team A the winner
<em> if(A-2>=B){
</em>
<em> cout<<"Team A won! ("<<A<<"-"<<B<<")"; }
</em>
This checks if team B score at least 2 more than team A. If yes, it declares team B the winner
<em> else if(B-2>=A){
</em>
<em> cout<<"Team B won! ("<<A<<"-"<<B<<")";}
</em>
This checks for a draw
<em> else if(B == A){
</em>
<em> cout<<"Game ended as a draw. ("<<A<<"-"<<B<<")";}
</em>
This checks if both team are within 1 point of one another
<em> else{
</em>
<em> cout<<"Team A: "<<A<<" - Team B: "<<B;}
</em>
<em>}
</em>
The main begins here
int main() {
This declares and initializes the score of both teams to 0
int teamA = 0; int teamB = 0;
This declares score as string
string score;
This prompts the user for score
cout<<"Score: "; cin>>score;
This loop is repeated until the user inputs "end" for score
while(score != "end"){
If score is A, then teamA' score is incremented by 1
<em> if(score == "A"){
</em>
<em> teamA++;}
</em>
If score is B, then teamB' score is incremented by 1
<em> else if(score == "B"){
</em>
<em> teamB++;}</em>
This prompts the user for score<em>
</em>
cout<<"Score: "; cin>>score;
}
This calls the weHaveAWinner function at the end of the loop
weHaveAWinner(teamA,teamB);