This program is suppose to do the following:
- read a file using fstream
- display each sentence on the screen
- display the first and last sentence in the file (don't forget to add the period back in b/c getline will remove it)
- count the number of words in the file
- display the number of times 'the' is in the program
Based on what I currently have I am not sure how to display the first and last sentence.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
/////////////////////////////
// prototypes
////////////////////////////
int showSentence();
int showStats();
//This program reads data from a file called 'lab3.txt'
//and echoes them to the display until the end of file flag is reached
int main()
{
//show the sentences from the file
showSentence();
//show the statistics from the file
showStats();
return 0;
}
//////////////////////////////////
// function definitions
/////////////////////////////////
int showSentence()
{
ifstream indata; //indata is used like cin
string sentence;
indata.open("lab3.txt"); //open the file for input
if (!indata) //test to make sure the file opened
{
cerr << "Error: File could not be opened" << endl;
return 1;
}
while (!indata.eof() ) //keep reading until end-of-file
{
//cout << num;
getline(indata, sentence, '.');
cout << sentence;
}
cout << endl << endl;
indata.close(); //close the input file
return 0;
}
int showStats()
{
//variables
ifstream indata; //indata is used like cin
string word;
int countWord = 0;
int countThe = 0;
//display title
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
cout << " STATISTICS " << endl;
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
cout << endl;
indata.open("lab3.txt"); //open the file for input
if (!indata) //test to make sure the file opened
{
cerr << "Error: File could not be opened" << endl;
return 1;
}
while (indata >> word)
{
cout << word;
cout << endl;
//count the number of times 'the' shows in file
//if (word == 'the' || word == 'The')
//{
//countThe++;
//}
}
cout << "Number of Words: " << countWord << endl;
//cout << "Number of time 'the' showed up in the file" << countThe << endl;
indata.close(); //close the input file
return 0;
}