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;
}
Step-by-step 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
if(A-2>=B){
cout<<"Team A won! ("<<A<<"-"<<B<<")"; }
This checks if team B score at least 2 more than team A. If yes, it declares team B the winner
else if(B-2>=A){
cout<<"Team B won! ("<<A<<"-"<<B<<")";}
This checks for a draw
else if(B == A){
cout<<"Game ended as a draw. ("<<A<<"-"<<B<<")";}
This checks if both team are within 1 point of one another
else{
cout<<"Team A: "<<A<<" - Team B: "<<B;}
}
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
if(score == "A"){
teamA++;}
If score is B, then teamB' score is incremented by 1
else if(score == "B"){
teamB++;}
This prompts the user for score
cout<<"Score: "; cin>>score;
}
This calls the weHaveAWinner function at the end of the loop
weHaveAWinner(teamA,teamB);