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

Thread: Incorrect values Printed

  1. #1
    Join Date
    Feb 2014
    Posts
    5

    Incorrect values Printed

    I have a piece of code that looks like this
    Basically, I have a Customer class and the Customer selects from a list of offers from a vector which is defined inside the Customer class.

    Code:
    for (vector<Customer>::iterator CIt = i_CustomersList.begin(); CIt != i_CustomersList.end(); CIt++)
    	{	for (int i = 0; i < 3; i++)
    			{
    				Offer Contract = CIt->selectOffer(i);
    				//Contract.display(); cout<<endl;
    				CIt->setCurrentContract(Contract,i);
    			}
    	}
    Now the setCurrentContract method is defined in the Customer Class as:
    Code:
    	void setCurrentContract(Offer ContractOffer, int ProdIndex)
    		{
    			cout<<endl<<"***************************************"<<endl;
    			cout<<endl<<"Displaying from set Curr. Contr. method"<<endl;
    			c_Contract[ProdIndex] = &ContractOffer;
    			c_Contract[ProdIndex]->display();
    			cout<<endl<<endl;
    			//cout<<endl<<"Selected"<<endl;
    		}
    c_Contract is declared thus

    Code:
     Offer * c_Contract[3]
    Now when I am printing the offers from the setCurrentContract method as shown above the correct contracts are being output.
    However, when I am trying to do this:

    Code:
    for (vector<Customer>::iterator CIt = i_CustomersList.begin(); CIt != i_CustomersList.end(); CIt++)
    			{
    		       for (int i = 0; i<3;i++)
    		    	   {CIt->displayContracts();}
    			}
    only the last offer is output for all the contracts.

    the displayContracts method is :

    Code:
    void displayContracts()
    		{
    
    			
    			for( int i = 0; i < 3; i++ )
    				{	cout<<endl<<"ProdIndex"<<i<<endl;
    					if(  c_Contract[i] != NULL  )
    					 	 {c_Contract[i]->display();}
    					else
    						 {cout<<endl<<"No Contract for ProdIndex: "<<i;}
    		     }
    		}
    Probably something funny is going on while assigning the contracts under the c_Contracts variable. Please help.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,366

    Re: Incorrect values Printed

    And the debugging of this program you have done has revealed what?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    Re: Incorrect values Printed

    As 2kaud said, try the debugger. I'm having trouble making sense of your code. I don't see what you're first loop is trying to accomplish. In setCurrentContract, you're storing the address of a local object, which is never good.

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Incorrect values Printed

    I applaud you for trying to use code tags, but please try to make the indenting more sensible in the future. The code within the tags is still very messy and hard to read. Also, it is a good idea to post something that others might compile. Produce an example that can be copied and pasted into an online compiler, and just maybe someone here will run it and point out an obvious problem. When you post only snips from different parts of the program, it is impossible for anyone to know what you actually compiled and executed!

    While doing the above, many people end up finding their own problem and no longer need to post a question.
    Last edited by kempofighter; March 18th, 2014 at 10:14 PM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center