Thread: Can someone look over my code? part 2

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

    Can someone look over my code? part 2

    I think I did what everyone said in my other thread and once again, reduced a bunch of errors. I started with 200+ errors but I'm down to around 40. Most of it has to do with the output part in my first function in my cpp file. Another part that is giving me problems now is the last function. I have the errors of that function in the code. My compiler tells me that most of my errors are in the cpp file. Here is what I have now:

    header file:
    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    
    class inventoryItem
    {
    
    private:
    	
    	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
    	string itemNumber;			//a variable that is set when the item is created
    
    
    
    
    
    public:
    
    	/*adds a new item and set itemNumber along with quantity and cost*/
    	inventoryItem( string n, int q, double c );
    	inventoryItem::inventoryItem ( string n, int q, double c ) 
    		:itemNumber(n), quantity(q), cost(c)
    
    	void string erase(string n) {itemNumber = n;}	//it will delete the item number
    	void setQuantity(int q) {quantity = q;}	//modifies the quantity of inventory
    	void setCost(double c) {cost = c;}	//modifies the cost of the inventory		
    	int getItemNumber() {return itemNumber;}	//gets the item number of the inventory
    	int getQuantity() {return quantity;}		//gets the quantity of the inventory (sold)
    	int getQuantity2() {return quantity2;}		//gets the quantity of the inventory (delievered)
    	double getCost() {return cost;}		//gets the cost of the inventory
    	double getTotalPrice();	//cost*quantity
    
    };
    main file:
    Code:
    #include <iostream>
    #include "Wong_inventoryItem.h"
    #include <fstream>
    #include <string>
    
    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
    
    
    	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;
    	}
    
    		inventoryItem iItem;	//creates inventoryItem object iItem
    
    While (!Input.eof())	//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
    cpp file:
    Code:
    #include "Wong_inventoryItem.h"
    #include <fstream>
    
    
     inventoryItem::inventoryItem ( string n, int q, double c )
    	{
    		vector < inventoryItem > inventoryList; //vector to store the inventory item
    	
    		inventoryItem myItem(n,q,c);
    		inventoryList.push_back(myItem);	//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
     inventoryItem::getQuantity( inventoryItem(string n, int q)
    {
    	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 == << c <<"\t" << c * q <<endl;		
    		}
    		else
    		{
    			Error <<"Error: " << n << "not in inventory"<<endl;n) //searchs for the item number 
    											   //that the input file has just been read
    		{
    			
    			inventoryList[i].setQuantity( inventoryList[i].getQuantity() - q_q);	
    		
    			inventoryItem myItem(n,q,c);
    		
    			inventoryList.push_back(myItem);	//adds inventory number to vector
    												//itemNumber and cost into vector
    
    			cout << n << "\t" << q << "\t" << c <<"\t" << c * q <<endl;
    			Output << n << "\t" << q << "\t" 
    		}
    	}
    	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);	
    			inventoryItem myItem(n,q,c);
    			inventoryList.push_back(myItem);	//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, 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);	
    			inventoryItem myItem(n,q,c);
    			inventoryList.push_back(myItem);	//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;
    }
    
    
    /*
    The errors of the following function are:
    error C2039: 'erase' : is not a member of 'inventoryItem'
    error C2065: 'inventoryList' : undeclared identifier
    error C2228: left of '.size' must have class/struct/union type
    error C2593: 'operator =' is ambiguous
    error C2676: binary '++' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
    error C2228: left of '.erase' must have class/struct/union type
    errorC3861: 'inventoryList': identifier not found, even with argument-dependent look up
    
    */
    
    
    
    
    
    
    //this function will remove the item number of the product
    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(n);		
    		}
    		else
    		{
    			Error <<"Error: " << n << "not in inventory"<<endl;
    		}
    	}
    	return -1;
    }
    input:
    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
    I don't understand why it said erase is not part of inventoryItem. I have it in my header file.
    Last edited by brooklyn; 04-16-2006 at 04:58 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I think I did what everyone said in my other thread and once again, reduced a bunch of errors. I started with 200+ errors but I'm down to around 40.
    I just added some pointers.

    The bottom line is if you had 200 errors with your program, you should have started over. Even with 40 errors, you should start over. You really shouldn't be writing programs that are over 30 lines of code at this point in your learning process.

    In any case, you should program one function at a time and make sure the first function works by testing it thoroughly before writing another function. So, I suggest you start over, copy and paste one function into your file, and test it until it works. Once you get the first function to work, copy and paste the next function into your file, and test it until you get it to work, etc.
    Last edited by 7stud; 04-16-2006 at 05:02 AM.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    One other small point, creating a program of sucj magnitude as a beginner can be quite over-whelming. Like 7stud said, take it a bit at a time, decipher any errors you get then re-compile. As you get more confident, you can create larger programs without compiling too many times during the creatiion, and you learn what will and what will not create an error.

    The golden rule I was taught was " compile when you are unsure of somthing, even if it looks correct" this way, you will reduce the amount of errors you produce at compile time

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> void string erase
    Is the return type void or string? Pick one, you can't have both.

    >> inventoryItem::erase( string n )
    Where is the return type here? Put one in (whichever you choose above).

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    well, for one iItem.getquantity() doesn't exist as a funtion. iItem.<insert function> doesn't exist, because iItem doesn't exist ( that and the fact that in your declaration of the functions inventoryitem is the function surname ( that's my name for it, it's the precedeing name before the function name ) and doesn't exist in your app just because you happen to include it's header file ). So i would bet most of the time it's iItem that causes problems huh? Also, an easier way to do header files and functions in general is this:

    p.s. I won't use includes, I think you know them well enough

    Header file:
    Code:
     
    //C89c
    //Sample
     
     
    /*by using the namespace command you only have to use sandwich in the header file and can then use using namespace sandwich in any cpp files, technically you can even just use the namespace command and just type functions, but calrity never hurt anyone*/
     
    namespace sandwich{
     
    void sandwich::sandwichtype( string sandtype );
     
    //I was hungry when I wrote this.
     
    //end of header.
    }
    Cpp
    Code:
    //c89c
    //sample
     
    using namespace sandwich;
     
    	 sandwichtype( string sandtype ){
     
    		 cout << "eating a " << sandtype << " sandwich huh. good?\n\n";
     
    	 }
    P.S. i finally saw where the errors where, okay, it explains what i'm talking about with iItem. see you function name would really be inventoryitem::<insert one of your functions here>
    Last edited by c89c; 04-16-2006 at 04:27 PM. Reason: gramaticall and code mistakes

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    Thumbs up

    Quote Originally Posted by 7stud
    I just added some pointers.

    The bottom line is if you had 200 errors with your program, you should have started over. Even with 40 errors, you should start over. You really shouldn't be writing programs that are over 30 lines of code at this point in your learning process.

    In any case, you should program one function at a time and make sure the first function works by testing it thoroughly before writing another function. So, I suggest you start over, copy and paste one function into your file, and test it until it works. Once you get the first function to work, copy and paste the next function into your file, and test it until you get it to work, etc.
    I agree, if you really had 200+ errors, you'd do better to start over, however your program is fixable from what I saw ( I still recommend starting over ).

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    about .erase and .size

    okay, after reading the code for 15 mins ( p.s. use more comments ) i have figured out why .erase and .size will not work. Simply, inventorylist doesn't contain anything, because vector <invetoryitem> inventorylist isn't valid. It won't cause errors, until you try to use inventorylist in anyway. to use vector properly it must be:
    Code:
    vector <int/sting/char/other variable type> <name of vector>
     
    //example
     
    vector <int> inventortlist
     
    vector <char> inventorylist
     
    vector <string> inventorylist
     
    vector <any c++ variable type> inventorylist
     
    //replace inventorylist with your name
    the above is somewhat relative after what i noticed, however the real problem is that inventorylist doesn't exist. you see, when you started vector < inventoryitem > inventorylist it only applied to that function, and still only applies to that function the subsequent times you call it.
    Last edited by c89c; 04-16-2006 at 04:40 PM. Reason: noticed something

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Thanks everyone for their input. I'm down to 6 errors (yay!!! from triple digit error to single digit error) now which I think was a huge step from 200+ errors... Next time I have an assignment, I'll take 7stud's advice and work one function at a time. I wouldn't have made it this far without the generosity of everyone's help. Some of them were stupidity on my part. For my while loop in main, I had:
    Code:
    While (!Input.eof())
    //instead of
    while (!Input.eof())



    What does it mean when an error tells me that an overloaded member function not found in inventoryItem? I have that for 3 of my functions



    P. S. This assignment is due in less than 90 minutes anyways. Having 6 errors in a program I think will get me a better grade than a program of 200+ errors. So thanks a lot again for everyone's help!!!
    Last edited by brooklyn; 04-16-2006 at 08:43 PM.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    Talking

    Quote Originally Posted by brooklyn
    Thanks everyone for their input. I'm down to 6 errors (yay!!! from triple digit error to single digit error) now which I think was a huge step from 200+ errors... Next time I have an assignment, I'll take 7stud's advice and work one function at a time. I wouldn't have made it this far without the generosity of everyone's help. Some of them were stupidity on my part. For my while loop in main, I had:
    Code:
    While (!Input.eof())
    //instead of
    while (!Input.eof())



    What does it mean when an error tells me that an overloaded member function not found in inventoryItem? I have that for 3 of my functions



    P. S. This assignment is due in less than 90 minutes anyways. Having 6 errors in a program I think will get me a better grade than a program of 200+ errors. So thanks a lot again for everyone's help!!!
    it usually means you have a variable of some data type in your function that's either extra, the wrong type as defined in your declaration, or that doesn't contain the information you promised the compiler and computer it would.

    hope it helps.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What does it mean when an error tells me that an overloaded member function not found in inventoryItem? I have that for 3 of my functions
    It means you declared a function like this:
    Code:
    class inventoryItem
    {
    public:
    
       int someFunc(int num1, string str);
    
       ...
    };
    which says the function someFunc() is expecting two arguments, where the first argument has to be an int and the second argument has to be a string type. But you are calling someFunc() like this:
    Code:
    int num = 10;
    double weight = 6.5;
    inventoryItem myInvItem;
    
    myInvItem.someFunc(num, weight);
    Last edited by 7stud; 04-17-2006 at 01:00 AM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    P. S. This assignment is due in less than 90 minutes anyways. Having 6 errors in a program I think will get me a better grade than a program of 200+ errors. So thanks a lot again for everyone's help!!!
    That's too bad because if you had started over instead of spending all your time trying to track down 200 errors, you would have finished in time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Skipping part of the code.
    By And3rs in forum C Programming
    Replies: 38
    Last Post: 10-01-2008, 01:52 PM
  2. Code writing issues Part 2...
    By jaybo684 in forum C++ Programming
    Replies: 10
    Last Post: 08-01-2005, 08:28 AM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM