For this project you will write a complete C program that pe

<Project 7 Description>
For this project, you will write a complete C++ program that performs all of the following tasks.Remember, your program must contain appropriate comments in order to receive full credit:
(1) Prompt for, read, echo print and open a user defined input file. If the user inputs the name of a file thatwill not open, a loop is entered that prints out an appropriate error message, resets the input stream(see the end of the hints section), prompts for the name of the input file again and tries to open the newfile name. The loop is to continue executing until the user successfully enters the name of a file or typesctrl-c to terminate the program.(2) Print out a 5 column heading for the data to be printed(3) Read every character (tabs, spaces and new-lines are characters) on a line-by-line basis from the inputfile and count them as indicated:

a. Count the number of characters on a line that are letters onlyb. Count the number of characters on a line that are digits onlyc. Count the number of other characters that are neither a letter or a digit

(4) Output the character counts on a line by line basis (counting stops for a line when the new line characteris encountered - be sure to count the new line character)(5) After reading all lines, output a total line for each column of data printed. Below this line, print out a lineindicating the percent (shown to two decimal places) of the total characters that each columnrepresents(6) The columns of information printed are: Line number, number of letters, number of digits, number ofother characters and the total number of characters for the line.
Your output is to be identical to the outputof the sample solution (i.e. 5 columns).

Solution

#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;

int main(){
   string fileName;

   cout << \"Input file name . \ \"; // OUTPUT promt


   std::ifstream file;


   file.exceptions(std::ifstream::failbit | std::ifstream::badbit);

   while(1){
       cin >> fileName;   // INPUT file name


       try {
           file.open(fileName); // OPEN file
       }
       catch (std::system_error& e) {
           std::cerr << e.code().message() << std::endl; // Catch Error
           continue;
       }

       char charRead;

       while(file.get(charRead)){

           int countDigits = 0; // Initialise variables
           int countLetters = 0;
           int countSymbols = 0;

           int lineNo = 1;


           cout << \"Line, Letters, Digits, Symbols, Total\" << endl;

           cout << lineNo++;


           while(1){
               if(isalnum(charRead)) {    // Check alpha, digit
                   if(isdigit(charRead))
                       countDigits++;
                   if(isalpha(charRead))
                       countLetters++;
               } else countSymbols++;


               file.get(charRead);

               if(charRead == \'\ \'){ // Print sums;
                   int total = countLetters + countDigits + countSymbols;
                   cout << \" \";
                   cout << countLetters;
                   cout << \" \";
                   cout << countDigits;
                   cout << \" \";
                   cout << countSymbols;
                   cout << \" \";
                   cout << total << endl;
                  
                   double lPercent = ((countLetters * 100) / total);
                   double dPercent = ((countDigits * 100) / total);
                   double sPercent = ((countSymbols * 100) / total);

                   cout << std::fixed;
               std::cout << std::setprecision(2);

                   cout << \" \" << lPercent << \" \" << dPercent << \" \" << sPercent << endl;
                   break;
               }
           }

           if(file.eof())         // check if endstream reached
               break;
       }

       break;

   }


}

<Project 7 Description> For this project, you will write a complete C++ program that performs all of the following tasks.Remember, your program must conta
<Project 7 Description> For this project, you will write a complete C++ program that performs all of the following tasks.Remember, your program must conta

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site