Question

In: Computer Science

For the following code I need to open the file of a persons choice which I...

For the following code I need to open the file of a persons choice which I did. I can get it to read the file, but I need help with counting lines and characters feel like i have the code I just cant make it work.

Either way, the program should then ask the user Analyze another file (y/n)? and repeat the process again (asking for a file and analyzing its meta-data). This should continue repeating until the user chooses n for not analyzing another file.

#include <iostream>

#include <fstream>

using namespace std;

int main() {

  int lines, chars;

  string file, text;

  fstream reader;

  char choice;

  string line;

  int count = 0;

  string L;

  

  do{

    cout << "File to open:\n";

    getline(cin, file);

    

  ifstream ReadFile;

  ReadFile.open(file);

  if(ReadFile.fail())

  {

  // output after failing at opening file

    cout << "Clould not open file." << endl;

  }

  else

  {

  for(string words; getline(ReadFile, words, '.'); ) //read sentences including

  //spaces

  cout<<words; //Display sentences

  

  while(  getline (ReadFile, line)){    //gets the # of chars

             cout<<line<<endl;

              chars++;

    }

  while ( getline (file, L))

        count++;

  // output after analyzing a file

    cout << "Lines: " << L << endl;

    cout << "Characters: " << chars << endl;

  

  

      }

  ReadFile.close();

    // prompt user whether they want to repeat

    cout << "Analyze another file (y/n)?" << endl;

    cin >> choice;

    cin.ignore();

  }while( choice==y);

  

  return 0;

}

Solutions

Expert Solution

Code:

#include <iostream>

#include <fstream>

#include<string>

using namespace std;

int main() {

char ch;

string file,line;

fstream reader;

char c;

ifstream ReadFile;

do{

int count_line = 0,count_word=0;

cout << "File to open:\n";

fflush(stdin); //used to flush the output buffer of the stream

getline(cin, file);

ReadFile.open(file.c_str());

if(ReadFile.fail()) // output after failing at opening file

{

cout<<"Clould not open file."<<endl;

}

else

{

while (getline(ReadFile, line))

count_line++; //count number of lines

ReadFile.clear(); //return file pointer to first position of the file

ReadFile.seekg(0);

while (1)

{

ReadFile.get(c);

if (ReadFile.eof()) break;

count_word++; //count number of charecter including space

}

ReadFile.close(); //close the file

}

// output after analyzing a file

cout << "Lines: " << count_line << endl;

cout << "Characters: " << count_word << endl;

cout << "Analyze another file (y/n)?" << endl; // prompt user whether they want to repeat

cin>>ch;

}while(ch=='y');

return 0;

}

Testing file:

Output:


Related Solutions

Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice). This program behaves similarly to the AddValueToArray program from before. However, instead of modifying the array in-place, it will return a new array holding the modification. The original array does not change. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output with the command-line argument 3 is shown below: Original array: 3...
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice)....
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write: swapRows: Swaps the contents of two rows, given a two-dimensional array and the indices of the rows to swap. swapCols: Swaps the contents of two columns, given a two-dimensional array and the indices of the columns to swap. main contains some code which will call swapRows and swapCols, for the purpose of informal...
Download the Take.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Take.java file, and open it in jGrasp (or a text editor of your choice). This program will “take” a given number of elements from a given array, using the command-line arguments as an array, and the hard-coded value of 3 elements to take. Example output of this program with the command-line arguments foo bar baz is shown below: foo bar baz In the above example, three elements were taken. However, it's possible that we may request to take...
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, converts them to integers, and then determines if they all have the same value. An example run of the program is below, using the command-line arguments 1 2 3: Are equal: false A further example is shown below, with the command-line arguments 2 2: Are equal: true In the event that the program is given...
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table: Compass Direction Integer ID North 0 Northeast 1 East 2 Southeast 3 South 4 Southwest 5 West 6 Northwest 7 Further details are in the comments of Compass.java. import java.util.Random; import java.util.Scanner; public...
i need a javafx code 1. first create menu bar with option open and save 2....
i need a javafx code 1. first create menu bar with option open and save 2. when user click on open it opens the file only image file 3. when user click on save it saves as new file.
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
Persons insured under Section I of the HO-3 policy include which of the following? I. The...
Persons insured under Section I of the HO-3 policy include which of the following? I. The named insured's mother-in-law, who lives in the same household as the insured. II. A 16-year-old Italian exchange student, who is residing with the named insured while attending high school in the United States.
I would need both SAS & R code, the output and .txt file/s, please. Because many...
I would need both SAS & R code, the output and .txt file/s, please. Because many HMOs either do not cover mental health costs or provide only minimal coverage, min- isters and priests often need to provide counseling to persons suffering from mental illness. An in- terdenominational organization wanted to determine whether the clerics from different religions have different levels of awareness with respect to the causes of mental illness. Fifteen clerics from different Christian denominations were sampled. Each was...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT