File Display Program

I have a program challenge for homework that asks the user for the name of a file. The program should display the contents of the file on the screen. If the files contents wont fit on a single screen, the program should display 24 lines of output at a time, and then pause. Each time the program pauses, it should wait for the user to strike a key before the next 24 lines are displayed.

The function expects a declaration and I have a feeling something else is wrong that I can't see. Any help is greatly appreciated.

Here's what I have so far:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void showContent(fstream &);

int main(void)
{
	string fileName;
	char line[100];
	fstream fin;
	fstream dataFile;

	cout << "Enter Filename: " << endl;
	cin >> fileName;

	fin.open(fileName, ios::in);
	dataFile.close();

	showContent(dataFile);

	while(!fin)
	{
		cout << "Error Opening File! Re-enter File Name: " << endl;
		cin >> fileName;
	}

	system("pause");
}

void showContent(fstream &file);
{
	int counter = 0;
	char getch();

	while(!fin.eof())
	{
		if (counter > 25)
		{
			getch();
			counter = 0;
		}

		fin.getline(line, 101, '\n');
		cout << line << endl;
		counter++;
	}
}
you cannot use a local variable (like line within main()) in another function (like showContent()). Just create a new variable within showContent()
Why do you have two fstream objects? Why do you close datastream without ever having opened it?

while(!fin)
{
cout << "Error Opening File! Re-enter File Name: " << endl;
cin >> fileName;
}


Then, what will you do if the comparison expression (fin == 0) is always true?
"cin >> fileName;" seems redundant because it cannot solve the problem (fin == 0).
Loop forever.........

Edit :

showContent(dataFile);

while(!fin)
{
cout << "Error Opening File! Re-enter File Name: " << endl;
cin >> fileName;
}

...is bad code logic. It should be changed to :


while(!fin)
{
cout << "Error Opening File! Re-enter File Name: " << endl;
//Reenter the file name then try again...
}

showContent(dataFile);
Last edited on
Topic archived. No new replies allowed.