C++ program with pointers, review and correct code! I\'ll post the instruction a
ID: 3688927 • Letter: C
Question
C++ program with pointers, review and correct code!
I'll post the instruction and my own code below:
My own code: https://www.dropbox.com/s/b9rgd84j7ebor9z/StockLookUp.cpp?dl=0
I have a runtime error after I run the code. Also, I need the code to work for multiple stocks, more than just 5. Please revise and correct my code please!
On your own, create a modular C++ program to lookup the trading symbol in a given stock portfolio. The program will use dynamic parallel arrays to hold the stock information: trading symbol, company name, number of shares and current price. Do not use global variables or constants in your solution. Stock portfolio information will be read from file and can contain any number of stocks. The first line of the file contains an integer identifying the remaining number of stocks to read from the file. Each remaining line in the file contains data for a single stock (data types in parentheses): trading symbol (multiple characters), company name (multiple characters with embedded spaces and end delineated by #), number of shares (integer), and current price (real) A sample stock file is given below MCD McDonald's Corporation# 35 99.25 HD The Home Depot, Inc.# 31 115.54 F Ford Motor Co.# 288 16.49 WMT Wal-Mart Stores Inc.# 10 83.33 BA The Boeing Company# 24 151.46 a) Create the source code file StockLookUp.cpp. Add a block comment at the top of the file to identify your name, file, date, class, assignment, and short description of the program b) Include the following design requirements in your code Prompt and read a filename from the user. If there is an error opening the file, exit the program with an error code of -1 . If the file is successfully opened for input, use the integer on the first line to dynamically create parallel arrays of the appropriate size and data type for each of the stock fields trading symbol, company name, number of shares, and current price. Use either C++Explanation / Answer
/*
FILE: StockLookUp.cpp
This program will take in a stock file and display the Company information according to the symbol.
*/
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void arrSelectSort(string arr[], int size);
int Search(string symbol, string *arr, int size);
int main()
{
fstream file;
string filename;
cout << "Please enter the file location: ";
getline(cin, filename);
file.open(filename.c_str(), ios::in);
if (file.fail())
{
cout << "Couldn't open file " << filename << " for input!" << endl;
exit(-1);
}
int size = 0;
file >> size;
string *symbol = new string[size];
string *cName = new string[size];
int *shares = new int[size];
double *price = new double[size];
for (int i = 0; i < size && !file.eof(); i++)
{
file >> symbol[i];
getline(file, cName[i], '#');
file >> shares[i];
file >> price[i];
}
string *sortedSymbol = new string[size];
for (int i = 0; i < size; i++)
{
sortedSymbol[i] = symbol[i];
cout << sortedSymbol[i] << endl;
}
arrSelectSort(sortedSymbol, size);
cout << "Available stocks: " << endl;
for (int i = 0; i < size; i++)
{
cout << (sortedSymbol[i]) << " ";
if ((i + 1) % 3 == 0)
{
cout << endl;
}
}
cout << endl;
cout << "Please enter the trading symbol: ";
string symbolInput;
cin >> symbolInput;
int foundIndex = Search(symbolInput, symbol, size);
cout << fixed << setprecision(2);
cout << left << setw(30) << "Company Name: " << right << setw(30) << cName[foundIndex] << endl;
cout << left << setw(30) << "Number of Shares: " << right << setw(30) << shares[foundIndex] << endl;
cout << left << setw(30) << "Current Price (per share): " << right << setw(30) << price[foundIndex] << endl;
cout << left << setw(30) << "Current Value: " << right << setw(30) << shares[foundIndex] * price[foundIndex] << endl;
file.close();
return 0;
}
void arrSelectSort(string *arr, int size)
{
int startScan, minIndex;
string minElem = "";
for (startScan = 0; startScan < (size - 1); startScan++) {
minIndex = startScan;
minElem = arr[startScan];
for (int index = startScan + 1; index < size; index++) {
if ((arr[index]) < minElem) {
minElem = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minElem;
}
}
int Search(string symbol, string *arr, int size)
{
string *ptrArray = arr;
bool found = false;
int value = 0;
for (int i = 0; i < size; i++)
{
if (ptrArray[i] == symbol)
{
value = i;
found = true;
break;
}
}
if (found)
{
return value;
}
else
{
cout << "Error, trading symbol not found!" << endl;
exit (-2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.