Sie sind auf Seite 1von 3

School of Engineering, California Baptist University

Lab 3: Interactive I/O and File I/O


Due: Wednesday, October 5th, 2011

1 Objectives
In this lab, you will investigate how interactive I/O works and how to change the I/O to be from/to files.

2 Interactive I/O
Open Microsoft Visual C++ 2008, create a new project called Frame1 (use the same configuration as lab #1). Enter the following program to your project as Frame1.cpp. Use this program for exercises 1-3.
// Program Frame1 prompts the user to input values representing // the dimensions of a print. The amount of wood needed for // the frame is calculated and printed on the screen. #include <iostream> using namespace std; int main() { int side; int top; int inchesOfWood;

// Vertical dimension in inches // Horizontal dimension in inches // Inches of wood needed

cout << "Enter the vertical dimension of your print." << endl; cout << "The dimension should be in whole inches. " << "Press return." << endl; cin >> side; cout << "Enter the horizontal dimension of your print." << endl; cout << "The dimension should be in whole inches. " << "Press return." << endl; cin >> top; inchesOfWood = top + top + side + side; cout << "You need " << inchesOfWood << " inches of wood." << endl; return 0; }

Exercise 1. Read the program and answer the following questions before you run the program. a) b) How many lines of input does program Frame1 expect? What happens if you key both values on the same line with a blank between them?

EGR 121 Introduction to Computer Programming in C++, Fall 2011

School of Engineering, California Baptist University

c)

What happens if you key both values on the same line without a blank between them?

Exercise 2. Compile and run the program. Verify your answers in Exercise 1. Show your results in the lab report.

Exercise 3. Key in floating-point values as side and top of the print. What happens? Make changes in the program Frame1 such that you can key in floating-point values and get a correct result that is displayed as a floating-point value. Make sure to update the prompts accordingly. Include your code and testing results in the lab report.

3 File I/O
Create a new project called Frame2 (use the same configuration as lab #1). Enter the following program to your project as Frame2.cpp. Use this program for exercises 4-5.
// Program Frame2 reads input values that represent the dimensions // of a print from a file and calculate the amount of wood needed for // the frame. #include <iostream> #include <fstream> using namespace std; int main() { ifstream inFile; inFile.open("Frame.in"); int side; // Vertical dimension in inches int top; // Horizontal dimension in inches int inchesOfWood; // Inches of wood needed inFile >> side >> top; cout << "Dimensions are " << top << " and " << side << "." << endl; inchesOfWood = top + top + side + side; cout << "You need " << inchesOfWood << " inches of wood." << endl; return 0; }

Exercise 4. Create a data file in the same directory as program Frame2, and name it Frame.in. Make the values in the file consistent with the input statements. Run program Frame2. Show the contents in your Frame.in file
EGR 121 Introduction to Computer Programming in C++, Fall 2011

School of Engineering, California Baptist University

and the output of your program in the lab report. Exercise 5. Change program Frame2 so that the output goes to a file named Frame.out. You must perform the following tasks: Declare outFile to be of type ofstream. Invoke function open to associate the internal name outFile and the external name Frame.out. Send the output to steam outFile.

Include your code and the contents of the files Frame.in and Frame.out in your lab report.

EGR 121 Introduction to Computer Programming in C++, Fall 2011

Das könnte Ihnen auch gefallen