A file is downloaded and placed in your working directory.
Write a program asking the user for the name of the file in main.
If the file does not exist, the function should use a loop to allow the user
to enter the file name two more times before exiting. If the file is not found, display an appropriate message to the user and exit the program using return 0.
If the file is found, the program should display the contents of the file on the screen and write a copy of the file to a text file named newxxx.txt in your working directory. The xxx represents your initials. This is easiest done as the individual lines are read as input.
Each line of the screen output should be preceded with a line number, followed by a colon and a space.
If the file’s contents will not fit on a single screen, the program should display 20 lines of output at a time, and then pause. Do not use system(“pause”); but use a counter to pause instead. Each time the program pauses, it should wait for the user to strike any key before the next 20 lines are displayed.
Prior to exiting the program, display a message to the user which display the total number of lines in the file.
The file name is "Prog12" and it is a .txt file.
Here's what I have so far...
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
string filename;
//Get the filename from the user.
cout << "Enter the filename: ";
cin >> filename;
//Display an error message.
do
{
cout << "Error opening the file.\n";
cout << "Please re-enter the filename: ";
cin >> filename;
}
while (inputFile.fail());
//Open the file.
inputFile.open(filename.c_str());
//Close the file.
inputFile.close();
return 0;
}
The professor wants us to use a Do-While loop. How can this be done with a Do-While loop? I can't get my Do-While loop to work.
int attempts = 0;
bool success = false;
do
{
// get the filename and check if the file opens
// if not increment attempts else set success to true
}while (attempts < 3 && !success);