In: Computer Science
Done in C++, 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.
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 File
Cincinnati over Buffalo by 3 Detroit over Cleveland by 14 Kansas City over Oakland by 17 Carolina over Minnesota by 25 Pittsburgh over NY Jets by 13 Philadelphia over Tampa Bay by 11 Green Bay over Baltimore by 2 St. Louis over Houston by 25 Denver over Jacksonville by 16 Seattle over Tennessee by 7 New England over New Orleans by 3 San Francisco over Arizona by 12 Dallas over Washington by 15 |
code in c++ (code to copy)
#include<bits/stdc++.h>
using namespace std;
int main()
{
//use ifstream to read from input.txt
ifstream infile("input.txt");
//use ofstream to write from input.txt
ofstream outfile("output.txt");
string buffer;
while(getline(infile, buffer)){
string teamA, teamB;
int goalsA, goalsB;
//get position of first digit
int pos=0;
while(buffer[pos]<'0'||buffer[pos]>'9'){
pos++;
}
// read first team in winteam
teamA = buffer.substr(0, pos-1);
//remove this from buffer
buffer = buffer.substr(pos);
pos=0;
//read position of comma
while(buffer[pos]!=','){
pos++;
}
//read goals by team A till comma
goalsA = stoi(buffer.substr(0, pos));
pos+=2;
//remove this from buffer
buffer = buffer.substr(pos);
pos=0;
//read till next digit
while(buffer[pos]<'0'||buffer[pos]>'9'){
pos++;
}
// read second team in winteam
teamB = buffer.substr(0, pos-1);
//remove this from buffer
buffer = buffer.substr(pos);
pos=0;
//read goals by team A till end of file
goalsB = stoi(buffer);
outfile<<teamA<<" over "<<teamB<<" by "<<(goalsA-goalsB)<<endl;
}
// close files
infile.close();
outfile.close();
}
code screenshot
input.txt screenshot
output.txt after execution
..