-
March 4th, 2014, 03:31 PM
#1
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.
-
March 4th, 2014, 04:00 PM
#2
Re: Incorrect values Printed
And the debugging of this program you have done has revealed what?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 4th, 2014, 04:41 PM
#3
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.
-
March 18th, 2014, 10:11 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|