Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description Write a modularized (c++), menu-driven program to read an unknown number of car records from a file, validate the records, and store the valid

Description

Write a modularized (c++), menu-driven program to read an unknown number of car records from a file, validate the records, and store the valid records in an array of structs/objects. You will add more functionality in the next lab.

Review all tabs and the Best Practices, Standards, & Guidelines (BPSG)Links to an external site. before you start to work on this assignment

Menu (use a switch statement) options to

print all valid items in the inventory unsorted

print invalid records from an error file

quit the program

Requirements/Specifications

Menu

a user should be able to run as many times as the user wants

implement the menu as a switch statement using enum; Although it is recommended to use enum classes instead of enum, for the purpose of this lab and considering that many students in the class are taking CS 216 this semester, use enum

Record

consist of the following fields - car ID, Model, quantity on hand, and price; for example, 12ABMP34Z Fusion_5 20 17000.00

car Id must be 9 characters long with the first two numeric only (0-9), the next four alpha characters, and the other 3 characters may be alpha-numeric ( for example, 12ABMP34Z- valid, 35KMOP324 - invalid); the only alpha characters allowed are A-S and letter O is not allowed

the name may contain only alpha-numeric and underscore characters (for simplicity, one word only: Fusion_5); must start with an alpha character

quantity and the price must be above zero

even if one piece of data is invalid, the whole record is invalid (4 pieces of data, what are the chances of a record to be valid? 24=16 possible combinations of valid/invalid and only one of them is valid 1/16=6.25%)

Formatting output (screen and error file)

records to be printed in a table format with headers

output must be well-formatted, and easily understandable to those who have not seen the code

clearly label the output

all numerical values must be right-aligned

decimal numbers must be right aligned with 2 decimal places; dots should be on the same vertical line

no need to clutter the output with $; do not waste your time trying to print $ in front of the first digit

Input File

records in the file are well-formed but the values of each data piece might be invalid

each record has four pieces of data

all data pieces are of the right data type and in the right order (for simplicity)

All items are unique

Reading from the input file and storing the records

should be done in a function, not main()

do not pass files to the function, you do not need them in main, you only need them in the function

use an array of structs if you do not know how to write classes yet; use classes if you have completed CS 216 or similar

create only one array for valid records, no need to store all records or bad records

the input file has an unknown number of records of inventory items

one record per line in the following order: item ID, item name (one word), quantity on hand, and a price

All fields in the input file are separated by a tab (\t) or a blank (up to you)

the input file may be empty or has more records that your array can store- output the appropriate messages to the user

read a record from a file into a temp struct; error-check the record and only store the record in the array of records if it is valid

check for the array boundaries when storing the records

Only go through the file once - do not read the file once to count the number of the records, close it, and then read it again

Store all invalid records along with the error messages in a separate file

print the record first and then print the list of all errors on the same line

print the bad record once only, for example: /123 candy -5 3.8 ID must be 5 characters and alphanumeric only qty must be at least 0

one bad record = one entry/line in the error file; do not print bad records more than once

could create a local variable to store error messages and keep appending to it if more errors found string errMessage="" ; if( Id length !=MAX_ID_LENGTH) errMessage= errMessage + " ID must be 5 characters";

could create a const string errList{" ID must be 5 characters", " ID must be alphanumeric"...} and a parallel enum

Well document your code (comments)

Include test runs/output as comment /* */ below all function definitions at the very bottom of the file with main(); not including test runs will result in lower score

How to Approach

Plan, plan and plan again

test, test, and test again

START SMALL and add functionality to your program incrementally, almost one at a time and test it before adding any more functionality; for example,

write a function to read from a file (getDta()) into an array of structures

write a function to print the array of structures (print())

test getData() and print() functions

add a function(s) to validate a record (isValidRec()), modify getData() so it calls isValidRec() and stores good records in the array of records and stores bad records in the error file, test again

modify main() by adding a menu to print all inventory, bad records, and to quit, test menu, and the menu options

keep adding functionality and corresponding menu choices, and keep testing each menu choice as you add them

if you choose to create a class for an inventory item

write one function to set a record, call this functions in all c'tor and other setter functions ( if any); it reduces code redundancies, and modifications will be reduced to updating of one function only; for example, class Record has only two member vars: m_id and m_name setRecord( string id, string name) { m_id=id; m_name=name;} setName(string name) {setRecord(m_id, name)} Record() {setRecord("", "')};

create a toString() function

to return the record as a formatted string, then you can use this function to output a record to a screen or file ( cout<

use stringstream to format the string string toString() { stringstream ss; ss<

General Notes

Thoroughly test your program. Your grade partially depends on the quality of your test data.

This and all other programs in this course must comply with the Guidelines and Standards posted under Resources

use function prototypes

function definitions must be below main()

no global variables (including files) except for const

files must be declared in the functions

Opening files common mistake

if(!infile){

cout << "Error opening infile";

exit(1); } //use exit(EXIT_FAILURE); more readable

if(!errorfile){ //if true, will terminate the program and leave infile open

cout << "Error opening errorfile";

exit(1);

use peek() to check if a file is empty

use setw() to format output

do not mix setw() and '\t'; it could produce different results on different computers

const may be declared global; if global, do not have to be passed to function; any function has access to global variables output must be well-formatted; all numerical values must be right aligned with 2 decimal places; dots my be on the same vertical line

Clearly label the output

All numbers to be printed right-aligned

vectors and template classes are not allowed

do not use STL libraries such as list, stack, quite, map, etc

one function = one return statement; use flags if necessarily

no dynamic memory allocation

programs that do not comply with the above will not be accepted

any submenu must have an option to go back to the previous menu or main menu if a user got into a submenu by mistake, a user should be able to go back without executing other menu choices; for example, if a user entered the Sort submenu, a user should be able to go back to the previous menu or main without being forced to sort

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis In Microsoft Excel

Authors: Alex Holloway

1st Edition

B0CCCPKTTX, 979-8852388452

More Books

Students also viewed these Databases questions

Question

10. Revise the draft in a repeated fashion.

Answered: 1 week ago