I wonder if anyone can help me. I'm suppose to write a program to ask the user to enter the file they want to display and display the file they want 24 lines at a time. It pauses at 24 and asks them to press enter and displays the next 24 lines. I have a pretty good code I think for a beginner, but no matter what file I enter when I test the program it goes to the error checker and says it couldn't open the file. I will attach my code. If anyone has any tips I would appreciate it. Thanks.

//This program will write to a file and then
//ask the user to recall that file and display.
#include <iostream>
#include <fstream>
using namespace std;

//Constant for array size
const int SIZE = 100;

int main()
{
	char MyFile[SIZE];  //Hold the file name
	char inputLine[SIZE];     //To hold the line of input
	fstream address;   //File stream object
	int line = 0;       //Line counter
	char ch;            //To hold a character

	address.open("address.txt", ios::out);
	address<<"Anthony Smith "<< endl;
	address.close();


	//Get the file name.
	cout<< " Enter the name of the file you are";
	cout<< " interested in.\n";
	cout<<" ";cin.getline(MyFile, SIZE);

	//Open the file.
	address.open(MyFile);

	//Test for errors
	if(!address)
	{
		cout<<" There was an error opening "<< MyFile << endl;
		exit(EXIT_FAILURE);
	}
	else;
	{
		address.get(ch);
	}

	//Read the contents of file and display
	while(!address.eof())
	{
		//Get a line from the file.
		address.getline(inputLine,SIZE,'\n');


		//Display the line.
		cout<< inputLine << endl;

		//Update line counter
		line++;

		//Display 24 lines at a time.
		if(line == 24)
		{
			cout<< " Press enter to continue.\n";
			cin.get();
			line = 0;
		}
	}

	//Close file.
	address.close();
	return 0;
}

After closing the file on line 20 you need to clear fstream's error before opening it again. On line 21 add address.clear(); If that does not fix the problem then more than likely the file is not where you think it is. If it's not in the current working directory then you have to specify the full path to it.

Ok, I've figured out how to display the file that the user asks for. But when they enter the wrong file it tells them that there was an error opening the file that they choose. Does anyone know how to make it keep looping the question until they enter the right file. I know how to make a loop with numbers but not with text variables. Thank you for everyone's help.

Ok, I've figured out how to display the file that the user asks for. But when they enter the wrong file it tells them that there was an error opening the file that they choose. Does anyone know how to make it keep looping the question until they enter the right file. I know how to make a loop with numbers but not with text variables. Thank you for everyone's help.

Here's a BIG hint:
"Does anyone know how to make it keep looping the question until they enter the right file." And while we're looking at code, nice formatting. It's good to see someone takes pride in their code.

Two things -- Line 24, split those two commands onto separate lines. And use 4 SPACEs instead of TABs for your indents. Eventually your code will look too horizontal by using TABs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.