Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment involves writing a C++ program to identify and count lines, word

ID: 3792311 • Letter: T

Question

This assignment involves writing a C++ program to identify and count lines, words, and characters. For the purpose of this assignment, we define a “word” as a sequence of characters that are not whitespace characters. Words are delimited by one or more whitespace characters. A whitespace character, such as a space, tab, or newline, is any character for which the function isspace() returns a value of true.

The operation of your program is controlled by the command line arguments that are passed to your program. Command line arguments consist of an optional set of flags arguments (a flag argument is an argument whose first character is a dash), followed by an optional list of filenames. If no filenames are specified, the program should read from standard input.

For each file specified (or, in the case of no file names specified, the standard input), the program must read the entire input and produce a count of characters, lines, and words. The output should be printed, one line per file, as follows:

Lines words characters filename

The numerical output (lines, words, characters) should be printed right justified in columns that are 12 characters wide. The filename should be printed left justified in the fourth column. In the case where the program in reading standard input, no filename should be printed.

If the program reads multiple files, then it should also provide a line at the end of the output with a total of all of the lines, words and characters in all of the files that were processed. The totals are tagged with the label “total”:

totalLines totalWords totalCharacters total

If the program is run with a flag in the form “-findchar=x”, then the program must also count the number of times it sees the character x in each file.

For example if I run the program by saying program -findchar=m infile1 infile2

Then the output might be: 10 200 1000 infile1

20 150 1010 infile2

30 350 2010 total

m: 3 20 100 infile1

m: 0 0 0 infile2

m: 3 20 100 total 1

This means that the letter m appears on 3 different lines in infile1, in 20 different words, for a total of 100 times

If the program is run with a flag in the form “-findword=thisword”, then the program must count the number of times it sees the word thisword in each file.

For example if I run the program by saying

program -findword=this infile1

Then the output might be:

10 200 1000 infile1

this: 7 12 infile1

This means that the word this appears on 7 different lines for a total of 12 times.

The program should accept multiple -findchar= arguments and multiple -findword= arguments, in any order. The output should be counts, then all findchar cases in alphabetical order, then all findword cases in alphabetical order The student will be provided with a test script, a set of test files, and a series of test cases. There are several error cases that must be handled:

Error Action A file is not found Print “File filename is not found” and stop

An argument is poorly formed (a known argument is missing the = sign, has nothing after the = sign, or (in the case of findchar, has more than one character after the =) Print “Argument arg is poorly formed” and stop

An argument is not recognized Print “Argument arg is not recognized” and stop A flag is duplicated Ignore the fact that a particular flag argument is duplicated.

Only process each letter or word one time no matter how many times it is specified on the command line A file name appears more than once The file should be processed each time the name appears in the list

Explanation / Answer

/*
Program to read class object of employee from the key board and to
store it on a specified file using write() member functions finally
reading all objects using read() member function

write((char *) &objectname,sizeof(objectname))
read((char *) &objectname,sizeof(objectname))
*/

#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 20

class Employee
{
   protected:
       int eno;
       char ename[20],job[15];
       double sal,comm,np;
   public:
       Employee()
       {
           comm=0;
           np=0;
       }
       void getData();
       void dispData();
       void findComm();
       void findNp();
};

void Employee :: getData()
{
   cout << "Eployee no: "; cin >> eno;
   cout << "Employee name: "; cin >> ename;
   cout << "Employee job: "; cin >> job;
   cout << "Employee sal: "; cin >> sal;
}

void Employee :: dispData()
{
   cout << "No: " << eno << endl;
   cout << "Name: " << ename << endl;
   cout << "Job: " << job << endl;
   cout << "Salary: " << setprecision(3) << sal << endl;
   cout << "Comission: " << setprecision(3) << comm << endl;
   cout << "Netpay: " << setprecision(3) << np << endl;
}

void Employee :: findComm()
{
   if(strcmp(job,"Manager")==0)
   comm=0;
   else
   comm=sal * float(0.12);
}

void Employee :: findNp()
{
   np = sal + comm;
}

void main()
{
   clrscr();
   char fname[12];
   int n,i;
   Employee e[MAX];
   fstream f;
   cout << " Enter the file name: ";
   cin >> fname;
   f.open(fname,ios::in,ios::out);
   cout << " Enter no of records: ";
   cin >> n;

   cout << " Enter the employees information..! ";
   for(i=0; i<n; i++)
   {
       cout << endl << "Record: " << (i+1) << endl;
       e[i].getData();
       e[i].findComm();
       e[i].findNp();
   }
   f.open(fname,ios::out);
   cout << " Writing to the file....! ";
   for(i=0;i<n;i++)
   {
       f.write((char *) &e[i],sizeof(e[i]));
   }
   cout << endl << n << " Records written sucessfully....!";
   f.close();
   clrscr();
   //Reading the records from the file using read() member function
   f.open(fname,ios::in);
   cout << " Employee list....! " << endl;
   for(i=0;i<n;i++)
   {
       f.read((char *) &e[i],sizeof(e[i]));
       if(f.eof())
       break;
       e[i].dispData();
       cout << endl;
       getch();
   }
   f.close();
   getch();
}