Question: Write a program that allows the user to navigate through the lines of text in any text file. The program prompts the user for a

Write a program that allows the user to navigate through the lines of text in any text file. The program prompts the user for a filename and copies the lines of text from the file into a list. The program then prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the text in that line number.

Input validation: You must print a friendly error message and allow the user to try again if the program cannot open the requested file, and whenever the user enters an invalid line number.

Here is a sample run (user input was changed to red for improved readability):

Enter the input file name: quotes.xtxt

I cannot open the file quotes.xtxt - Please check the name and try again.

Enter the input file name: quotes.txt The file has 16 lines. Enter a line number [0 to quit]: 1

1 : Hakuna Matata!

Enter a line number [0 to quit]: -1

Try again. Line number must be between 0 and 16

Enter a line number [0 to quit]: 16

16 : Just keep swimming!

Enter a line number [0 to quit]: 17

Try again. Line number must be between 0 and 16

Enter a line number [0 to quit]: 5

5 : It's kind of fun to do the impossible.

Enter a line number [0 to quit]: A

Try again. Please enter a number between 0 and 16

Enter a line number [0 to quit]: $

Try again. Please enter a number between 0 and 16

Enter a line number [0 to quit]: 4

4 : To infinity and beyond!

Enter a line number [0 to quit]: 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!