Implement a well-structured general C program to enable a grocery owner to maint
ID: 3693428 • Letter: I
Question
Implement a well-structured general C program to enable a grocery owner to maintain the inventory of his fruits as well as to sell fruits to customers. The inventory is kept in a text-file fruits.txt of the form: 14.5 5.50 27.2 7.00 20.0 4.50 17.6 6.00 34.5 8.00 16.0 10.00 21.5 6.50 where each line of the text-file contains the stock quantity of a fruit in kilograms, and the current price per kilogram for the fruit in Saudi Riyals. The fruits are sequentially numbered: 1, 2, 3, . . . , and so on in the textfile. Your program must have the following menu: 1. Display Fruit Info for all Fruits 2. Display Fruit Info for a particular Fruit 3. Sell Fruit to Customer 4. Exit Please select your choice: Your program must loop as long as option 4 has not been selected. It must display an appropriate error message if an invalid choice is entered. After executing each of the options 1 to 3, your program must pause and display the message: “Press Enter key to continue . . .” . Your program must clear the screen and display the main menu after pressing the Enter key. Each of the options 1 to 3 must be implemented in a separate function. The code for “Press Enter key to continue . . .” must also be implemented is separate function. The options must have the following behaviors: Option 1: Display Fruit Info for all Fruits It displays the fruit information of all fruits. The option must be implemented by reading directly from fruits.txt text-file. It then waits for the Enter key to be pressed before returning control to the main menu: Please select your choice: 1 Fruit Number Stock Quantity (kg) Price per Kilogram (SAR) 1 14.5 5.50 2 27.2 7.00 3 20.0 4.50 4 17.6 6.00 5 34.5 8.00 6 16.0 10.00 7 21.5 6.50 Press Enter key to continue . . . Option 2: Display Fruit Info for a particular Fruit It prompts for and reads a fruit number. It then searches for this fruit in the fruits.txt text-file. If the fruit number entered is zero or negative or more than the number of lines in the text-file an appropriate error message is displayed, otherwise; the fruit information is displayed. In both cases, the option waits for the Enter key to be pressed before returning control to the main menu. Please select your choice: 2 Enter fruit number [1 to 7]: 5 Fruit Number Stock Quantity (kg) Price per Kilogram (SAR) 5 34.5 8.00 Press Enter key to continue . . . Please select your choice: 2 Enter fruit number [1 to 7]: 10 Error: Invalid fruit number Press Enter key to continue . . . Options 3. Sell Fruit to Customer It initializes two parallel arrays with the information from the inventory file fruits.txt (count the number of lines in the inventory text-file and use it as the size of each array). It then sells one or more fruits to a customer if the requested fruit or fruits are available and if the requested quantities are less or equal to the associated stock quantities. Each time a fruit is sold, its number, quantity sold, price per kilogram, and the cost, are appended to a line of bills.txt text-file and the array of stock quantity is updated. When the customer has bought all fruits he wants, the total bill is appended to bills.txt, the inventory text file fruits.txt is updated, and the customer bill, i.e., bills.txt is displayed on the screen. Please select your choice: 3 Enter fruit number [1 to 7]: 1 Enter quantity [kg]: 40.0 Error: Insufficient stock. Stock available is 14.5 kilograms Do you want to buy another fruit (y/n): y Enter fruit number [1 to 7]: 1 Enter quantity [kg]: 10.0 Do you want to buy another fruit (y/n): y Enter fruit number [1 to 7]: 4 Enter quantity [kg]: -2 Error: Invalid quantity Do you want to buy another fruit (y/n): y Enter fruit number [1 to 7]: 5 Enter quantity [kg]: 4.0 Do you want buy another fruit (y/n): y Enter fruit number [1 to 7]: 9 Error: Invalid fruit number Do you want to buy another fruit (y/n): y Enter fruit number [1 to 7]: 3 Enter quantity [kg]: 2.0 Do you want to buy another fruit (y/n): n Fruit Number Quantity (kg) Price per Kilogram (SAR) Subtotal(SAR) 1 10.0 5.50 55.00 5 4.0 8.00 32.00 3 2.0 4.50 9.00 Total: 96.00 Press Enter key to continue . . . Control is returned to the main menu after pressing the Enter key. Note: • Your program must be general. It must work for any number of fruits in fruits.txt • Your program must not use 2D-arrays. • You must use meaningful variable names and proper indentation in your program. • You may use other functions in your program, like countLines, searchForFruit, updateTextFile, loadToArrays, etc. • You must not share code with another project group. Doing so will result in a zero grade for all groups involved. • Project demo slots of 15 to 20 minutes per group will be announced later. All group members are required to attend the project demo together. A grade of zero will be given for any student not attending the Project Demo. Hint: • When you update the fruits text-file, make sure you do not insert a blank line at the end of the file, because this will generate reading errors. Write the last file line without generating ‘ ’
Explanation / Answer
So far i have got this much..!! #include #include #include #include using namespace std; void main (void) { fstream readFile("fruit.txt"); string fruit,amount,line; bool found = false; string fruitArray[20][2]; cout > fruit; while (!readFile.eof()) { int y =0 ; int x =0 ; getline(readFile,line); stringstream ss(line); string tempFruit; while (getline(ss,tempFruit,',')) { fruitArray[x][y] = tempFruit; y++; } x++; } for (int i = 0; i < 20; i++) { for (int ii = 0; ii < 2; ii++) { string test = fruitArray[i][ii] ; if (test == fruit) { amount = fruitArray[i][ii+1]; found = true; break; } else{ coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.