1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//function prototype
int findStock(string *, int);
void ArrSort(string *[], int );
int main()
{
string filename;
fstream inFile;
int numOfStocks = 0;
string *TradeSymbol = nullptr, *CompanyName = nullptr;
int *numOfShares = nullptr;
double *CurrentPrice = nullptr;
char delimiter1 = ' ';
char delimiter2 = '#';
int index = 0;
cout << "User please enter the name of the file that you would like to read." << endl;
cout << "Example Format: CplusplusIsFun.txt" << endl;
cout << "\nEnter: ";
cin >> filename;
inFile.open(filename);
if (inFile.fail())
{
cout << "\nFile not found!" << endl;
exit(-1);
}
else
{
cout << "\nFile has opened successfully!" << endl;
inFile >> numOfStocks; //reading the first integer at top of text file
}
cout << "Number of different trading stocks found in file: " << numOfStocks << endl;
TradeSymbol = new string[numOfStocks], CompanyName = new string[numOfStocks]; //dynamically allocated memory for parallel arrays
numOfShares = new int[numOfStocks], CurrentPrice = new double[numOfStocks];
for ( int x = 0; x < numOfStocks; x++)
{
inFile.ignore(100000, '\n'); // skip the previous newline
getline (inFile, TradeSymbol[x], delimiter1);
getline(inFile, CompanyName[x], delimiter2);
inFile >> numOfShares[x];
inFile >> CurrentPrice[x];
}
inFile.close();
//create array of pointers
string **SortedArray = nullptr;
SortedArray = new string *[numOfStocks]; //dynamically create new array of pointers
//store address
for (int count = 0; count < numOfStocks; count++)
SortedArray[count] = &TradeSymbol[count];
ArrSort(SortedArray, numOfStocks);
index = findStock(TradeSymbol, numOfStocks);
cout << "\nCompany Name: " << right << setw(42) << CompanyName[index] << endl;
cout << "Number of Shares: " << right << setw(38) << numOfShares[index] << endl;
cout << "Current Price (per share): " << right << setw(29) << CurrentPrice[index] << endl;
double totalValue = *(numOfShares + index) * *(CurrentPrice + index);
cout << fixed << setprecision(2);
cout << "Current Value: " << right << setw(41) << totalValue << endl;
//free and release the memory
delete [] SortedArray, delete [] CompanyName, delete [] numOfShares;
delete [] CurrentPrice, delete [] TradeSymbol;
system("pause");
return 0;
}
//sort array of pointers
void ArrSort(string *iparr[], int size)
{
int startScan, minIndex;
string *minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = iparr[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (*(iparr[index]) < *minElem)
{
minElem = iparr[index];
minIndex = index;
}
}
iparr[minIndex] = iparr[startScan];
iparr[startScan] = minElem;
}
for (int count = 0; count < size; count++)
{
cout << *iparr[count] << ' ';
if (count > 0 && (count % 3) == 0)
cout << endl;
}
}
int findStock(string *TradeName, int index)
{
bool found = false;
string symbol;
cout << endl;
cout << "Enter the symbol: ";
cin >> symbol;
for (int i = 0; i < index; i++)
{
if (symbol == TradeName[i])
{
found = true;
return i;
}
}
if (found == false)
{
cout << "Symbol not found!" << endl;
exit(-2); //error message, exit with an error code of -2
}
}
|