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

please I really need a help with this question. please don\'t answer if your ans

ID: 3651194 • Letter: P

Question

please I really need a help with this question. please don't answer if your answer does nothing with mine.

Write a C++ program, which prints number of lines,words and characters from an input text file. We define a word as number of character devided by one or more a white spaces.
A white space is contains one of the following newline, carriage return, tab or space (' ', ' ', ' ', ' '),
A character is any character includin white space.
The program shall promt the user for the name of input and output file.
The program writes the result to the screen and to the output file in the
following way:
The input contains:
xx lines
yy words
zz characters
Observe:
1) Devide the program in to subtasks.
2) Implement each suptask as a function. The main program is mostly
function calls. .
3) Write comments in the code.

Explanation / Answer

#include<iostream>
#include<string>
#include<fstream>

using namespace std;
//uses/makes deletemeAll.dat,deletemeA.dat,deletemeB


void create(){//testing purposes

string a="The Star Jane Taylor Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like a diamond in the sky. ";
string b=" When the blazing sun is gone, When he nothing shines upon, Then you show your little light, Twinkle, twinkle, all the night. ";
string c=" Then the traveller in the dark, Thanks you for your tiny spark: He could not see which way to go, If you did not twinkle so. ";
string d=" In the dark blue sky you keep, And often through my curtains peep, For you never shut your eye Till the sun is in the sky. ";
string e=" As your bright and tiny spark Lights the traveller in the dark, Though I know not what you are, Twinkle, twinkle, little star.";//some text


ofstream dat;
dat.open("deletemeAll.dat");
if(dat.is_open()){
cout<<"Making input files ";

dat<<a<<b<<c<<d<<e;
dat.close();
dat.open("deletemeA.dat");
dat<<a;
dat.close();
dat.open("deletemeB.dat");
dat<<a<<b;
}
else
cout<<" input file not made ";
return;
}
void ckprevious(char a, int arr[]){
if((a==' '||a==' ')){//if a tab or spac
arr[1]++;//increment word count
}
else if(a==' '||a==' '){//if a newline or return
arr[1]++;//increment word count
arr[0]++;//increment line count
}
else
arr[2]++; //increment char count
return;
}
string read(int Counts[]){
//sets all of count elements to 0 (just incase you call this fxn multiple times or forgot to restart in main)
Counts[0]=0;
Counts[1]=0;
Counts[2]=0;

ifstream infile; //delcaring inputting stream


string inputFile,outputFile;//declare two string variables
char character,cprevious;//character is for holding the char it recieves from reading infile

GetName://label

//prompting for input and output file names
cout<<" Enter the name of the input file: ";
getline(cin,inputFile);
cout<<"Enter the name of the output file: ";
getline(cin,outputFile);

//opening the file into input stream
infile.open(inputFile);


if(infile.is_open()){//if inputFile opened
cout<<" File opened successfully";
cprevious=infile.get();
ckprevious(cprevious,Counts);

while(infile.good()){//loop til extraction from good fails
character=infile.get();
//line, word, char

if((character==' '||character==' ')&& (cprevious!=' ' && cprevious!=' ')){//if a tab or space and not a multiple of tab/space
Counts[1]++;//increment word count
}
else if((character==' '||character==' ')){//if a newline or return
if((cprevious!=' ' && cprevious!=' '))//if not multiples of newlines or returns, is a word
Counts[1]++;//increment word count
Counts[0]++;//increment line count
}
else
Counts[2]++; //increment char count

cprevious=character;
}//end while good
//Counts[2]--;//decrement one for eof character

infile.close();//close infile file

}//end if open OK

else{
cout<<" Input File "<<inputFile<<" failed to open. Check file name and try again ";// if file did not open
goto GetName;//go to GetName label
}
return outputFile;
}

void print(string outputFile,int info[]){
ofstream outfile;//declaring outputting stream
outfile.open(outputFile);
outfile<<endl<<info[0]<<" lines "<<info[1]<<" words "<<info[2]<<" characters ";
cout<<info[0]<<" lines "<<info[1]<<" words "<<info[2]<<" characters ";
outfile.close();//close outfile file
return;
}
int main(){
string printinto;
int info[3]={0};//integer array of 3 elements all initialized to 0;
//let info[0]=linescount,info[1]=wordcount, info[2]=charcount

create();//creates testing input file
printinto=read(info);
print(printinto,info);

cout<<" Enter any key to exit . . .";
cin.get();
return 0;

}