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;
}