Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C + + program whose main function is merely a collection of variable declarations and function calls. This program reads in text and

Write a C++ program whose main function is merely a collection of variable declarations and function calls. This program reads in text and outputs the letters, together with their counts. You are not allowed to use global variables. All information must be passed in and out of the functions. You will use an array to keep a count for all of the letters. Both the input file and output file must be given by the user. Your program will be broken up into 4 functions, in four different files. Because they are in different files, you are being forced to not use global variables. One of the files is given to you (main.cpp).
You will be given 3 other files, that are all .hpp files. Your job is to write the 3 functions, openFiles, count, and printResults in the three different files.
InputOutput openFiles()
Opens the input and output files. It will throw an error if something is wrong. There are three edge cases:
If it there are an incorrect amount of input parameters, it will throw an IncorrectParameters error.
If it fails to open the input file, throw a InvalidInputFile error
It if fails to open the output file, throw an InvalidOutputFile error
count()
Counts every occurrence of capital letters A-Z and small letters a-z in the text file opened in the function openFile.
printResult()
Prints the number of capital letters and small letters, as well as the percentage of both. Don't include non letters in the count for getting the percentage
The format of each line needs to line up to the right
Few other key logistics:
The program takes in two command line arguments, input and output
Example[x].txt corresponds with output[x].txt
There are 4 test cases you are not given, but the are described in the rubric.
A computer is grading this. Just as compilers are unforgiving, so too will the computer be unforgiving.
You get half credit for windows, and half credit for linux. If it doesnt compile on linux, you will get no credit for that half of the assignment.
Dont commit anything other than the .cpp or .cc files
Example: Consider a file with content as below:
C++ is an object oriented programming language. It used to be called c with classes. Soon we will make a class for our programming assignment. We turn every assignment in with git. I wish that Linux Torvalds could have made it more user friendly.
Here is the output file generated by the code:
Letter Count Percentage
A 00.00%
B 00.00%
C 10.51%
D 00.00%
E 00.00%
F 00.00%
G 00.00%
H 00.00%
I 21.02%
J 00.00%
K 00.00%
L 10.51%
M 00.00%
N 00.00%
O 00.00%
P 00.00%
Q 00.00%
R 00.00%
S 10.51%
T 10.51%
U 00.00%
V 00.00%
W 10.51%
X 00.00%
Y 00.00%
Z 00.00%
a 168.16%
b 21.02%
c 63.06%
d 73.57%
e 2010.20%
f 21.02%
g 9 The main.cpp file is #include
#include "count.hpp"
#include "openFiles.hpp"
#include "printResults.hpp"
int main(int argc, char* argv[])
{
try
{
auto io = a3::openFiles(argc, argv);
std::array counts = a3::count(io.fin);
a3::printResults(io.fout, counts);
}
catch (a3::IncorrectParameters& ex)
{
std::cerr << ex.what();
return ex.getCode();
}
catch (a3::InvalidInputFile& ex)
{
std::cerr << ex.what();
return ex.getCode();
}
catch (a3::InvalidOutputFile& ex)
{
std::cerr << ex.what();
return ex.getCode();
}
return 0;
} The openFiles.hpp file is #ifndef OPEN_FILES_HPP
#define OPEN_FILES_HPP
#include
namespace a3
{
class IncorrectParameters : public std::runtime_error
{
public:
IncorrectParameters() : std::runtime_error("Invalid input parameters"){};
int getCode(){ return 1; }
};
class InvalidInputFile : public std::runtime_error
{
public:
InvalidInputFile() : std::runtime_error("Invalid input file"){};
int getCode(){ return 2; }
};
class InvalidOutputFile : public std::runtime_error
{
public:
InvalidOutputFile() : std::runtime_error("Invalid output file"){};
int getCode(){ return 3; }
};
struct InputOutput
{
InputOutput(std::ifstream& in, std::ofstream& out)
: fin(std::move(in)), fout(std::move(out))
{
}
std::ifstream fin;
std::ofstream fout;
};
InputOutput openFiles(int argc, char* argv[]);
}// namespace a3
#endif The printResults.hpp file is #ifndef PRINT_RESULTS_HPP
#define PRINT_RESULTS_HPP
#include
#include "count.hpp"
namespace a3
{
void printResults(std::ofstream& fout,

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions