stock


// Date: October 5, 2010
// Section: CS 1044: #11348
// Compiler: Visual C++ 2008 Express Edition
// Platform: Windows Vista
//
//
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<iostream>
using namespace std;
//
//
int main() {

// Constant Declarations:

const string DASHLINE = "==============================================================================================================================";
const char DATAFILE[] = "StockFile.txt";
const char RESULTSFILE[] = "Stockdata.txt";




ifstream in;
ofstream out;
double input;
in.open("StockFile.txt");

out.open(RESULTSFILE);

out<<"Alli"<<endl;
out<<"CS 1044 Project 3 Fall 2010"<<endl;
out<<endl;
out<<"Stocks Summary Table"<<endl;
out<<endl;
out<<"Company\tNumber of\tPurchase\tSelling\t\tFee\t\tDividend\tTax\t\tGain/"<<endl;
out<<"Name\tshares\t\tPrice\t\tPrice\t\t\t\t\t\t\t\tLoss"<<endl;
out<<DASHLINE<<endl;

out<<fixed<<setprecision(2);
in.ignore(255, '\n');
in.ignore(255, '\n');
in.ignore(255, '\n');
in.ignore(255, '\n');

char company[255];
double purchasePrice;
double sellingPrice;
int numberOfShares;
double BrokerageFee;
double dividend;
double TaxRate;
int MonthsHeld;

double NewDividend;
double Tax;
double GainLoss=0;
double TotalGain=0;
double TotalLoss=0;
double AverageGainLoss;
int count=0;
double TotalTax=0;
double Net=0;
double Networth;
double T;

while (!in.eof()){

in>>company>>purchasePrice>>sellingPrice>>numberOfShares>>BrokerageFee>>dividend>>TaxRate>>MonthsHeld;
NewDividend=dividend*numberOfShares;


if (MonthsHeld<=3)
T=TaxRate*2;
else if (MonthsHeld<=6)
T=TaxRate*1.5;

if (sellingPrice<purchasePrice)
Tax=0;
else
Tax=T*(sellingPrice-purchasePrice)*numberOfShares;


GainLoss=(sellingPrice-purchasePrice)*numberOfShares+NewDividend-2*BrokerageFee;

if(GainLoss>0)
TotalGain+=GainLoss;
else
TotalLoss+=GainLoss;

TotalTax+=Tax;
count++;
Networth=sellingPrice*numberOfShares-2*BrokerageFee+NewDividend-Tax;
Net+=Networth;
out<<company<<'\t'<<numberOfShares<<"\t\t$"<<purchasePrice<<"\t\t$"<<sellingPrice<<"\t\t$"<<BrokerageFee<<"\t\t$"<<NewDividend<<"\t\t$"<<Tax<<"\t\t$"<<GainLoss<<endl;

}
AverageGainLoss=(TotalGain+TotalLoss)/count;
out<<DASHLINE<<endl;
out<<"Total\t\tTotal\t\tAverage\t\tTotal\t\tNet"<<endl;
out<<"Gain\t\tLoss\t\tGain/Loss\tTax\t\tWorth"<<endl;
out<<DASHLINE<<endl;
out<<"$"<<TotalGain<<"\t$"<<abs(TotalLoss)<<"\t\t$"<<AverageGainLoss<<"\t$"<<TotalTax<<"\t$"<<Networth<<endl;


out.close();
in.close();

return 0;
}
Last edited on
Mmk?
this code reads the input file and outputs the last line twice messing up the total results. Idk how to fix. HELP?
Don't loop on eof(), because that will not be false until after you have tried and failed to read the data. Loop on good() instead.
Topic archived. No new replies allowed.