Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I wrote the following program for buying stock, and also displaying and summarizing a record of these transactions. I am using command line entry. In
I wrote the following program for buying stock, and also displaying and summarizing a record of these transactions. I am using command line entry. In the main portion of my program, I ask the user to type in a command that will let them buy another stock, display the records, summary of records, etc. Then when they are finished using the program, they type exit to leave the program after a confirmation. Everything works as it is supposed to except for when after the user either enters the "buy" or "exit" commands. Whenever this happens, the command line also outputs the following extra line:
"Please enter a command. Invalid command. Type "help" for more info."
and then afterwards, outputs "Please enter a command." like it is supposed to I am not sure why it is outputting this extra line only after entering the above commands.
Can you please help me debug. Many thanks.
Below is the "main portion of my program:
int mainint argc, char argv
vector allStocks;;
Stock oneSingleStock;
double totalCostFileStocks ;
int totalNumStocks ;
double totalCostSingleStock ;
double totalCostAllStocks ;
bool gaveGoodFileName false;
if argc
cout "Usage: argv endl;
if argc && gaveGoodFileName false
string dataFileName argv;
cout "Stock data file name: dataFileName endl;
ifstream findataFileName;
if finisopen false
cout "Unable to open data file. Please check data file name: dataFileName endl;
else
allStocks readInFilefin totalCostFileStocks, totalNumStocks;
totalCostAllStocks totalCostFileStocks;
gaveGoodFileName true;
if gaveGoodFileName f true
bool finishedEntering false;
string userCmd;
while finishedEntering false
cout "Please enter a command: ;
getlinecin userCmd;
stringstream ssinuserCmd;
string arg;
ssin arg;
if arg "buy"
oneSingleStock buyOneStocktotalCostSingleStock;
totalCostAllStocks totalCostSingleStock;
totalNumStocks;
allStocks.pushbackoneSingleStock;
cout endl;
else if arg "display"
displayAllStocksallStocks;
else if arg "summary"
displayStockSummarytotalNumStocks totalCostAllStocks;
else if arg "find"
string findSymbol;
ssin findSymbol;
displayStocksWithSymbolallStocks findSymbol;
else if arg "amount
double findAmount;
ssin findAmount;
displayGreaterThanStocksallStocks findAmount;
else if arg "help"
help;
else if arg "exit"
char exitChoice;
cout "Are you sure that you want to exit the program yn;
cin exitChoice;
if exitChoice y
finishedEntering true;
else
cout "Invalid command. Type help for more info." endl;
return ;
Here is my "buyOneStock function:
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