0% found this document useful (0 votes)
1 views

Lab_4_4_Using_the_if_Statement_with_Input_and_Output_Failures

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab_4_4_Using_the_if_Statement_with_Input_and_Output_Failures

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 4.

4 Using the if Statement with Input and Output Failures

Attempting to read invalid data causes the input stream to enter a fail state. Once an input
stream enters a fail state, all subsequent input statements associated with that input stream
are ignored, and the computer continues to execute the program, which produces
erroneous results. Other than reading invalid data, two common causes of input failure are:

• Attempting to open an input file that does not exist


• Attempting to read beyond the end of an input file

You can use the if statement to check the status of an input stream variable. If the input
stream enters the fail state, you also can include instructions that stop program execution.

When you use the input stream variable as an expression in an if statement, it evaluates to
true if the last input succeeded and to false if the last input failed. For example, if
(cin) or if (infile) evaluates to true if the last input from the input device
succeeded.

If an input fails, a message can be displayed, and a return statement is generated with the
value of 1 included to terminate the program. A return value 0 indicates that the program
ended correctly.

Objectives
In this lab, you evaluate the standard input device and an ifstream variable for input
failure. If failure occurs, the programs terminate.

After completing this lab, you will be able to:


• Use the if statement to check for input failure.
• Evaluate the standard input variable for input failure.
• Evaluate the ifstream variable for input failure.
• Evaluate the ofstream variable for output failure.

Estimated completion time: 40–45 minutes


Lab 4.4 Steps: Using the if Statement with Input and Output
Failures
In this lab, you design and write a program that tests for input failure.

1a. Design a program that prompts the user to enter his or her age and first initial. Include
code that checks for input failure in case the user enters character data for numeric
data. If input failure occurs, display a message and return the value 1 from your main
function. If no input failure occurs, display a message that echoes the data entered.

Write your design in the following space. Your design should be a list of C++ comments
without any code.
1b. Write a C++ program based on the design you created in Exercise 1a and name it
failure.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

In the following space, show what is displayed on the screen after executing the output
message.

1c. Enter, compile, link, and execute failure.cpp. Then copy the output and save it in a block
comment at the end of your program. Save failure.cpp in the Chap04 folder of your
Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.
Please enter your age and first initial: 19 J
Your age is 19 and your initial is J.

Please enter your age and first initial: J 19


You did not enter your age followed by your initial.
2a. Design a program that prompts the user to create a name and address file from a name,
street address, city, state, and zip code entered. You should create the name and
address file on a drive with a removable disk. Check for output failure in case the user
does not have access to the drive (for example, if the disk is not inserted in the drive). If
output failure occurs, display a message indicating the problem; otherwise, display a
message indicating that the program ended successfully.

Write your design in the following space. Your design should be a list of C++ comments
without any code.
2b. Write a C++ program based on the design you created in Exercise 2a and name it
noDisk1.cpp. Step through the code by hand. Specify that the output file is named
address.txt.

Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when
Initialized

In the following space, show what is displayed on the screen after executing the output
message.

2c. Enter, compile, link, and execute noDisk1.cpp. Execute the program twice, once with no
disk in the drive and once with a disk in the drive. After your program is executed, copy
the output and save it in a block comment at the end of your program. Save noDisk1.cpp
in the Chap04 folder of your Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.

Cannot open output file. The program terminates.

Please enter a name: Judy Shaffer


Please enter a street address: 774 S. Main St.
Please enter a city, state, and zip code: Austin, Texas
78736

Your program ended successfully.


3a. Create the design for a program that reads the address.txt file that you created in
Exercise 2 from drive A (or another drive with a removable disk). Read in the name,
street address, city, state, and zip code from address.txt, and display this information on
the screen. Check for input failure in case the user does not have access to the drive (for
example, if the disk is not inserted in the drive). If input failure occurs, display a message
indicating the problem.

Write your design in the following space. Your design should be a list of C++ comments
without any code.
3b. Write a C++ program based on the design you created in Exercise 3a and name it
noDisk2.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code is
executed. To fill out the memory table, use two lines for each variable. On one line,
enter declaration information. Write the name of the declared variable, its data type,
and the line number at declaration.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

In the following space, show what is displayed on the screen after executing the output
message.

3c. Enter, compile, link, and execute noDisk2.cpp. Execute the program twice, once with no
disk in the drive and once with a disk in the drive. After your program is executed, copy
the output and save it in a block comment at the end of your program. Save noDisk2.cpp
in the Chap04 folder of your Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered. Recall that the input comes from address.txt.

Either:
Judy Shaffer
774 S. Main St.
Austin, Texas 78736

Cannot open input file. The program terminates.

You might also like