Write a c++ program for the World Bowling Competition. An input file, score.txt, contains the name of each player, the name of the player's team and each player's individual score. There will only be two teams(Blue and White). Read the data from the input file into three parallel arrays.The program must accumulate the total score for each team and then output the name of the winning team and the player's names and score of the winning team. Assume there is no chance of a tie. Use functions to:
1) Read the data from the input file into parallel arrays,
2) Determine the winning team,
3) Display the winning team's player names and scores
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include<vector>
using namespace std;
/*
Write a c++ program for the World Bowling Competition. An input file, score.txt,
contains the name of each player, the name of the player's team and each player's individual score.
There will only be two teams(Blue and White). Read the data from the input file into three parallel arrays.
The program must accumulate the total score for each team and then output the name of the winning
team and the player's names and score of the winning team. Assume there is no chance of a tie. Use functions to:
1) Read the data from the input file into parallel arrays,
2) Determine the winning team,
3) Display the winning team's player names and scores
*/
#define NO_OF_PLAYERS 3
int Score_Blue[NO_OF_PLAYERS],Score_White[NO_OF_PLAYERS];
string Name_Blue[NO_OF_PLAYERS],Name_White[NO_OF_PLAYERS];
string S;
vector<string> k;
#define MAX_ITEMS 3
vector<string> splitString(string str, string delimiter)
{
vector <string> result;
size_t pos=0;
string token;
while((pos = str.find(delimiter))!= std::string::npos)
{
token = str.substr(0,pos);
result.push_back(token);
str.erase(0,pos+delimiter.length());
}
if(!str.empty()) result.push_back(str);
return(result);
}
int main()
{
char x;
int n=0,i,Flag=1,a=0,b=0,TotalBlue=0,TotalWhite=0;
string Name1 = "G:\\Score.txt";
string z,str;
std::ifstream infile(Name1.c_str());
if (!infile)
{
cout << "Unable to open file: "<<Name1;
exit(1); // terminate with error
}
else
{
cout<<"\n\nInput File Contents:\n";
while (std::getline(infile, str))
{
z = "";
if(str.size() > 0) z = z + str;
k = splitString(&z[0]," ");
if(k[1] == "blue")
{
Name_Blue[a] = k[0];
Score_Blue[a] = atoi(k[2].c_str());
TotalBlue = TotalBlue+Score_Blue[a];
a++;
}
if(k[1] == "white")
{
Name_White[b] = k[0];
Score_White[b] = atoi(k[2].c_str());
TotalWhite = TotalWhite+Score_White[b];
b++;
}
n++;
}
infile.close();
}
cout<<"\n\tTeam Blue Score Card:";
for(n=0;n<a;n++)
{
cout<<"\n\tPlayer-"<<n+1<<". "<<Name_Blue[n]<<"\tScore: "<<Score_Blue[n];
}
cout<<"\n\t\tTotal Score (Blue Team): "<<TotalBlue;
cout<<"\n\n\tTeam White Score Card:";
for(n=0;n<a;n++)
{
cout<<"\n\tPlayer-"<<n+1<<". "<<Name_White[n]<<"\tScore: "<<Score_White[n];
}
cout<<"\n\t\tTotal Score (White Team): "<<TotalWhite;
if(TotalWhite>TotalBlue) cout<<"\n\n\tTeam WHITE is the WINNER.";
if(TotalWhite<TotalBlue) cout<<"\n\n\tTeam BLUE is the WINNER.";
if(TotalWhite==TotalBlue) cout<<"\n\n\tThere is a TIE between teams WHITE and BLUE.";
return(0);
}
Contens of Score.txt File:
abc blue 23
lmn blue 34
xyz blue 78
uvw white 87
cde white 65
ijk white 45
Comments
Leave a comment