File Previewer

I am working on this program, but it seems it is not working...it makes sense for me so I do not know what I am doing wrong, I will appreciate some help figuring out what is wrong and how to fix it.

The program has to ask the user for a file name and display the first 10 lines then if there are less than 10 lines it should display the entire file with a message indicating that the entire file has been displayed.

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

int main()
{
	//Variables
	ifstream file;
	string fileName;
	string line;
	int counter = 0;
	
	//1. Ask user for the name of a text file
	cout << "Please enter the name of the text file you want to open: " << endl;
	while (true)
	{
	getline(cin, fileName);
	file.open(fileName.c_str());
		//Check if file open was successful
		if (!file)
		{
			cout << "Error: cannot open the file " << fileName << endl;
			exit(1);
		}
	}

	//2. Display the first 10 lines
	while (getline(file, line))
	{
		counter++;
		if (counter > 1 && counter < 10)
		{
			cout << line << endl;
			cout << "The file has less than 10 lines." << endl;
			cout << "The entire file has been displayed!" << endl;
		}
		else if (counter > 1 && counter > 10)
		{
			cout << line << endl;
		}
		else
		{
			break;
		}
	}

	//3. CONDITION: 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.


	system("pause");
	return 0;
}
Last edited on
Where does the loop that begins on line 16 and ends on line 26 get left? In other words, how is it possible to get to line 29?
I think I see what you say...

I tried this:


1
2
3
4
5
6
7
8
9
10
//1. Ask user for the name of a text file
	cout << "Please enter the name of the text file you want to open: " << endl;
	getline(cin, fileName);
	file.open(fileName.c_str());
		//Check if file open was successful
		if (!file)
		{
			cout << "Error: cannot open the file " << fileName << endl;
			exit(1);
		}
The problem seems to be in my while loop, but I am not sure how to fix it....
The getline never reaches the while loop....

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
int main()
{
	//Variables
	ifstream file;
	string fileName;
	string line;
	int counter = 0;
	
	//1. Ask user for the name of a text file
	cout << "Please enter the name of the text file you want to open: " << endl;
	getline(cin, fileName);
	file.open(fileName.c_str());

	//2. Display the first 10 lines
	while (getline(file, line))
	{
		counter++;
		//Check if file open was successful
		if (!file)
		{
			cout << "Error: cannot open the file " << fileName << endl;
			exit(1);
		}
		else if (counter > 1 && counter < 10)
		{
			cout << line << endl;
			cout << "The file has less than 10 lines." << endl;
			cout << "The entire file has been displayed!" << endl;
		}
		else if (counter > 1 && counter > 10)
		{
			cout << line << endl;
		}
		else
		{
			break;
		}
	}

	//3. CONDITION: if the file has fewer than 10 lines the entire file
	//should be displayed along with a message indicating the entire file
	//has been dipalyed.


	system("pause");
	return 0;
}
Topic archived. No new replies allowed.