Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I wrote the following program for buying and selling stock, and also displaying a record of these transactions. I am using command line entry. I
I wrote the following program for buying and selling stock, and also displaying a record of these transactions. I am using command line entry. I am using exceptions to be able to display an error message that either a negative price or negative number of stocks whenever a stock is either bought or sold. Afterwards, it does not record the transaction, and instead asks the user to enter another command. Afterwards, it will let me buy or sell another stock, however, afterwards, if I again try to display the list of transactions, it displays them, however, afterwards kicks me out of the program. I am not sure why. Can you please help me debug. Many thanks.
To begin the execution of the program, at the command line, I enter the following:
"TransactStock transactions where TransactStock is the name of the program, and transactions is the name of the text file that has a list of buy or sell stocks that are first imported into the program, and then the user can manually buy or sell other stocks, to add to the transaction list.
Here is the text in the transactions text file:
INTC
CSCO
IBM
AAPL
where buy, sell
the nd number is the number of shares
the rd entry is the stock code
the last number is the share price
Attached are only snippets of the exception code parts of my "TransactStock" program. I was unable to import the entire code.
class StockException : public runtimeerror
private:
string mreason;
public:
StockExceptionstring reason : runtimeerror::runtimeerrorreason
mreason reason;
string getReason
return mreason;
;
class Stock
private:
int mnumShares;
string msymbol;
double msharePrice;
public:
Stock : mnumShares msymbol msharePrice
Stockint numShares, string symbol, double sharePrice : mnumSharesnumShares msymbolsymbol msharePricesharePrice
if numShares
StockException errorStockObjError: Invalid quantity. Ignored.";
throw errorStockObj;
if sharePrice
StockException errorStockObjError: Invalid price. Ignored.";
throw errorStockObj;
void setNumSharesint numShares mnumShares numShares;
void setSymbolstring symbol msymbol symbol;
void setSharePricedouble sharePrice msharePrice sharePrice;
int getNumShares return mnumShares;
string getSymbol return msymbol;
double getSharePrice return msharePrice;
virtual string toString
stringstream ss;
ss "SYMBOL msymbol PRICE$ fixed setprecision msharePrice QUANTITY mnumShares TOTAL$ setprecisionmnumShares msharePrice;
return ssstr;
;
Stock buyOneStock
Stock buyOneStock nullptr;
string enterSymbol ;
int enterNumShares ;
double enterSharePrice ;
try
cout endl "Please enter the stock symbol: ;
cin enterSymbol;
cout endl "Please enter the number of shares: ;
cin enterNumShares;
cout endl "Please enter the share price: ;
cin enterSharePrice;
buyOneStock new BuyTransactionenterNumShares enterSymbol, enterSharePrice;
cout
The stock has been purchased successfully." endl;
return buyOneStock;
catch StockException& errObj
cout errObj.getReason endl;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started