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

C++: only use Strings in this question. Write a program to read the input file,

ID: 3602513 • Letter: C

Question

C++: only use Strings in this question.

Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. The idea of the program is to read the text file into 4 variables and rearrange the output. please explain as you go through.

here is the start code i wrote (change if needed that okey):

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

main()
{
string team,team2;
string score,score2;
ifstream myfile("input.txt");
/*if (myfile.is_open())
{
while ( getline )
{
cout << city << score<< ' ';
}
myfile.close();
}

else cout << "Unable to open file";
*/
return 0;

}


Input File


Output 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

Explanation / Answer

The complete working code in c++ for the above problem :

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

unsigned int split(const std::string &txt, std::vector<std::string> &strs, char ch)
{
   unsigned int pos = txt.find(ch);
   unsigned int initialPos = 0;
   strs.clear();

   // Decompose statement
   while (pos != std::string::npos) {
       strs.push_back(txt.substr(initialPos, pos - initialPos + 1));
       initialPos = pos + 1;

       pos = txt.find(ch, initialPos);
   }

   // Add the last one
   strs.push_back(txt.substr(initialPos, std::min(pos, txt.size()) - initialPos + 1));

   return strs.size();
}

int main()
{
   string team, team2;
   string score, score2;
   string line;
   ifstream myfile("input.txt");
   ofstream outputFile("output.txt");

   vector<string> words;

   if (myfile.is_open())
   {
      while (getline(myfile, line) )
   {
          words.clear();
          split(line, words, ' ');
          if (words.size() == 4)
          {
              words[1].erase(2,1);
              outputFile << words[0] << "over " << words[2] << " " << words[1] << "to " << words[3] << endl;
          }
   }
   myfile.close();
   }
   else cout << "Unable to open file";
   outputFile.close();
   return 0;

}

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