Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++, Redo the following parsing program for the football scores using only

ID: 3666396 • Letter: U

Question

Using C++, Redo the following parsing program for the football scores using only <string>OR (not both) <cctype> functions for the parsing.

#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;

int main()
{
const char filename[] = "C:/Users/Desktop/scores.txt";
char buffer[16], winner[32], loser[32];
int winnerScore, loserScore;

ifstream fin(filename);
if (!fin)
{
cerr << "Unable to open file " << filename << endl;
exit(1);
}

while (!fin.eof())
{
// get winner
fin >> winner;
if (fin.eof()) break;

// get next token. Could be score or 2nd part of winner name
fin >> buffer;

// If buffer has a comma, then it's a score
if (strchr(buffer,','))
{
winnerScore = atoi(strtok(buffer,","));
}
else
{
strcat(winner," ");
strcat(winner,buffer);
fin >> buffer;
winnerScore = atoi(strtok(buffer,","));
}

// get loser
fin >> loser;

// get next token. Could be score or 2nd part of loser name
fin >> buffer;

// If first digit is numeric, then it's a score
if (isdigit(buffer[0]))
{
loserScore = atoi(buffer);
}
else
{
strcat(loser," ");
strcat(loser,buffer);
fin >> buffer;
loserScore = atoi(buffer);
}
cout << winner << " over " << loser << ' ' << winnerScore << " to " << loserScore << endl;
}
}

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string.h>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
string filename = "C:/Users/Desktop/scores.txt";
string buffer, winner, loser;
int winnerScore, loserScore;
ifstream fin(filename.c_str());
if (!fin)
{
cerr << "Unable to open file " << filename << endl;
exit(1);
}
while (!fin.eof())
{
// get winner
fin >> winner;
if (fin.eof()) break;
// get next token. Could be score or 2nd part of winner name
fin >> buffer;
// If buffer has a comma, then it's a score
if (buffer.find(','))
{
int pos=buffer.find(',')-1;
winnerScore = atoi(buffer.substr(0,pos));
}
else
{
winner="";
winner.append(buffer);
fin >> buffer;
winnerScore = atoi(strtok(buffer,","));
}
// get loser
fin >> loser;
// get next token. Could be score or 2nd part of loser name
fin >> buffer;
// If first digit is numeric, then it's a score
if (isdigit(buffer[0]))
{
loserScore = atoi(buffer);
}
else
{
loser="";
loser.append(buffer);
fin >> buffer;
loserScore = atoi(buffer);
}
cout << winner << " over " << loser << ' ' << winnerScore << " to " << loserScore << endl;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote