Question

You've been hired by Text Turtles to write a C++ console application that determines the reading...

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.

Homework Answers

Answer #1

#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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: d. def count_words(text): """ Count the number of words in text """ return 0 e. def words_per_sentence(text): return 0.0 f. def word_count(text, punctuation=".,?;"): """ Return a dictionary of word:count...
Using the string below to make a list of words, show that there are 272 words...
Using the string below to make a list of words, show that there are 272 words in the Gettysburg Address. How many distinct words are in the speech? Hint: You may need to delete some of the punctuation, including new lines, which are represented by \n. In [63]: gettysburg_address = """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....
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
This is C. Please write it C. 1) Prompt the user to enter a string of...
This is C. Please write it C. 1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Write a program in python that reads in the file quiztext (txt) and creates a list...
Write a program in python that reads in the file quiztext (txt) and creates a list of each unique word used in the file, then prints the list. Generate a report that lists each individual word followed by the number of times it appears in the text, for example: Frequency_list = [('was', 3), ('bird',5), ('it', 27)….] and so on. Note: Notice the structure: a list of tuples. If you're really feeling daring, Google how to sort this new list based...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...