Question

The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory.

//CPP

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <iomanip>

#define MAX 1000

using namespace std;

//Function to Add a new inventory item to the data into the array in memory

void addItem(string desc[],string idNum[], float prices[], int qty[],int &num)

{

cout<<"Enter the names:";

cin>>desc[num];

cout<<"Enter the item number:";

cin>>idNum[num];

cout<<"Enter the price of item:";

cin>>prices[num];

cout<<"Enter the quantity:";

cin>>qty[num];

num++;

}

//Function to Search the inventory (in the array in memory) for an item by inventory number

void searchItem(string desc[],string idNum[], float prices[], int qty[],int num)

{

string itemno;

int i;

cout<<"Enter the item number to be searched:";

cin>>itemno;

for(i=0; i<num; i++)

{

if(itemno == idNum[i])

break;

}  

if(i == num)

cout<< itemno <<" not found"<<endl;

else

{

cout<<left<<setw(25)<<"Item Name"<<left<<setw(25)<<"Item Number"<<left<<setw(15)<<"Price"<<left<<setw(15)<<"Quantity"<<endl;

cout<<left<<setw(25)<<desc[i]<<left<<setw(25)<<idNum[i]<<left<<setw(15)<<prices[i]<<left<<setw(15)<<qty[i]<<endl;

}

}

//Function List the inventory (in the array in memory) on the screen in neat columns

void listItem(string desc[],string idNum[], float prices[], int qty[],int num)

{

cout<<left<<setw(25)<<"Item Name"<<left<<setw(25)<<"Item Number"<<left<<setw(15)<<"Price"<<left<<setw(15)<<"Quantity"<<endl;

for(int i=0; i<num; i++)

cout<<left<<setw(25)<<desc[i]<<left<<setw(25)<<idNum[i]<<left<<setw(15)<<prices[i]<<left<<setw(15)<<qty[i]<<endl;

}

//Function to Calculate the total value on hand of the inventory (in the array in memory)

void totalValue(string desc[],string idNum[], float prices[], int qty[],int num)

{ float value=0;

for(int i=0; i<num; i++)

value = value + (prices[i] * qty[i]);

  

cout<<"The total value of Inventory:"<<value<<endl;

}

//Function to Save the array in memory to a disk file

void saveFile(ofstream &fout,string desc[],string idNum[], float prices[], int qty[],int num)

{

fout.open("inventory.dat");

if(!fout)

{

cout<<"File cannot be opened for writing"<<endl;

exit(0);

}

for(int i=0; i<num; i++)

{

fout<<desc[i]<<endl;

fout<<idNum[i]<<endl;

fout<<prices[i]<<endl;

fout<<qty[i]<<endl;

}

fout.close();

cout<<"File Saved"<<endl;

}

//Read from a disk file to the array in memory

void readFile(ifstream &fin,string desc[],string idNum[], float prices[], int qty[],int &num)

{

fin.open("inventory.dat");

if(!fin)

{

cout<<"File cannot be opened for reading"<<endl;

exit(0);

}

num=0;

while(!fin.eof())

{ fin>>desc[num];

fin>>idNum[num];

fin>>prices[num];

fin>>qty[num];

num++;

}

num--;

cout<<"Read from a disk file to the array"<<endl;

fin.close();

}

//main() function display the menu on screen & call desired function selected from menu option

int main()

{

int choice, num=0;

string descriptions[MAX];

string idNumbers[MAX];

float prices[MAX];

int quantity[MAX];

  

ifstream fin; // input file stream for reading file

ofstream fout; // output file stream for writing into file

do {

//Displaying menu options

cout<<endl<<endl;

cout<<"1) Add an item to memory"<<endl;

cout<<"2) Search memory for an item"<<endl;

cout<<"3) List Items"<<endl;

cout<<"4) Total value on hand"<<endl;

cout<<"5) Save to a file"<<endl;

cout<<"6) Read in from a file"<<endl;

cout<<"0) Exit Program"<<endl;

cout<<"Enter Choice:"<<endl;

cin>>choice;

// function calling will be based on the choice selected from menu options

switch(choice) {

case 1: addItem(descriptions, idNumbers, prices, quantity, num);

break;

case 2: searchItem(descriptions, idNumbers, prices, quantity, num);

break;

case 3: listItem(descriptions, idNumbers, prices, quantity, num);

break;

case 4: totalValue(descriptions, idNumbers, prices, quantity, num);

break;

case 5: saveFile(fout,descriptions, idNumbers, prices, quantity, num);

break;

case 6: readFile(fin, descriptions, idNumbers, prices, quantity, num);

break;  

}

}while(choice!=0); // end of loop

return 1;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <iomanip>

#define MAX 1000

using namespace std;

//Function to Add a new inventory item to the data into the array in memory

void addItem(string desc[],string idNum[], float prices[], int qty[],int &num)

{

               cout<<"Enter the names:";

               cin>>desc[num];

               cout<<"Enter the item number:";

               cin>>idNum[num];

               cout<<"Enter the price of item:";

               cin>>prices[num];

               cout<<"Enter the quantity:";

               cin>>qty[num];

               num++;

}

//Function to Search the inventory (in the array in memory) for an item by inventory number

void searchItem(string desc[],string idNum[], float prices[], int qty[],int num)

{

               string itemno;

               int i;

               cout<<"Enter the item number to be searched:";

               cin>>itemno;

               for(i=0; i<num; i++)

               {

               if(itemno == idNum[i])

               break;

               }

               if(i == num)

               cout<< itemno <<" not found"<<endl;

               else

               {

               cout<<left<<setw(25)<<"Item Name"<<left<<setw(25)<<"Item Number"<<left<<setw(15)<<"Price"<<left<<setw(15)<<"Quantity"<<endl;

               cout<<left<<setw(25)<<desc[i]<<left<<setw(25)<<idNum[i]<<left<<setw(15)<<prices[i]<<left<<setw(15)<<qty[i]<<endl;

               }

}

//Function List the inventory (in the array in memory) on the screen in neat columns

void listItem(string desc[],string idNum[], float prices[], int qty[],int num)

{

               cout<<left<<setw(25)<<"Item Name"<<left<<setw(25)<<"Item Number"<<left<<setw(15)<<"Price"<<left<<setw(15)<<"Quantity"<<endl;

               for(int i=0; i<num; i++)

                              cout<<left<<setw(25)<<desc[i]<<left<<setw(25)<<idNum[i]<<left<<setw(15)<<prices[i]<<left<<setw(15)<<qty[i]<<endl;

}

//Function to Calculate the total value on hand of the inventory (in the array in memory)

void totalValue(string desc[],string idNum[], float prices[], int qty[],int num)

{

               float value=0;

               for(int i=0; i<num; i++)

               value = value + (prices[i] * qty[i]);

               cout<<"The total value of Inventory:"<<value<<endl;

}

//Function to Save the array in memory to a disk file

void saveFile(ofstream &fout,string desc[],string idNum[], float prices[], int qty[],int num)

{

               fout.open("inventory.dat");

               if(!fout)

               {

               cout<<"File cannot be opened for writing"<<endl;

               exit(0);

               }

               for(int i=0; i<num; i++)

               {

               fout<<desc[i]<<endl;

               fout<<idNum[i]<<endl;

               fout<<prices[i]<<endl;

               fout<<qty[i]<<endl;

               }

               fout.close();

               cout<<"File Saved"<<endl;

}

//Read from a disk file to the array in memory

void readFile(ifstream &fin,string desc[],string idNum[], float prices[], int qty[],int &num)

{

               fin.open("inventory.dat");

               if(!fin)

               {

               cout<<"File cannot be opened for reading"<<endl;

               exit(0);

               }

               num=0;

               while(!fin.eof())

               { fin>>desc[num];

               fin>>idNum[num];

               fin>>prices[num];

               fin>>qty[num];

               num++;

               }

               num--;

               cout<<"Read from a disk file to the array"<<endl;

               fin.close();

}

// Function to Remove the item from the inventory (in the array in memory) by item number

void removeItem(string desc[],string idNum[], float prices[], int qty[],int &num)

{

               bool found = false;

               string itemNo;

               int i,j;

               // input of item number to be removed

               cout<<"Enter the item number to be removed :";

               cin>>itemNo;

               // loop over the items to find the index of itemNo

               for(i=0;i<num;i++)

               {

                              // if item is found

                              if(itemNo == idNum[i]) // case-sensitive search

                              {

                                             // shift all the elements one position up in order to remove the item from the inventory

                                             for(j=i;j<num-1;j++)

                                             {

                                                            desc[j] = desc[j+1];

                                                            idNum[j] = idNum[j+1];

                                                            prices[j] = prices[j+1];

                                                            qty[j] = qty[j+1];

                                             }

                                             num--; // decrement the number of items in inventory

                                             found = true;

                                             break;

                              }

               }

               if(!found)

                              cout<< itemNo <<" not found"<<endl;

               else

                              cout<<itemNo<<" removed"<<endl;

}

//main() function display the menu on screen & call desired function selected from menu option

int main() {

               int choice, num=0;

               string descriptions[MAX];

               string idNumbers[MAX];

               float prices[MAX];

               int quantity[MAX];

               ifstream fin; // input file stream for reading file

               ofstream fout; // output file stream for writing into file

               do {

               //Displaying menu options

               cout<<endl<<endl;

               cout<<"1) Add an item to memory"<<endl;

               cout<<"2) Search memory for an item"<<endl;

               cout<<"3) List Items"<<endl;

               cout<<"4) Total value on hand"<<endl;

               cout<<"5) Save to a file"<<endl;

               cout<<"6) Read in from a file"<<endl;

               cout<<"7) Remove an item from memory"<<endl;

               cout<<"0) Exit Program"<<endl;

               cout<<"Enter Choice:"<<endl;

               cin>>choice;

               // function calling will be based on the choice selected from menu options

               switch(choice) {

               case 1: addItem(descriptions, idNumbers, prices, quantity, num);

               break;

               case 2: searchItem(descriptions, idNumbers, prices, quantity, num);

               break;

               case 3: listItem(descriptions, idNumbers, prices, quantity, num);

               break;

               case 4: totalValue(descriptions, idNumbers, prices, quantity, num);

               break;

               case 5: saveFile(fout,descriptions, idNumbers, prices, quantity, num);

               break;

               case 6: readFile(fin, descriptions, idNumbers, prices, quantity, num);

               break;

               case 7:

                              removeItem(descriptions, idNumbers, prices, quantity, num);

                              break;

               }

               }while(choice!=0); // end of loop

               return 0;

}

//end of program

Output:

1) Add an item to memory 2) Search memory for an item 3) List Items 4) Total value on hand 5) Save to a file 6) Read in from

1) Add an item to memory 2) Search memory for an item 3) List Items 4) Total value on hand 5) Save to a file 6) Read in from1) Add an item to memory 2) Search memory for an item 3) List Items 4) Total value on hand 5) Save to a file 6) Read in from

1) Add an item to memory 2) Search memory for an item 3) List Items 4) Total value on hand 5) Save to a file 6) Read in from

Add a comment
Know the answer?
Add Answer to:
The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • 1. in this programe i want add some products to be shown after choosing choise (...

    1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce. 2. allow the user to buy products. ===========================================================...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT