CIS 150 Programming Project: Sequential access (text) file

Objectives

  • Create an input loop to enter data
  • Stop input when the user chooses to end it
  • Validate user input using functions
  • Convert strings to uppercase
  • Write data to a sequential access text file

Overview

This assignment involves the creation of a sequential access, text, variable-length record data file. You will create the user interface and store the data the user enters. Each record in the file has the following fields:

  1. department: string
  2. employee id: string
  3. month: int (must be between 1 and 12)
  4. day: int (must be between 1 and 31)
  5. year: int (must be between 2000 and 2100)
  6. hours: double (must be between 0.0 and 24.0)

Program requirements

  1. Include required libraries.
  2. Create prototypes before main for the functions getInt, getDouble, getChar, and upper.
  3. In main()
    1. Declare variables for each of the fields the record has.
    2. Declare a character variable for asking the user if he wants to enter another record.
    3. Open a file named seqfile.txt for text output where you want to append to the end of the file.
    4. If the file failed to open, display an error and exit with a failure exit status.
    5. Create a loop that will run until the user chooses to not enter another record.
    6. In the loop, prompt the user for each field in the record and validate the input. [Use getInt and getDouble]
    7. In the loop, convert the department and employee ID to uppercase. [Use upper]
    8. In the loop, output each field to the file separated by a comma, and followed by a newline.
    9. After the loop, close the file and exit with a success exit status.
  4. There should be four functions used, complete with prototypes:
    1. int getInt(string prompt, int min, int max): which prompts the user, gets a valid integer within the range of min and max, and returns that value
    2. double getDouble(string prompt, double min, double max): which prompts the user, gets a valid double within the range of min and max, and returns that value
    3. char getChar(string prompt, string allowed): which prompts the user, gets a valid character, validates that the character is within the allowed list, and returns that character
    4. string upper(string s): which creates an uppercase version of the string passed in and returns it

Function: string upper(string)

The following code requires the C++ libraries: cctype, string

Function notes: char getChar(string, string)

  1. The first parameter is the prompt
  2. The second parameter is a string of allowed characters.
  3. All input validation routines should include a loop.
  4. You can find out if a character in variable ch is in a string variable s using: s.find(ch)
  5. If the character is found, the integer position where it was found will be returned.
  6. If the character is not found, then string::npos will be returned.

Sample Run 1

Enter dept ID: it Employee ID: d4772 Enter month: 1 Enter day: 2 Enter year: 2011 Enter hours: 8.25 Enter another record (Y/N)? y Enter dept ID: it Employee ID: f9642 Enter month: 1 Enter day: 4 Enter year: 2011 Enter hours: 8.25 Enter another record (Y/N)? n

The file so far would look like this:

IT,D4772,1,2,2011,8.25 IT,F9642,1,4,2011,8.25

Sample Run 2

Enter dept ID: IT Employee ID: V1001 Enter month: -9 Error: Value below minimum of 1 Enter month: 1 Enter day: 34 Error: Value above maximum of 31 Enter day: 3 Enter year: 08 Error: Value below minimum of 2000 Enter year: 2011 Enter hours: 480 Error: Value above maximum of 24 Enter hours: 5.25 Enter another record (Y/N)? Y Enter dept ID: mgt Employee ID: a0010 Enter month: 1 Enter day: 2 Enter year: 2011 Enter hours: 6.5 Enter another record (Y/N)? p Error: Invalid choice Enter another record (Y/N)? n

The file would now look like this:

IT,D4772,1,2,2011,8.25 IT,F9642,1,4,2011,8.25 IT,V1001,1,3,2011,5.25 MGT,A0010,1,2,2011,6.5

Rubric

  • Note: Points may be deducted for not meeting the course coding standards.
  • 2 points for including the required libraries
  • 3 points for having the input loop run and end properly
  • 3 points for converting all text input to uppercase
  • 5 points for a properly functioning getInt function
  • 5 points for a properly functioning getDouble function
  • 5 points for a properly functioning getChar function
  • 4 points for writing the data correctly to a sequential access file
  • 3 points for appending data to the file rather than replacing the file contents