Help with C++ Program

1. File Previewer
Write a program that asks the user for the name of a text file. The program should display the first 10 lines of the file on the screen. If the file has fewer than 10 lines, the entire file should be displayed along with a message indicating the entire file has been displayed.
2. File Display Program
Write a program that asks the user for the name of a file. The program should display the contents of the file on the screen. If the file’s contents won’t fit on a single screen, the program should display 10 lines of output at a time and then pause. Each time the program pauses, it should wait for the user to type a key before the next 10 lines are displayed.

This is my cpp that needs to be needs changes.
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
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdio> //standard input output library
#include "Lab6_FileReader.h"
/*
Constructor
Read file and determine the number of records
*/
Lab6_FileReader::Lab6_FileReader(string _name)
{
	numrecords = 0;
	string oneRec;
	filename = _name;
	ifstream ifile(_name);

	if (!ifile)
	{
		cout << "File open failed..program terminating..\n";
		system("pause");
		exit(0);
	}

	while (getline(ifile, oneRec))
		numrecords++;

	ifile.close();
}
/*
return class private variable holding number of records
*/
int Lab6_FileReader::getNumRrecords()
{
	return numrecords;
}
/*
Display first 10 records with line numbers. If the file has fewer than 10 records,
display all records

*/
void Lab6_FileReader::displayFirst10records() //Complete this function
{
	cout << "\n" << filename << ":  FIRST 10 records in file \n\n";
}
void Lab6_FileReader::displayLast10records()//Complete this function
{
	cout << "\n" << filename << ":  LAST 10 records in file \n\n";
}
void Lab6_FileReader::displayAllRecords()
{
	ifstream ifile(filename);
	int displayed_lines = 1;
	string arec;

	cout << "\n" << filename << ": ALL records in the file with line numbers, 10 at a time\n\n";

	while (getline(ifile, arec))
	{
		if (displayed_lines % 10 == 0 && displayed_lines > 0)
			system("pause");

		cout << setw(2) << (displayed_lines + 1) << ".\t" << arec << endl;
		displayed_lines++;
	}

	cout << "\n\n-----------------------------------------\n\n";
	ifile.close();
}


This is my completed .cpp tester file.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
#include "Lab6_FileReader.h"

void display_file(string fname);
int main()
{
	display_file("Lab6_A.txt");
	display_file("Lab6_B.txt");

	system("pause");
	return 0;
}

void display_file(string fname)
{
	Lab6_FileReader myfile(fname);

	cout << "\n" << fname << " :  # of records in file = "
		<< myfile.getNumRrecords() << "\n";

	myfile.displayFirst10records();
	myfile.displayLast10records();
	myfile.displayAllRecords();
	system("pause");
}


This is my completed .h(header) file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
class Lab6_FileReader
{
private:
	string filename;
	int numrecords;
public:
	Lab6_FileReader(string _filename);
	void displayFirst10records();
	void displayLast10records();
	void displayAllRecords();
	int getNumRrecords();
};
Last edited on
Topic archived. No new replies allowed.