Question

In: Computer Science

Write a modularized, menu-driven program to read a file with unknown number of records. Input file...

Write a modularized, menu-driven program to read a file with unknown number of records.

  • Input file has unknown number of records of inventory items, but no more than 100; 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)
  • No error checking of the data required
  • Create a menu which allows to
    • print inventory unsorted
    • search for an item by ID or name
    • sort by any field in ascending order (smallest to largest): item ID, item name (one word), quantity on hand , or price. Write one function, that can sort by any field. Do not copy and paste sort code five times into the same function. Tip: use bubble sort. It is easier to modify.
    • quit the program
  • A user should be able to run many as many times as user wants
  • NO goto, continue, break (except for switch)
  • Clearly label the output
  • Well document your code (comments)
  • Include your test data
  • All items are unique

Record sample:

997196478 Stroller 25 134.78

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Code:

// InvenrotyManagement.cpp : Defines the entry point for the console application.
//

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

//structure containing ID, name, quantity and price
typedef struct{
   string ID;
   string name;
   string quantity;
   string price;
}item;

//swap two structures, this helps in bubble sort function
void swap(item *xp, item *yp)
{
   item temp = *xp;
   *xp = *yp;
   *yp = temp;
}

//Searching By ID and if found print details else print not found
void searchByID(item inventory[100],int size){
   string ID;
   cout<<"Please give ID :";//taking ID from user
   cin>>ID;
   for(int i=0;i<size;i++){//looping through whole inventory
       if(inventory[i].ID==ID)//checking if ID matches, if yes print results and return
           cout<<inventory[i].ID<<"\t"<<inventory[i].name<<"\t"<<inventory[i].quantity<<"\t"<<inventory[i].price<<endl<<endl;
       return;
   }
   cout<<"ID not found.\n";
}

//sorting using bubble sort.
void sort(item inventory[100],int size,int sortBy){
   int i, j;
   bool condition;
   for (i = 0; i < size-1; i++) {
       for (j = 0; j < size-i-1; j++){
           //checking condition based in user selection
           if(sortBy==0){
               condition=inventory[j].ID > inventory[j+1].ID;
           }
           else if(sortBy==1){
               condition=inventory[j].name > inventory[j+1].name;
           }
           else if(sortBy==2){
               condition=inventory[j].quantity > inventory[j+1].quantity;
           }
           else if(sortBy==3){
               condition=inventory[j].price > inventory[j+1].price;
           }
           if (condition){ //if condition is true swap
               swap(&inventory[j], &inventory[j+1]);
           }
       }
   }
}

//reading file
void readFile(item inventory[100],int* size){
   ifstream file("inventory.txt");
   if (file.is_open()) {
       string line;
       //reading file line by line
       while (getline(file, line)) {
           size_t pos = 0;
           string token;
           item temp;
           pos = line.find(" ");//finding position of space
           token = line.substr(0, pos);//getting substring from start to space location
           temp.ID=token;//first is ID
           line.erase(0, pos + 1);//removing ID part from line
           //repeating process for name,price and quantity
           pos = line.find(" ");
           token = line.substr(0, pos);
           temp.name=token;
           line.erase(0, pos + 1);

           pos = line.find(" ");
           token = line.substr(0, pos);
           temp.quantity=token;
           line.erase(0, pos + 1);
           temp.price=line;
           inventory[*size]=temp;
           *size=*size+1;
       }
       file.close();
   }else{
       cout<<"Cannot open file.\n";
       EXIT_SUCCESS;
   }
}

//printing inventory
void printList(item inventory[100],int size){
   cout<<"\n=============================================\n";
   cout<<"=================Inventory===================\n";
   cout<<"=============================================\n";
   for(int i=0;i<size;i++){
       cout<<inventory[i].ID<<"\t"<<inventory[i].name<<"\t"<<inventory[i].quantity<<"\t"<<inventory[i].price<<endl;
   }
   cout<<"=============================================\n\n";
}

int showMenu(){
   //choice variable
   int choice=8;
   //showing Menu
   cout<<"Please choose :\n";
   cout<<"1) Print Inventory.\n";
   cout<<"2) Search By ID.\n";
   cout<<"3) Sort By ID.\n";
   cout<<"4) Sort By Name.\n";
   cout<<"5) Sort By Quantity.\n";
   cout<<"6) Sort By Price.\n";
   cout<<"7) Exit.\n";
   //taking choice
   cin>>choice;
   //take choice again for invalid input
   while(choice<1 || choice>7){
       cout<<"Please choose valid input: ";
       cin>>choice;
   }
   //clearing cin
   cin.clear();
   cin.ignore();
   return choice;
}
void main(){
   //array of structure item
   item inventory[100];
   int size=0;
   int choice=1;
   //reading file and storing in array
   readFile(inventory,&size);
   //continue untill user chooses to exit
   while(choice!=7){
       choice=showMenu();
       switch (choice)
       {
       case 1://for case 1 print inventory
           printList(inventory,size);
           break;
       case 2:
           //for input 2 search inventory by ID
           searchByID(inventory,size);
           break;
       case 3:
           //sort inventory by ID
           sort(inventory,size,0);
           break;
       case 4:
           // sort inventory by name
           sort(inventory,size,1);
           break;
       case 5:
           // sort inventory by quantity
           sort(inventory,size,2);
           break;
       case 6:
           // sort inventory by price
           sort(inventory,size,3);
           break;
       case 7:
           break;
       default:
           //printing message for invalid input
           cout<<"Invalid Input. Please try again.\n";
           break;
       }
   }
}

Output:


Related Solutions

Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a program to implement problem statement below: provide the menu for input N and number...
Write a program to implement problem statement below: provide the menu for input N and number of experiment M to calculate average time on M runs. randomly generated list. State your estimate on the BigO number of your algorithm/program logic. (we discussed in the class) Measure the performance of your program by given different N with randomly generated list with multiple experiment of Ns against time to draw the BigO graph (using excel) we discussed during the lecture. Lab-08-BigBiggerBiggtest.png ***...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write a C++ program that does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
Write a menu-driven program to handle the flow of widgets into and out of a warehouse....
Write a menu-driven program to handle the flow of widgets into and out of a warehouse.     The warehouse will have numerous deliveries of new widgets and orders for widgets     The widgets in a filled order are billed at a profit of 50 percent over their cost     Each delivery of new widgets may have a different cost associated with it     The accountants for the firm have instituted a last-in, first-out system for filling orders         the newest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT