Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

c++ write a program to keep track of a hardware store inventory

+1 vote
asked Jan 27, 2021 by nurul hanisah (220 points)

hi .. i have a problem with my coding for my final assignment ... the coding just work perfectly but the problem is in the inventory part cause it keep repeating the table .. and the data not changing at all .. i really hope someone can help me with my coding because my group membe just dissapear and leave the coding to me to settle ... i am very stress right noww .. huhu

so here is my coding....

#include<iostream>

#include <fstream>

#include <string>

#include<vector>

#include<iomanip>

using namespace std;

vector<string> vitem_name;    //item name

vector<int> vitem_ID;      //item ID

vector<int> vpOrdered;       //number of pieces ordered

vector<int> vpInStore;       //number of pieces currently in the store

vector<int> vpsold;           //number of pieces sold

vector<float> vmanufPrice;     //manufacturer's price for the item 

vector<float> vsellingPrice;        // selling price of item

   

// function to store file data into vector

void storeInVector()

{

std::ifstream infile("inventory.txt");

    string item_name;

    int item_ID;

    int pOrdered;

    int pInStore;

    int pSold;

    float manufPrice;

    float sellingPrice;

    

while (infile >> item_ID >> item_name>>pOrdered >> pInStore >>pSold >>manufPrice >>sellingPrice)

    {

    vitem_ID.push_back(item_ID );

    vitem_name.push_back(item_name);

    vpOrdered.push_back(pOrdered);

    vpInStore.push_back(pInStore);

    vpsold.push_back(pSold);

    vmanufPrice.push_back(manufPrice);

    vsellingPrice.push_back(sellingPrice);

   

    }

    infile.close();

}

//function to print report

void printReport()

{

    storeInVector();

   cout<<"Friendly Hardware Store Report\n\nItemID   itemName   pOrdered   pInstore    pSold    manufPrice   sellingPrice\n";

    

    vector<string>::iterator it;

    float total_inv=0;

    int tInStore=0;

    int i=0;

    for(it = vitem_name.begin(); it != vitem_name.end(); it++,i++)    

{

cout<<vitem_ID.at(i)<<setw(15) <<*it<<setw(10)<<vpOrdered.at(i) <<setw(10) <<vpInStore.at(i) <<setw(10) <<vpsold.at(i) <<setw(10) <<vmanufPrice.at(i) <<setw(10) <<vsellingPrice.at(i) <<"\n";

        total_inv += (vpInStore.at(i) - vpsold.at(i))*vsellingPrice.at(i); 

        tInStore += vpInStore.at(i) - vpsold.at(i);

    }

    cout<<"Total Inventory: $"<<total_inv<<endl;

    cout<<"Total number of items in the Store: "<<tInStore<<"\n\n";

     

}

// function to search item by name

bool searchItem(string name)

{

    storeInVector();

    vector<string>::iterator it;

    for(it = vitem_name.begin(); it != vitem_name.end(); it++)    

{

    if(*it == name)

{

       return true;

       }

   }

    return false;

}

// function to check price of item

float checkPrice(string name)

{

    storeInVector();

    vector<string>::iterator it;

    int i=0;

    for(it = vitem_name.begin(); it != vitem_name.end(); it++,i++ )    

{

    if(*it == name)

{

       return vsellingPrice.at(i);

       }

   }

    return 0;

}

// function to sell item

float sellItem(string name, int cnt)

{

storeInVector();

    vector<string>::iterator it;

    int i=0;

    for(it = vitem_name.begin(); it != vitem_name.end(); it++,i++ )    

{

    if(*it == name)

{

       return cnt*vsellingPrice.at(i);

       }

   }

    return 0;

}

// function to display menu

void displayMenu()

{

cout<<"Welcome to the Friendly Hardware Store!\n\nChoose from the following options.\n1: To see if an item is in the store (search by name of the item entered).\n2: To buy an item (ask for the name of the item and then how many the buyer wants to purchase, output how much the buyer owes).\n3. To check the price of an item (ask for the name of the item).\n4: To print the inventory (print the inventory report.\n9: To end the program.\n\n";

}

int main()

{

int choice=0;

do

{

    displayMenu();

cin>> choice;

switch(choice)

{

case 1: {

      string name;

     cout<<"\nEnter item name to search: ";

     cin>> name;

     if(searchItem(name))

     {

      cout<<"Item found in Store\n\n";

}

else  cout<<"Item not found in Store\n\n";

}

    break;

case 2:

{

string name;

int cnt=0;

     cout<<"\nEnter item name to search: ";

     cin>> name;

     cout<<"How many u want to purchase? ";

     cin>>cnt;

     float pprice = sellItem(name, cnt);

     if(pprice > 0)

     {

      cout<<"You need to pay: "<<pprice<<"\n\n";

}

else  cout<<"Item not found in Store\n\n";

}

break;

case 3:

{

string name;

     cout<<"\nEnter item name to check price: ";

     cin>> name;

     float price = checkPrice(name);

     if(price >0)

     {

      cout<<"Price of Item: "<<price <<"\n\n";

}

else  cout<<"Item not found in Store\n\n";

}

break;

case 4: {

    printReport();

}

break;

case 9:  exit(0);

break;

default: cout<<"Invalid choice";

}

}while(choice > 0);

return 0;

}

and the inventory.txt file are writing like this :

3333 CookingRange 50 50 20 450.00 850.00
4444 CircularSaw 150 150 40 45.00 125.00

.p/s : i really hope someone can help me with this codingg image

1 Answer

0 votes
answered Feb 1, 2021 by xDELLx (10,500 points)
// function to store file data into vector
void storeInVector()
{
static bool stored = false;
if (stored == true ) return ;
std::ifstream infile("inventory.txt");
if ( infile.is_open() != true ){
        
   cout<<"Unable to open input file \n\n";
    return;
}
    string item_name;
    int item_ID;
    int pOrdered;
    int pInStore;
    int pSold;
    float manufPrice;
    float sellingPrice;
        
while ( !infile.eof() &&  
         infile >> item_ID >> item_name>>pOrdered >> pInStore >>pSold >>manufPrice >>sellingPrice)
    {   
    vitem_ID.push_back(item_ID );
    vitem_name.push_back(item_name);
    vpOrdered.push_back(pOrdered);
    vpInStore.push_back(pInStore);
    vpsold.push_back(pSold);
    vmanufPrice.push_back(manufPrice);
    vsellingPrice.push_back(sellingPrice);
    stored = true;
   
    }   
    infile.close();
}


storeInVector() needs to be updated , plz think why the changes were made .
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...