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 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...
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...
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...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
Must be done in C Write a C program found out the maximum value between the...
Must be done in C Write a C program found out the maximum value between the three numbers? Show steps with comments or else it will be flagged.
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT