How to display first and last sentence in file

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;
}
Topic archived. No new replies allowed.