Using C++, Redo the following parsing program for the football scores using only
ID: 3666216 • Letter: U
Question
Using C++, Redo the following parsing program for the football scores using only <string> or <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;
}
}
Input file :
Cincinnati 27, Buffalo 24
Detroit 31, Cleveland 17
Kansas City 24, Oakland 7
Carolina 35, Minnesota 10
Pittsburgh 19, NY Jets 6
Philadelphia 31, Tampa Bay 20
Green Bay 19, Baltimore 17
St. Louis 38, Houston 13
Denver 35, Jacksonville 19
Seattle 20, Tennessee 13
New England 30, New Orleans 27
San Francisco 32, Arizona 20
Dallas 31, Washington 16
Output looks Like :
Cincinnati over Buffalo 27 to 24
Detroit over Cleveland 31 to 17
Kansas City over Oakland 24 to 7
Carolina over Minnesota 35 to 10
Pittsburgh over NY Jets 19 to 6
Philadelphia over Tampa Bay 31 to 20
Green Bay over Baltimore 19 to 17
St. Louis over Houston 38 to 13
Denver over Jacksonville 35 to 19
Seattle over Tennessee 20 to 13
New England over New Orleans 30 to 27
San Francisco over Arizona 32 to 20
Dallas over Washington 31 to 16
Explanation / Answer
The above code contains <string> and <cctype> functions. It works fine.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.