### Longest held Rule

Hi Everyone
I done a project in C++ where I have to deal with stocks buying and selling in stock exchange. In question, they have asked that when calculating total capital amount for one year in buying and selling should follow the longest held rule.
Input data format
dd/mm/yyyy,share_code,activity,number_of_shares,price_of_one_share,
Sample data is
 ``` 02/08/2011,ABC,buy,100,20.00, 05/08/2011,ABC,buy,20,24.00, 06/08/2011,ABC,buy,200,36.00, 15/08/2011,ABC,sell,150,30.00, 19/10/2011,ABC,sell,40,35.50, 03/11/2011,ABC,buy,100,27.25, ```

The longest held rule:
 ```As an example, suppose the user buys 100 ABC shares at \$20 on a particular day 1, 20 shares at \$24 on a day 2, 200 shares at 36 on day 3 and then sells shares at day 4 at \$30 each. Applying the longest held rule means that of the 150 shares sold on day 4, 100 were bought on day 1, 20 were bought on day 2, and 30 were bought on day 3. To calculate capital gain or loss, the price differential (i.e sale price-buy price) is multiplied by the corresponding number of shares. For this example, the calculation is (100*(30-20)+20*(30-24)+30*(30-36)), giving a capital gain of \$940. ```

What I have done so far(its quite lengthy sorry for that)
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102`` `````` #include #include #include #include using namespace std; struct data { string activity; int shares; double price; }; int main() { ifstream in("losstestfile.txt"); const int length=28; data tx[length]; for(int i=0;i>tx[i].activity>>tx[i].shares>>tx[i].price; } int start_index=0; int sell_index,share_hold=0,last_hold_index=0; int sell_count=0; double total=0,sold_amount=0,buy_amount=0; bool found; int share_left; while(start_index0) cout<<"Capital Gain for the year 2011 is \$"<

Its working fine, but I want to know whether its suitable for the given rule.
Is there any other simpler way to do it?

Thank you
Topic archived. No new replies allowed.