Thanks to some people who has already given me some pointers and insights, I wrote down on paper the layout before I started putting them into code, which I eventuall did and they're in code now. I tried to run it and gave me a list of lots of errors. :( The program is suppose to be an inventoryItem class that calculates data. It reads from input.txt and the input file has

command Item Number Quantity Cost Total cost (cost*quantity)


The commands are:
add - adds a new product number, price and quantity of that item to a vector
sold - finds the product number, and decrease the quantity based on the the amount that the input file has
deliver - finds the product number, and increase the quantity based on the the amount that the input file has
priceChange - finds the product number and changes the price
remove - removes the itemNumber from the inventory

Here is what I have as of now

The header file:

class inventoryItem
{
private:
	string itemNumber;			//a variable that is set when the item is created
	int quantity;				//the variable that represents number of items currently in stock
	int cost;					//the per-unit price of the item
	int totalCost;				//total inventory cost of the item

public:
	
	void inputnewItem(string n)		//adds a new item and set itemNumber only
		:itemNumber(n)

	/*adds a new item and set itemNumber along with quantity and cost*/
	void inventoryItem( string n, int q, double c )	
		:itemNumber(n), quantity(q), cost(c)
	
	void string erase();				//it will delete the item number
	void int setQuantity();	//modifies the quantity of inventory
	void double setCost();	//modifies the cost of the inventory		
	int getItemNumber();	//gets the item number of the inventory
	int getQuantity();		//gets the quantity of the inventory (sold)
	int getQuantity2();		//gets the quantity of the inventory (delievered)
	double getCost();		//gets the cost of the inventory
	double getTotalPrice();	//cost*quantity


};

The calculation file:

#include "Wong_inventoryItem.h"
#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>
using namespace std;


void inventoryItem::inventoryItem ( string n, int q, double c )
{
	vector < inventoryItem > inventoryList; //vector to store the inventory item

	inventoryList.push_back( n, q, c);	//adds inventory number to vector
	cout << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;
	Output << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;
}

//this function will change the quantity number of the product
int inventoryItem::getQuantity( string n, int q, double c )
{
	string getQuantity( const vector <inventoryItem> &inventoryList, string n);
	string i = 0;

			int q_q = 0;
			void swap (int q_q, int q);
			//swaps the read from input quantity into q_q
			int c = q_q;
			q_q = q;
			q = c;

	for ( i = 0; i < inventoryList.size(); i++)
	{
		if (inventoryList[i].getQuantity == n) //searchs for the item number 
 //that the input file has just been read
		{
			
			inventoryList[i].setQuantity( inventoryList[i].getQuantity() - q_q);	
			iventoryList.push_back( n, q, c );	//stores the new quantity along with
	//itemNumber and cost into vector

			cout << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;
			Output << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;		
		}
		else
		{
			Error <<"Error: " << n << "not in inventory"<<endl;
		}
	}
	return -1;
}

//this function will change the quantity number of the product
int inventoryItem::getQuantity2( string n, int q)
{
	string getQuantity2(const vector <inventoryItem> &inventoryList, string n);
	string i = 0;

			int q_q = 0;
			void swap (int q_q, int q);
			//swaps the read from input quantity into q_q
			int c = q_q;
			q_q = q;
			q = c;

	for ( i = 0; i < inventoryList.size(); i++)
	{
		if (inventoryList[i].getQuantity2 == n) //searchs for the item number 
//that the input file has just been read
		{
			
			inventoryList[i].setQuantity( inventoryList[i].getQuantity2() + q_q);	
			iventoryList.push_back( n, q, c );	//stores the new quantity along with
	//itemNumber and cost into vector

			cout << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;
			Output << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;		
		}
		else
		{
			Error <<"Error: " << n << "not in inventory"<<endl;
		}
	}
	return -1;
}

//this function will change the price of the product
double inventoryItem::getCost( string n, int q, double c )
{
	string getCost(const vector <inventoryItem> &inventoryList, string n);
	string i = 0;

			double c_c = 0;
			void swap (double c_c, double c);
			//swaps the read from input price into c_c
			int d = c_c;
			c_c = c;
			c = d;

	for ( i = 0; i < inventoryList.size(); i++)
	{
		if (inventoryList[i].getCost == n) //searchs for the item number 
//that the input file has just been read
		{
			
			inventoryList[i].setCost(c);	
			iventoryList.push_back( n, q, c );	//stores the new quantity along with
	//itemNumber and cost into vector

			cout << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;
			Output << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;		
		}
		else
		{
			Error <<"Error: " << n << "not in inventory"<<endl;
		}
	}
	return -1;
}
//this function will remove the item number of the product
void inventoryItem::erase( string n )
{
	string erase(const vector <inventoryItem> &inventoryList, string n);
	string i = 0;

	for ( i = 0; i < inventoryList.size(); i++)
	{
		if (inventoryList[i].erase == n) //searchs for the item number 
//that the input file has just been read
		{
			
			inventoryList[i].erase(c);	

		}
		else
		{
			Error <<"Error: " << n << "not in inventory"<<endl;
		}
	}
	return -1;
}

The main:

#include "Wong_inventoryItem.h"
#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>
using namespace std;

int main ()
{
	string command;		//reads commands from the input file

	string holditemNumber;		//variable to hold the item number
	int holdquantity;		//variable that will hold the return of quantity
	double holdcost;		//variable that will hold the return of the cost of product
	double holdtotalcost;	//variable that will hold the return of the total cost
	string findvecbyitemnumber(vector <inventoryItem> &L, string n);
	//searches for a vector by inventory item number
	inventoryItem iItem;	//creates inventoryItem object iItem

	ifstream Input ("input.txt");	//declares the input file "Input"
	if (!Input)
	{
		cerr <<"Cannot open the file input.txt"<<endl;
		//checks to see if input file is opened successfully
		return 0;
	}

	ofstream Output ("Wong_inventory.txt");	//declares the output file "Output"
	if (!Output)
	{
		cerr <<"Cannot open the file Wong_inventory.txt"<<endl;
		//checks to see if output file is opened successfully
		return 0;
	}
		
	ofstream Error ("Wong_log.txt");	//declares the error output file "Error"
	if (!Error)
	{
		cerr <<"Cannot open the file Wong_log.txt"<<endl;
		//checks to see if Error file is opened successfully
		return 0;
	}

While (!Input())	//keeps reading the lines of the input file until the end
{
	Input>>command;
	if (command == "add")	//if it reads add, inventoryItem will run
	{
		Input >> holditemNumber >> holdquantity >> holdcost;
		iItem.inventoryItem(holditemNumber, holdquantity, holdcost);

	}

	else if (command == "sold")	//if it reads sold, getQuantity will run
	{
		Input >> holditemNumber >> holdquantity;
		iItem.getQuantity(holditemNumber, holdquantity);
	}
	else if (command == "delievered")	//if it reads delievered, getQuantity2
		//will run
	{
		Input >> holditemNumber >> holdquantity;
		iItem.getQuantity2(holditemNumber, holdquantity);
	}

	else if (command == "priceChange")	//if it reads priceChange, getCost
		//will run
   	{
		Input >> holditemNumber >> holdcost;
		iItem.getCost(holditemNumber, holdcost);
	}
	else if (command == "remove")	//if it reads remove, 
	{
		Input >> holditemNumber;
		iItem.erase(holditemNumber);
	}
} //ends while loop
Input.close();	//we close the input file
Output.close();	//we close the output file
Error.close();	//we close the error file


	return 0;
} //end of main

And the input file:

add 123456789123 12.65 50
add 123456789123 12.65 50
add 123456789124 12.65 50
add 123456789125 12.65 50
add 123456789126 12.65 50
delivered 123456789123 50
delivered 123456789123 1
sold 123456789123 2
delivered 123456789123 1
sold 123456789123 100
sold 123456789123 99
sold 123456789123 5
remove 123456789124
sold 123456789124 5
delivered 123456789127 50
sold 123456789128 50
priceChange 123456789123 10.99
priceChange 123456789128 10.99
remove 123456789128

Can anyone help point out some errors or hints to what I'm doing wrong?

Recommended Answers

All 4 Replies

the header file is incomplete. you probably didn't post the whole thing.

Until you get much more experience never write a complete project and then compile. If you're as good as Narue, Salem, or Ancient Dragon you may get away with it, but for the rest of us mortals it is a recipe for disaster. Write a single task, or at most, a single function, and then compile. It makes it much easier to debug!

Here's a few things I see after a quick review of the program.

void inputnewItem(string n) :itemNumber(n)

The : used in this manner is the initialized list operator, however this isn't a constructor, and therefore use of the : here is an error.

void inventoryItem( string n, int q, double c ) :itemNumber(n), quantity(q), cost(c)

This is a constructor but constructors are the one function that don't have a return type. Leave the void off. Also, the initializer list is used at the time of implementation of the constructor. If the constructor is implemented inline in the header file then there should be some curly brackets at the end of this statement. Otherwise, leave the initializer list off and just have this in the header file.

inventoryItem( string n, int q, double c );


inventoryItem::inventoryItem ( string n, int q, double c ) :itemNumber(n), quantity(q), cost(c)


This is where the initialization list should be when the constructor is implemented in the cpp file.

void inventoryItem::inventoryItem ( string n, int q, double c )
{
    vector < inventoryItem > inventoryList;

This means that every inventoryItem object will have it's own vector of inventoryItem. That doesn't make sense. I would have the inventoryItem class with mutator and accessor methods for each of the variable members. However, I would have a single vector of inventoryItems per program by either declaring a vector of inventoryItem in main() and have the add, remove, change functions as stand alone functions or I would have an Inventory class and each Inventory object would have a vectory of inventoryItem variable and it would have the add, remove, change functions as public member functions.

Thanks, those were some great help. It reduced my errors by more than half!!! :cheesy: I really appreciate everyone's help. Now a problem I'm having is in my cpp file, the output section is giving me a lot of errors. Does anyone know what I'm doing wrong there? Also, the remove command I'm having difficulties with. Should I name my erase member function differently and not put void in front of it?

Thanks again for everyone's input.

The answers to those questions depend in part in how you have modified the original code you posted.

In general I like to overload the << operator using a friend function to output by accessing the private information in a given struct/class rather than writing multiple pulbic accessor functions each time I want to output the information.

Renaming the erase function isn't necessary if it's a member function but making it more specific by calling it eraseInventoryItem or something similar if the function is stand alone will reduce the risk of name clashes causes the compiler to complain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.