In: Computer Science
C++ program, I'm a beginner so please make sure keep it simple
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.txt:
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
//c++ program to read txt file
#include <Bits/stdc++.h>
#include <cstring>
using namespace std;
int main()
{
const char file_name[] = "test.txt";//give location of your txt file
char buffer[16], winner[32], loser[32]; //variables are defined .
int winning _score, lossing_score;
ifstream fin(file_name);//conditions if file dosent open the it will give error message :Unable to open file
if (!fin)
{
cout<< "Unable to open file " << file_name << endl;
exit(1);
}
while (!fin.eof())
{
fin >> winner;
if (fin.eof()) break;
fin >> buffer;
if (strchr(buffer,','))
{
winning _score = atoi(strtok(buffer,","));
}
else
{
strcat(winner," ");
strcat(winner,buffer);
fin >> buffer;
winning _score = atoi(strtok(buffer,","));//atoi( ) function hepls to convert string to integer.
}
fin >> loser;
fin >> buffer;
if (isdigit(buffer[0]))//it wii get digits from the txt file.
{
lossing_score = atoi(buffer);
}
else
{
strcat(loser," ");//strcat( ) function will help to append the copy of the string to end of the string
strcat(loser,buffer);
fin >> buffer;
lossing_score = atoi(buffer);
}
cout << winner << " over " << loser << ' ' << " by " <<winning _score - lossing_score << endl;
}
return 0;
}
Here the output screen:
if you have any problem regarding this question then feel frere ro ask in comment section.