Question

In: Computer Science

Done in C++, Write a program to read the input file, shown below and write out...

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

Solutions

Expert Solution

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

..


Related Solutions

Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write a C++ program that does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT