You've been hired by Text Turtles to write a C++
console application that determines the reading grade level of the
text in a file. The text comes from two files: SampleText1.txt and
SampleText2.txt. Sample text1= It has often been said
there's so much to be read
you never can cram
all those words in your head.
So the writer who breeds
more words than he needs
is making a chore
for the reader who reads.
That's why my belief is
the briefer the brief is
the greater the sigh
of the reader's relief is.
Sample text 2=
Four score and seven years ago our fathers brought forth
on
this continent, a new nation, conceived in Liberty, and
dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether
that nation, or any nation so conceived and so dedicated,
can long endure. We are met on a great battlefield of that
war. We have come to dedicate a portion of that field, as a
final resting place for those who here gave their lives
that that nation might live. It is altogether fitting and
proper that we should do this. But, in a larger sense, we
cannot dedicate, we cannot consecrate, we cannot hallow
this ground. The brave men, living and dead, who struggled
here, have consecrated it, far above our poor power to add
or detract. The world will little note, nor long remember
what we say here, but it can never forget what they did
here. It is for us the living, rather, to be dedicated here
to the unfinished work which they who fought here have thus
far so nobly advanced. It is rather for us to be here
dedicated to the great task remaining before us, that from
these honored dead we take increased devotion to that cause
for which they gave the last full measure of devotion, that
we here highly resolve that these dead shall not have died
in vain, that this nation, under God, shall have a new birth
of freedom, and that government of the people, by the
people, for the people, shall not perish from the earth.
Place the files where your app can find them. File SampleText1 will be used to test your app. File SampleText2 will be used for your final run.In your app, attempt to open the input file. Print an error message if the file doesn't open. Otherwise, read one line at a time from the file and keep count of the number of lines read. For each line read, loop through the line and count the number of:
● Alphanumeric characters – use function isalnum.
● Punctuation characters – use function ispunct.
● Spaces.
● Other characters – this should be zero.
● Number of sentences – this is the number of periods (.).
● Number of characters.
After reading the file, these counts will be for the entire file. Then calculate the following:
● Number of words – this is the number of spaces plus the number of sentences.
● Number of letters in words – this is the number of alphanumeric characters.
● Factor1 = number of letters in words / words * 100
● Factor2 = sentences / words * 100
● Reading level = (0.0588 * Factor1) – (0.296 * Factor2) – 15.8
Use formatted output manipulators (setw, left/right) to print the following rows:
● Alphanumeric characters.
● Punctuation characters.
● Spaces.
● Other characters.
● Number of characters.
● Number of sentences.
● Number of words.
● Number of letters in words.
● Factor 1.
● Factor 2.
● Reading level.
And two columns:
● A left-justified label.
● A right-justified value.
Define constants for the file names and column widths. Format all real numbers to one decimal place. For file SampleText1, the output should look like this:
Welcome to Text Turtles
-----------------------
Reading lines from file 'SampleText1.txt' ...
12 line(s) read from file 'SampleText1.txt'.
Alphanumeric chars: 225
Punctuation chars: 6
Spaces: 56
Other characters: 0
Total characters: 287
Sentences: 3
Words: 59
Letters in words: 225
Factor 1: 381.4
Factor 2: 5.1
Reading level: 5.1
End of Text Turtles
Use file SampleText2 for the final run pasted below.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string.h>
#define MAX 100
using namespace std;
/**
* Function to read file contents and store it in string array
* Parameters:
* fileName - name of the file
* data - Array of type string to store data read from the
file
* row - To store number of lines data read from file
* Returns number of rows using pass by reference
*/
void readFile(string fileName, string data[], int &row)
{
// Opens the file for reading
ifstream readFile;
readFile.open(fileName.c_str());
// Checks if the file unable to open for reading display's error
message and stop
if(!readFile)
{
cout<<"\n ERROR: Unable to open the file
"<<fileName<<" for reading.";
exit(0);
}// End of if condition
// Loops till end of the file
while(!readFile.eof())
{
// Reads a line from the file and stores it at row index
position
getline(readFile, data[row]);
// Increase the record counter by one
row++;
}// End of while loop
// Display number of lines read from file
cout<<"\n Reading lines from file
"<<fileName<<"....";
cout<<"\n "<<row<<" line(s) read from file
\'"<<fileName<<"\'.";
// Closer the file
readFile.close();
}// End of function
/**
* Function to to calculate and display number of alphanumeric,
punctuation, space, other character
* Number of sentences etc.
* Parameters:
* data - Array of type string having string data read from
file
* row - Number of lines
*/
void calculate(string data[], int row)
{
// Declares counters
int alphaNum, punc, space, other, sentence, totChar;
// Initializes counters
alphaNum = punc = space = other = sentence = totChar = 0;
// Loops till number of lines
for(int c = 0; c < row; c++)
{
// Loops till end of the line
for(int d = 0; d < data[c].length(); d++)
{
// Checks if current character is a alphabet
// then increase character counter
if(isalpha(data[c].at(d)))
totChar++;
// Checks if current character is a alphanumeric character
// then increase alphanumeric counter
if(isalnum(data[c].at(d)))
alphaNum++;
// Otherwise checks if current character is a punctuation
character
// then increase punctuation counter
else if(ispunct(data[c].at(d)))
punc++;
// Otherwise checks if current character is a space
character
// then increase space counter
else if(isspace(data[c].at(d)))
space++;
// Otherwise it is other character
// then increase other counter
else
other++;
// Checks if current character is a '.' character
// then increase sentence counter
if(data[c].at(d) == '.')
sentence++;
}// End of inner for loop
}// End of ouster for loop
// Calculates factors and reading level
double factor1 = (double)alphaNum / (space + sentence) * 100;
double factor2 = (double)sentence / (space + sentence) * 100;
double readingLevel = (0.0588 * factor1) - (0.296 * factor2) -
15.8;
// Displays summary
cout<<left<<setw(25)<<"\n Alphanumeric chars:
"<<right<<alphaNum;
cout<<left<<setw(25)<<"\n Punctuation chars:
"<<right<<punc;
cout<<left<<setw(25)<<"\n Spaces:
"<<right<<space;
cout<<left<<setw(25)<<"\n Other characters:
"<<right<<other;
cout<<left<<setw(25)<<"\n Total characters:
"<<right<<totChar;
cout<<left<<setw(25)<<"\n Sentences:
"<<right<<sentence;
cout<<left<<setw(25)<<"\n Words:
"<<right<<space + sentence;
cout<<left<<setw(25)<<"\n Letters in words:
"<<right<<alphaNum;
cout<<left<<setw(25)<<"\n Factor 1:
"<<right<<fixed<<setprecision(1)<<factor1;
cout<<left<<setw(25)<<"\n Factor 2:
"<<right<<fixed<<setprecision(1)<<factor2;
cout<<left<<setw(25)<<"\n Reading level:
"<<right<<fixed<<setprecision(1)<<readingLevel;
cout<<"\n End of Text Turtles";
}// End of function
// main function definition
int main()
{
// Declares an array of type string
string data[MAX];
// Stores the file name
string fileName = "SampleText1.txt";
// To store number of lines
int row = 0;
cout<<"\n Welcome to Text Turtles";
cout<<"\n -----------------------";
// Calls the function to read file
readFile(fileName, data, row);
// Calls the function to calculate
calculate(data, row);
cout<<endl<<endl;
// Resets the file name
fileName = "SampleText2.txt";
// Resets the line counter
row = 0;
// Calls the function to read file
readFile(fileName, data, row);
// Calls the function to calculate
calculate(data, row);
return 0;
}// End of main function
Sample Output:
Welcome to Text Turtles
-----------------------
Reading lines from file SampleText1.txt....
3 line(s) read from file 'SampleText1.txt'.
Alphanumeric chars: 225
Punctuation chars: 6
Spaces: 56
Other characters: 0
Total characters: 225
Sentences: 3
Words: 59
Letters in words: 225
Factor 1: 381.4
Factor 2: 5.1
Reading level: 5.1
End of Text Turtles
Reading lines from file SampleText2.txt....
25 line(s) read from file 'SampleText2.txt'.
Alphanumeric chars: 1149
Punctuation chars: 38
Spaces: 243
Other characters: 0
Total characters: 1149
Sentences: 10
Words: 253
Letters in words: 1149
Factor 1: 454.2
Factor 2: 4.0
Reading level: 9.7
End of Text Turtles
Get Answers For Free
Most questions answered within 1 hours.