Question
This homework assignment gives you the opportunity to practice strings (C++ string class) and chars. Write code in c++ HW3 (Graded out of 100) Write
This homework assignment gives you the opportunity to practice strings (C++ string class) and chars.
Write code in c++
HW3 (Graded out of 100)
Write a program that performs input validation when reading grocery product records from a file. Each grocery product record consists of the following 5 items: - PLU code: Product Look Up Code. Unique for each product, stored as a string. - Product Name, stored as a string - Product Sales Type: 0 = per unit, 1 = per pound - Price per Pound or price per unit, - Current Inventory Level (pounds or units). Below is an example of file content. In this example, there are four records, and each line corresponds to a record. A001 Apples 1 0.90 21 A002 Peaches 1 0.82 11 A006 Avocados 0 1.54 27 A008 Mangos 0 1.69 19 Your program should read a file and performs input validation on the items read, then print the outcome of the input validation: File has valid content if all the data is valid File has invalid content otherwise, along with the reason.
To contain valid data, the file must have only valid records. A record is valid if It has exactly 5 items on the same line The first item should be a PLU. It is valid if and only if it contains only letters or digits, and is 4 characters long The second item should be product name. It is valid if and only if the first character is a letter The third item should be a sales type. It is valid if and only if it is only one character, and the character is a 1 or a 0 The fourth item should be a unit price. It is valid if and only if it contains only digits and at most one dot. If there is a dot, there are at most two digits after the dot The fifth item should be the inventory level. It is valid if and only if it contains only digits In order to validate the individual items, each line has first to be broken into tokens, where a token is a string that corresponds to an item. The tokens are separated by one or more delimiters, where a delimiter is a space, a tab, a new line or a carriage return. To take the line A001 Apples 1 0.90 21 as an example, the first token would be A001, the second token would be Apples, and so on. You may assume each line contains at most only one record.
1. Additional Requirements Make sure you meet all the requirements to avoid losing points
a) Functions
You are required to implement your program with these functions. You may implement more functions to make your program more modular if you wish.
b) Outline of main
c) Style
Make sure you follow the style requirements, especially regarding the comment header for functions, to avoid losing points.
2. Implementation Suggestions - You are not required to implement the suggestions
You are allowed to use any of the existing library functions in chapter 10 of the Gaddis textbook.
a) bool isFileValid(string file)Notes: To read line by line, you can use getline(inFile, stringRead) Detect end of file when getline returns an empty string
b) string tokenize(string & s)
2. Grading criteria
- Source code inspection (grader) *Style: 5 points (refer to the Homework Notes for the style requirements)
- Code compilation and execution (zylabs) *Output test-1 Valid file Output exactly matches output-1: 9 points *Output test-2 File with invalid PLU Output exactly matches output-2: 9 points *Output test-3 File with invalid product name Output exactly matches output-3: 9 points *Output test-4 File with invalid sales type Output exactly matches output-4: 9 points *Output test-5 File with invalid price Output exactly matches output-5: 9 points *Output test-6 File with invalid inventory Output exactly matches output-6: 9 points *Output test-7 File with excessive number of items Output exactly matches output-7: 9 points *Output test-8 File with insufficient number of items Output exactly matches output-8: 9 points *Output test-9 File with invalid price Output exactly matches output-9: 9 points *Unit test-1 tokenize: 9 points *Unit test-2 isValidPrice: 5 points
- 3. Screen outputs
/Returns true if the string is a valid PLU, false otherwise bool isValidPLU (string) / Returns true if the string is a valid product name, false otherwise bool isValidName (string) /Returns true if the string is a valid sales type, false otherwise bool isValidType (string) /*Returns true if the string is a valid price, false otherwise bool isValidPrice (string); /Returns true if the string is a valid inventory level, false otherwise bool isValidInventory (string) /Takes as reference parameter a string to be tokenized and returns the first token found Returns the empty string if no token is found The function deletes any leading delimiter and the first token found from the original string Tokenization is based on a delimiter, where a delimiter is the '\t' (tab) , ' ' (space), 'in, (new line) or ' (carriage return character Example: if the string s is "t abcdltlt 3451t 7s ", the function returns "abcd" as the first token found, and modifies the string s to become "tt 3451t 7s " string tokenize (string&) Prompt the user to enter a file name Read from the file and repeatedly call tokenize to extract the items Print the item and perform the appropriate validation. For example, for the first token which should be the PLU, perform PLU validation. For the second token, perform name validation, etc. Print the result of the validation. If an item is invalid, determine that the file is invalid and close the file. Check every line has the correct number of items. if the number of items is not 5, print an error message, determine that the file is invalid and close the file. If all validations are successful, the file is determined to be valid Print a message to indicate if the file is valid or invalid Open file // Check that open is successful Read line by line. For each line: Repeatedly call tokenize to extract the items Print the item and perf rm the appr priate validation. Print the resultf the validation. If an item is invalid, close the file and return false Check the line has the correct number of items. if the number of items is not 5, print an error message, close the file and return false At end of file, close the file and return true Loop over the characters of s as long as they are a delimiter and the end of s is not reached // Remove any leading delimiter (s) Loop over the remaining characters as long as they are not a delimiter and the end of s is not reached // Extract the first token found store the characters into token Delete the first n characters from s // n is the number of characters looped over in the two above loops Return token Enter input file:in.txt Checking in.txt Token #1 13 A001, PLU 13 valid Token #2 13 Apples, Product name is valid Token #3 13 1, Sales type i3 valid Token #4 13 0.90, Price is valid Token #5 13 21, Inventory i3 valid Token #1 is A002, PLU i3 valid Token #2 13 Peaches, Product name 13 valid Token #3 13 1, Sales type 13 valid Token #4 i3 0.82, Price is valid Token #5 13 11, Inventory i3 valid Token #l 13 A009, PLU 13 valid Token #2 13 Strawberrie3 , Product name i3 valid Token #3 is 0, Sales type is valid Token #4 13 12.50, Price 13 valid Token #5 is 8, Inventory is valid Token #1 i3 4261, PLU i3 valid Token #2 13 Rice1LBBag, Product name is valid Token #3 is 0, Sales type is valid Token #4 13 0.49, Price i3 valid Token #5 13 107, Inventory is valid - - - Output-1 Enter input file:ino.txt Checking ino.txt Token #1 i3 A001, PLU i3 valid Token #2 13 Apples, Product name 13 valid Token #3 is 1, Sales type is valid Token #4 13 0.90, Price i3 valid Token #5 is 21, val Inventory is Token #1 13 A00, PLU 13 invalid Output-2 Enter input file:in1.txt Checking ini.txt Token #1 i3 A001, PLU i3 valid Token #2 13 Apples, Product name 13 valid Token #3 is 1, Sales type is valid Token #4 13 0.90, Price i3 valid Token #5 is 21, Inventory i3 val Token #1 13 A002, PLU is valid Token #2 i3 [eaches, Product name i3 invalid Output-3 Enter input file:in2.txt Checking in2.txt Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.90, Price is valid Token #5 13 21, Inventory 13 valid Token #1 is A002, PLU is valid Token #2 is Peaches, Product name is valid Token #3 is 11, Sales type is invalid Output-4 Enter input file:in3.txt Checking in3.txt Token #1 is A001, PIU is valid Token #2 is Apples, Product name is valid Token #3 is 1, sales type is valid | Token #4 is -0.90, Price is invalid Output-5 Enter input file:in4.txt Checking in.txt Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 13 1, Sales type is valid Token #4 is 0.90, Price is valid Token #5 is 21, Inventory is valid Token #1 is A002, PLU is valid Token #2 is Peaches, Product name is valid Token #3 13 1, sales type 13 valid Token #4 is 0.82, Price is valid Token #5 is 11 ., Inventory is invalid Output-6 Enter input file:in5.txt Checking in5.txt Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 i3 0.90, Price is valid Token #5 is 21, Inventory is valid Token #1 is A002, PLU is valid Token #2 is Peaches, Product name is valid Token #3 is 1, Sales type is valid I ken #4 is 0.82, Price is valid Token #5 is 11, Inventory 13 valid Token #6 13 &^, Too many items in record ######## n 5 . txt has invalid content ####### Output-7
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started