I need to write a program that computes the total net profit or loss on a series
ID: 3624059 • Letter: I
Question
I need to write a program that computes the total net profit or loss on a series of stock transactions. It needs to ask the user how many transactions are involved; ask the user for the relevant data for each transaction. It needs to validate the input data, and re-prompt the user to enter valid data until valid data is entered for all variables. Report the profit or loss on each transaction, and the total on all transactions.
Your program should include, at a minimum, the following functions:
A function to get the number of shares from the user. This is a whole number and must be > 0. This function should handle prompting the user and validating the input.
A function to get the purchase price from the user. This is a floating-point number and must be > 0.
A function to get the purchase commission from the user. This is a floating-point number and must be >= 0.
A function to get the sale price from the user. This is a floating-point number and must be > 0.
A function to get the sale commission from the user. This is a floating-point number and must be >= 0.
A function that has parameters for all needed variables, and returns the net profit or loss on a particular transaction (a double).
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int gettrans();
int getShares(string);
double getPurchasePrice(string);
double getCommission(string);
double getSalePrice(string);
double getSaleCommission(string);
double getProfit(int,double,double,double,double);
int main()
{ int shares,i,trans;
double PP,Comm,SP,SComm,profit,total=0;
string name;
trans=gettrans();
for(i=0;i<trans;i++)
{cout<<" Stock #"<<i+1<<". ";
cout<<"Stock name? ";
cin>>name;
shares=getShares(name);
PP=getPurchasePrice(name);
Comm=getCommission(name);
SP=getSalePrice(name);
SComm=getSaleCommission(name);
profit=getProfit(shares,PP,Comm,SP,SComm);
total+=profit;
cout<<"Your profit on that stock was: "<<profit<<endl;
}
cout<<" Total profit for all stocks: "<<total<<endl;
system("pause");
return 0;
}
int gettrans()
{int t;
cout<<"How many stock transactions do you have? ";
cin>>t;
while(t<=0)
{cout<<"must be positive ";
cout<<"How many stock transactions do you have? ";
cin>>t;
}
return t;
}
int getShares(string n)
{int s;
cout<<"How many shares of "<<n<<" did you buy? ";
cin>>s;
while(s<=0)
{cout<<"must be positive ";
cout<<"How many shares of "<<n<<" did you buy? ";
cin>>s;
}
return s;
}
double getPurchasePrice(string n)
{double p;
cout<<"What was the price per share when you purchased "<<n<<"? ";
cin>>p;
while(p<=0)
{cout<<"must be positive ";
cout<<"What was the price per share when you purchased "<<n<<"? ";
cin>>p;
}
return p;
}
double getCommission(string n)
{double c;
cout<<"What was the commission paid when you purchased "<<n<<"? ";
cin>>c;
while(c<=0)
{cout<<"must be positive ";
cout<<"What was the commission paid when you purchased "<<n<<"? ";
cin>>c;
}
return c;
}
double getSalePrice(string n)
{double p;
cout<<"What was the price per share when you sold "<<n<<"? ";
cin>>p;
while(p<=0)
{cout<<"must be positive ";
cout<<"What was the price per share when you sold "<<n<<"? ";
cin>>p;
}
return p;
}
double getSaleCommission(string n)
{double c;
cout<<"What was the commission paid when you sold "<<n<<"? ";
cin>>c;
while(c<=0)
{cout<<"must be positive ";
cout<<"What was the commission paid when you sold "<<n<<"? ";
cin>>c;
}
return c;
}
double getProfit(int s,double pp,double c,double sp,double sc)
{return (sp*s-sc)-(pp*s+c);
}