Thread: Can someone look over the program I wrote?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    12

    Unhappy Can someone look over the program I wrote?

    Thanks to zuk and Daved and elad for giving me some pointers and insights. Like one of them said to get the idea before I start putting them into code, I did and they're in code. Except the codes are really I guess. 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:

    Code:
    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:
    Code:
    #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:
    Code:
    #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:
    Code:
    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

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Can you give us an idea of where and what the errors are? No one wants to sit here and handpick them out and I know that no one is going to compile the whole project.
    Sent from my iPadŽ

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    On a different note, to save a little time, place all your includes in your header file. that way all you have to include is WrongInventoryItems.h in a file you create

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Thanks for the help, that reduced my error list by a third actually. One of the many problems I'm having is
    Code:
    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;
    }
    I get 12 error C2784 with the line starting with where it says output. It's in my calculation file. Also with my header, there is something not right about:

    Code:
    	void erase(string n) (itemNumber = n;)
    That function is suppose to remove itemNumber towards the end of the calculation file.
    Last edited by brooklyn; 04-15-2006 at 04:40 AM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You should first get rid of the errors in the header. That should reduce the list quite a lot.
    Code:
            // a method cannot have two returntypes
    	// void int setQuantity();	        //modifies the quantity of inventory
            void setQuantity( int new_quantity );	//modifies the quantity of inventory
            
            //void double setCost();	        //modifies the cost of the inventory		
            void setCost( double new_cost );	//modifies the cost of the inventory
    Kurt

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    inventoryList.push_back( n, q, c);	//adds inventory number to vector
    Considering you made a vector of inventoryItems, I'd say you don't want to push a string, an int, and a double into your vector. You probably want:
    Code:
    inventoryItem myItem(n,q,c);
    inventoryList.pushback(myItem);
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> On a different note, to save a little time, place all your includes in your header file. that way all you have to include is WrongInventoryItems.h in a file you create

    This is actually not the best practice. In the long run it won't even save time as it will make compilation take longer.

    >> inventoryList.push_back( n, q, c); //adds inventory number to vector
    inventoryList.push_back(inventoryItem(n, q, c)); //adds inventory number to vector

    You should be compiling as you go, so you never have too many errors to fix at once, and so you know what changes you made that led to the errors.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) By convention, class names are capitalized.

    2)
    Quote Originally Posted by brooklyn
    Thanks for the help, that reduced my error list by a third actually. One of the many problems I'm having is
    Code:
    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;
    }
    I get 12 error C2784 with the line starting with where it says output. It's in my calculation file.
    What is 'Output'? It's not the name of a class member variable, and it's not declared anywhere in your function.


    Also with my header, there is something not right about:

    Code:
    	void erase(string n) (itemNumber = n;)
    That function is suppose to remove itemNumber towards the end of the calculation file.
    In C++, braces and a parentheses are different symbols and they aren't interchangeable.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Oh, output is suppose to be part of the fstream that prints out to an output.txt. I have it in my main and I named it output. Oh, and I didn't realize that it wasn't in curly brackets. Thanks a lot.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by brooklyn
    I have it in my main and I named it output.
    Which isn't worth much if you don't pass it to the function:
    Code:
    void inventoryItem::inventoryItem ( ofstream& Output, 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;
    }
    Sent from my iPadŽ

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. wrote a program; please review/comment
    By spirited in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2003, 01:37 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM