Week 7: Lab

profileMr. Goodman
CIS170C_W7_Lab_Instructions_V2.docx

devry-u-rgb_logo-small

Student Lab Activity

CIS170 Week 7 Lab Instructions

Lab 7 of 7: Sequential Files

Lab Overview—Scenario/Summary

You will code, build, and execute a program that creates an address database using a sequential text file in Comma Separated Value (CSV) format.

Learning Outcomes

1. Continue using a menu system with a console application.

2. Read and write a sequential text file.

3. Generate output records in CSV format.

4. Parse CSV formatted input records and display the results.

Deliverables

Section

Deliverable

Points

Lab

Program Listing and Output

· Accepts names and addresses from console

· Writes addresses to text file

· Reads and displays addresses from file

· Provides menu options for entry, display, and exit

· Code is provided in lab report

· Screen shots of working program provided in lab report

· Code is appropriately commented, formatted, and readable

5

5

5

5

5

5

10

All Steps

Total

40

Lab Steps

Preparation:

If you are using the Citrix remote lab, follow the login instructions located in the lab area in Course Home.

Locate the Visual Studio icon and launch the application.

Lab:

Step 1: Revjew the Requirements for an Address Record Application

Review and analyze the following requirements for a C++ console application that will store and retrieve names and addresses in a text file.

The program should do the following.

1. Display a main menu providing the user with options to 1) Enter new address records; 2) Display address records; 3) Exit the program.

2. If the user selects 1) Enter new address records:

a. Create a new text file named Address.csv

b. Prompt the user to enter a name, street, city, state, and zip code.

c. Write the entered data to the text file in Comma Separated Value (CSV) format.

d. Ask if the user wants to enter another record. If so, repeat; if not, close the file and return to the main menu.

3. If the user selects 2) Display address records:

a. Open the text file named Address.csv

b. Read and display the name, street, city, state, and zip code from each record in the file in a readable format.

c. Close the file and return to the main menu.

4. If the user selects 3) Exit: Exit the program.

Step 2: Create project and enter skeleton code

a. In Visual Studio, create a new C++ console application project named Lab7 using the "empty project" option.

b. Create a new C++ source file in the project named Lab7.cpp.

c. Enter or copy/paste in the following skeleton code, which you will build upon to complete the rest of the assignment:

// ---------------------------------------------------------------

// Programming Assignment: CIS170C Lab 7

// Developer: ______________________

// Date Written:

// Purpose: Store and retrieve names and addresses in a

// sequential text file in CSV format.

// ---------------------------------------------------------------

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

// function prototypes

void EnterAndWriteAddresses(void); //Accept address records from user, write to file

void ReadAndDisplayAddresses(void); //Read address records from file, display

const string ADDRESS_FILE_NAME = "Address.csv"; // file name for address data file

// main program -- displays main menu and processes user selection

void main()

{

cout << "Address Record Application" << endl << endl;

//TODO: Add main menu loop here with options for

// 1) Enter new address records

// 2) Display address records

// 3) Exit the program

// Display error message if invalid choice is entered

// Repeat loop while user has not entered 3

system("pause");

}

// Enter address records and write to address file

void EnterAndWriteAddresses(void)

{

//TODO: Declare variables for name and address fields

//TODO: Open address file for output using ofstream()

//TODO: Loop to accept and write addresses

// Prompt user to enter name and address fields

// Write entered data to address file separated by commas

// Ask user if more addresses to enter

// Repeat loop while user responds 'Y' or 'y'

//TODO: Close address file

}

// Read records from address file and display to user

void ReadAndDisplayAddresses(void)

{

string name, street, city, state, zip;

ifstream inputAddress(ADDRESS_FILE_NAME); // open address file for input

if (inputAddress.is_open()) {

//read and display all records from address file

while (getline(inputAddress, name, ',')) { //read name field

getline(inputAddress, street, ','); //read street field

//TODO: Add getline statements to read remaining fields

//NOTE: Delimiter for last field must be newline '\n' not comma

//TODO: Add cout statements to display addresses to user

}

inputAddress.close(); //close address file

}

}

d. Enter your name and the current date in the comment header.

e. Compile and test the program. Fix any reported errors. The output at this point should look similar to the following:

Step 3: Code and test main menu

a. Inside the main() function, code a loop that will display a main menu with the required 3 options, accept the user's choice, and repeat while the user's choice is not option 3 (exit).

NOTE: Remember to use cin.ignore() after you accept the user's choice, to discard the trailing newline character that may be left in the keyboard buffer after input of a number or single character.

b. Inside your main menu loop, code a switch structure that will process the user's choice. For now, for each option simply code cout statements to display an appropriate message for each choice.

c. Display an error message if the user made an invalid menu choice (anything other than 1, 2, or 3).

d. Compile and test the program. Fix any reported errors. The output at this point should look similar to the following:

Step 4: Code and test entering address data and writing to the address file

a. In the switch statement in your main menu loop, replace the cout statement for option 1 with a call to the EnterAndWriteAddresses() function.

b. In the EnterAndWriteAddresses() function, declare string variables to hold the name and address fields (name, street, city, state, and zip code).

c. Open the address file for output, using the string constant ADDRESS_FILE_NAME as the file name and an appropriate file stream object.

d. Code a loop in which you prompt the user to enter each of the name and address fields and accept the user's input into the corresponding string variable. Use the getline() function as some fields may contain spaces. At the bottom of the loop, ask the user if they want to enter another record, and repeat the loop as long as they respond with an upper- or lower-case Y.

e. Inside the data entry loop, after the user has entered a set of address data, write the name and address fields to the address file using the file stream object you created when you opened the file. Output a comma "," between each field. At the end of the address record, output a newline using a "\n" or endl (do not put a comma after the last field in the record).

f. At the end of the EnterAndWriteAddresses() function, close the address file.

g. Compile and test the program. Fix any reported errors. The output at this point should look similar to the following:

h. To check that your address data were written to the output file correctly, in Visual Studio, pull down the File menu and choose Open > File. Select the Address file and click Open. You should see something similar to the following:

Check that the name and address fields were written correctly, that the fields within each record are separated by commas, and that each record appears on a separate line. If you see any problems in the output file, make adjustments in your code and test again.

Step 5: Code and test reading and displaying data from the address file

a. In the switch statement in your main menu loop, replace the cout statement for option 2 with a call to the ReadAndDisplayAddresses() function.

b. In the ReadAndDisplayAddresses() function, following the getline() statement that reads the street field, add additional getline() statements to read each of the other fields into the appropriate string variable. These will be similar to the getline() for street, except for the variable name. However, in reading the last field (zip), change the comma ',' delimiter to a newline '\n' because the last field in the record is not followed by a comma.

c. After reading in the fields from the address file, add cout statements to display the name and address to the user in a professional, readable format.

d. Compile and test the program. Fix any reported errors. The output at this point should look similar to the following:

Step 6: Capture and submit screen shots and code

a. Do a final end-to-end test of your program in which you enter addresses (option 1) and display these addresses (option 2) in a single run.

b. Capture a screen shot of your output and paste into an MS Word document.

c. Copy your code and paste it into the same MS Word document.

d. Save the Word document as Lab07_LastName_FirstInitial and submit it for the Lab 7 assignment.

Version 1.0 Page 1 of 9

4/9/2009 Lab Activity CIS CIS170B-A1.docx

Page 5 of 6

Lab Activity CIS CIS170C-A7