CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Debugging error

  1. #1
    Join Date
    Feb 2010
    Posts
    0

    Debugging error

    i m doing my sch project and i hit this error, i didnt know how to solve it. anione can help mi? i think the problem is the array or my q[] & p[].
    THANKS

    this is my outfile:
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    void main()
    {
    ofstream outFile;
    outFile.open("C:\\Users\\hp\\Desktop\\testing.txt");
    outFile << "Mini Mart Items List" << endl<<endl;
    outFile << "Barcode		Product Description		Unit Price		Quanity"<<endl;
    outFile << "1000		Orange_Juice			$1.50			1000"<<endl;
    outFile << "1001		Dulex_Condom			$3.70			200"<<endl;
    outFile << "1002		M&M_Chocolate_100g		$1.20			230"<<endl;
    outFile << "1003		Full_Scape_paper		$1.20			2000"<<endl;
    outFile << "1004		Percy_Piggy_Sweets		$3.90			150"<<endl;
    outFile << "1005		Potato_Chips_150g		$2.00			500"<<endl;
    outFile << "1006		Pokie_Strawberry		$0.70			1500"<<endl;
    outFile << "1007		Next_Mentol_light		$10.90			200"<<endl;
    outFile << "1008		Tissue				$0.10			2000"<<endl;
    outFile << "1009		Tiger_Beer_Can			$3.50			4800"<<endl;
    outFile.close();
    }
    
    this is my main programme
    
    #include <iostream>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <istream>
    #include <iomanip>
    using namespace std;
    
    class cashier
    {
    	protected:
    		string a;
    		int code;
    		int quantity[9];
    		double price[9], total;
    		string continued;
    	public:
    	
    		int cash(string, int,double);
    		void showtext(void);
    		void buying(void);
    		void totalamount(void);
    };
    
    int	cashier::cash(string cont,int c,double t)
    		{
    			continued = cont;
    			code=c;
    			total=t;
    			
    			
    		}
    
    void cashier::showtext()
    {
    
    	ifstream infile("C:\\Users\\hp\\Desktop\\testing.txt",ios::in);
    	char ch;
    	while(!infile.eof())
    	{
    	infile.get(ch);
    	cout << ch;
    	}
    	infile.close();
    
    }
    
    
    void cashier::buying()
    {
    
    			
    ifstream infile("C:\\Users\\hp\\Desktop\\testing.txt",ios::in);
    			
    continued="y";
    while(continued == "y")
    {	
    	
    	cout<<"Please Enter Barcode ";
    	cin>>code;
    	if(code==1000)
    	{
    		cout<<"Quantity ";
    		cin>>q[0];
    		infile.seekg(80);
    		infile>>a;
    		cout<<a;
    		infile.seekg(94);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile>>a;
    		infile.seekg(97);
    		infile>>p[0];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[0]<<endl;
    	}
    	else if(code==1001)
    	{
    		cout<<"Quantity ";
    		cin>>q[1];
    		infile.seekg(116);
    		infile>>a;
    		cout<<a;
    		infile.seekg(131);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile>>a;
    		infile.seekg(132);
    		infile>>p[1];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[1]<<endl;
    	}
    	else if(code==1002)
    	{
    		cout<<"Quantity ";
    		cin>>q[2];
    		infile.seekg(150);
    		infile>>a;
    		cout<<a;
    		infile.seekg(170);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile.seekg(171);
    		infile>>p[2];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[2]<<endl;
    	}
    	else if(code==1003)
    	{
    		cout<<"Quantity ";
    		cin>>q[3];
    		infile.seekg(188);
    		infile>>a;
    		cout<<a;
    		infile>>a;
    		infile.seekg(207);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile.seekg(208);
    		infile>>p[3];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[3]<<endl;
    	}
    	else if(code==1004)
    	{
    		cout<<"Quantity ";
    		cin>>q[4];
    		infile.seekg(226);
    		infile>>a;
    		cout<<a;
    		infile.seekg(247);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile>>a;
    		infile.seekg(248);
    		infile>>p[4];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[4]<<endl;
    	}
    	else if(code==1005)
    	{
    		cout<<"Quantity ";
    		cin>>q[5];
    		infile.seekg(265);
    		infile>>a;
    		cout<<a;
    		infile.seekg(285);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile>>a;
    		infile.seekg(286);
    		infile>>p[5];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[5]<<endl;
    	}
    	else if(code==1006)
    	{
    		cout<<"Quantity ";
    		cin>>q[6];
    		infile.seekg(304);
    		infile>>a;
    		cout<<a;
    		infile.seekg(322);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile>>a;
    		infile.seekg(324);
    		infile>>p[6];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[6]<<endl;
    	}
    	else if(code==1007)
    	{
    		cout<<"Quantity ";
    		cin>>q[7];
    		infile.seekg(342);
    		infile>>a;
    		cout<<a;
    		infile.seekg(361);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile>>a;
    		infile.seekg(362);
    		infile>>p[7];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[7]<<endl;
    	}
    	else if(code==1008)
    	{
    		cout<<"Quantity ";
    		cin>>q[8];
    		infile.seekg(380);
    		infile>>a;
    		cout<<a;
    		infile.seekg(391);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile.seekg(392);
    		infile>>p[8];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[8]<<endl;
    	}
    	else if(code==1009)
    	{
    		cout<<"Quantity ";
    		cin>>q[9];
    		infile.seekg(410);
    		infile>>a;
    		cout<<a;
    		infile.seekg(428);
    		infile>>a;
    		cout<<" "<<a<<endl;
    		infile>>a;
    		infile.seekg(429);
    		infile>>p[9];
    		cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<p[9]<<endl;
    	}
    	else
    	{
    		cout<<"Error!! Wrong Barcode!! Please Reenter!"<<endl;
    	}
    
    	while(continued== "n");
    	{
    	cout<<"Do you have another item? y/n"<<endl;
    	cin>>continued;
    	if (continued== "y"||"n")
    	{
    		cout<<endl;
    	}
    	else
    	{
    		cout<<"Error!"<<endl;
    	}
    }
    }
    infile.close();
    }
    
    void cashier::totalamount(void)
    {
    
    	total=q[0]*p[0]+q[1]*p[1]+q[2]*p[2]+q[3]*p[3]+q[4]*p[4]+q[5]*p[5]+q[6]*p[6]+q[7]*p[7]+q[8]*p[8]+q[9]*p[9];
    	cout<<"Total: $"<<total;
    }
    
    class stock : public cashier
    {
    	protected:
    		int one,two,three,four,five,six,seven,eight,nine,ten;
    		string b;
    	public:
    		void stockup(void);
    };
    
    void stock::stockup(void)
    {
    	
    	ifstream infile("C:\\Users\\hp\\Desktop\\testing.txt",ios::in);
    	ofstream outFile;
    	outFile.open("C:\\Users\\hp\\Desktop\\stock.txt");
    
    	infile.seekg(103);
    	infile>>one;
    	one=one-q[0];
    	if(one<10)
    	{
    		infile.seekg(80)>>b;
    		outFile << b << endl;
    	}
    
    	infile.seekg(137);
    	infile>>two;
    	two=two-q[1];
    	if(two<10)
    	{
    		infile.seekg(114)>>b;
    		outFile<<b<<endl;
    	}
    
    	infile.seekg(175);
    	infile>>three;
    	three=three-q[2];
    	if(three<10)
    	{
    		infile.seekg(150)>>b;
    		outFile<<b<<endl;
    	}
    	
    	infile.seekg(212);
    	infile>>four;	
    	four=four-q[3];
    	if(four<10)
    	{
    		infile.seekg(188)>>b;
    		outFile<<b<<endl;
    	}
    
    	infile.seekg(252);
    	infile>>five;
    	five=five-q[4];
    	if(five<10)
    	{
    		infile.seekg(226)>>b;
    		outFile<<b<<endl;
    	}
    
    	infile.seekg(293);
    	infile>>six;
    	six=six-q[5];
    	if(six<10)
    	{
    		infile.seekg(266)>>b;
    		outFile<<b<<endl;
    	}
    
    	infile.seekg(330);
    	infile>>seven;
    	seven=seven-q[6];
    	if(seven<10)
    	{
    		infile.seekg(304)>>b;
    		outFile<<b<<endl;
    	}
    
    	infile.seekg(370);
    	infile>>eight;
    	eight=eight-q[7];
    	if(eight<10)
    	{
    		infile.seekg(342)>>b;
    		outFile<<b<<endl;
    	}
    
    	infile.seekg(398);
    	infile>>nine;
    	nine=nine-q[8];
    	if(nine<10)
    	{
    		infile.seekg(380)>>b;
    		outFile<<b<<endl;
    	}
    	
    	infile.seekg(436);
    	infile>>ten;
    	ten=ten-q[9];
    	if(ten<10)
    	{
    		infile.seekg(410)>>b;
    		outFile<<b<<endl;
    	}
    	outFile.close();
    	infile.close();
    
    }
    
    void main()
    {
    	
    	cashier minimart;
    	stock left;
    	minimart.cash(" ", 0, 0);
    	minimart.showtext();
    	minimart.buying();
    	minimart.totalamount();
    	left.stockup();
    }
    Last edited by Andreas Masur; February 15th, 2010 at 08:10 AM. Reason: Added code tags...

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Debugging error

    Quote Originally Posted by daisuke25 View Post
    i m doing my sch project and i hit this error, i didnt know how to solve it. anione can help mi? i think the problem is the array or my q[] & p[].
    It's more than that. The entire program makes little sense.

    Where do you get these magic numbers, such as 188, 247, 131 etc.? Why are you using seekg() in this program?

    What you should do is create an array (vector) of customer data, and read the entire line, extracting the data, adding this data to the array / vector. All of those lines in the file have all the fields separated with spaces. There is no need for you to count spaces and figure out where each column starts.

    What if there were 1000 items in that file? Would you write 1000 different else() statements with more magic numbers in the seekg() call? If you did that, you would be up all night counting spaces, no sleep, and eventually quit writing programs (I would). You would go nuts doing this.

    Also, the main() function returns int, not void. It's "int main()", not "void main()".
    Code:
    #include <vector>
    #include <string>
    #include <fstream>
    
    struct Customer
    {
        int barcode;
        std::string product_description;
        std::string unit_price;
        int quantity;
    };
    
    typedef std::vector<Customer> CustomerArray;
    This is where you start. You create an array (or vector) of Customer, and fill them in with the customer data that you read in a loop.
    Code:
    void ReadCustomerData( CustomerArray& customerData )
    {
        string line;
        ifstream infile("C:\\Users\\hp\\Desktop\\testing.txt",ios::in);
        Customer theCustomer;
        int i = 0;
        while(!inFile.eof())
        {
            infile >> theCustomer.barcode >> theCustomer.product_description >> theCustomer.unit_price >> theCustomer.quantity;
             customerData.push_back( theCustomer );
        }
    }  
    
    CustomerArray myCustomers;
                    
    int main()
    {
        //... now read the file into customerData
        ReadCustomerData( myCustomers );
        // now finish your program.
    }
    Now, if you state that "you must do things this the way I did them", you may get very little if any help. No programmer would sit there and count every single space in the file, and do this for every single line. Again, that's crazy, and no one will waste their time doing that.

    If you can't use vector, then use a fixed size array of Customer if that is required. But in no way should you proceed with the current code you have -- if you have to, then get another teacher.

    Also, I would start removing the headings from the output file. Once you can read a file without all of those decorations in it, then figure out how to skip the headings (the code I wrote will not work with that heading in the file).

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured