Question: 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 2 above commands.
Can you please help me debug. Many thanks.
Below is the "main()" portion of my program:
//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
int main(int argc, char* argv[])
{
vector allStocks;;
Stock oneSingleStock;
double totalCostFileStocks =0.00;
int totalNumStocks =0;
double totalCostSingleStock =0.00;
double totalCostAllStocks =0.00;
bool gaveGoodFileName = false;
if (argc ==1)
{
cout << "Usage: "<< argv[0]<<""<< endl;
}
if (argc ==2 && gaveGoodFileName == false)
{
string dataFileName = argv[1];
cout << "Stock data file name: "<< dataFileName << endl;
ifstream fin(dataFileName);
if (fin.is_open()== false)
{
cout << "Unable to open data file. Please check data file name: "<< dataFileName << endl;
}
else
{
allStocks = readInFile(fin, totalCostFileStocks, totalNumStocks);
totalCostAllStocks = totalCostFileStocks;
gaveGoodFileName = true;
}
}
if (gaveGoodFileName ==f true)
{
bool finishedEntering = false;
string userCmd;
while (finishedEntering == false)
{
cout << "Please enter a command: ";
getline(cin, userCmd);
stringstream ssin(userCmd);
string arg1;
ssin >> arg1;
if (arg1== "buy")
{
oneSingleStock = buyOneStock(totalCostSingleStock);
totalCostAllStocks += totalCostSingleStock;
totalNumStocks++;
allStocks.push_back(oneSingleStock);
cout << endl;
}
else if (arg1== "display")
{
displayAllStocks(allStocks);
}
else if (arg1== "summary")
{
displayStockSummary(totalNumStocks, totalCostAllStocks);
}
else if (arg1== "find")
{
string findSymbol;
ssin >> findSymbol;
displayStocksWithSymbol(allStocks, findSymbol);
}
else if (arg1== "amount>=")
{
double findAmount;
ssin >> findAmount;
displayGreaterThanStocks(allStocks, findAmount);
}
else if (arg1== "help")
{
help();
}
else if (arg1== "exit")
{
char exitChoice;
cout << "Are you sure that you want to exit the program (y/n)?";
cin >> exitChoice;
if (exitChoice =='y')
{
finishedEntering = true;
}
}
else
{
cout << "Invalid command. Type \"help\" for more info." << endl;
}
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!