I'm having a little bit of trouble. I have been working on this code for a while and no matter what I do I just can't seem to align the data correctly. I can really use some help. The information for the stocks and the output of the program is attached to this post. To get the stocks information to display copy & paste the file stocks into the folder with the code.
#include "stdafx.h" // Microsoft specific
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// declare Stock Class
class Stock
{
private:
string StockExchange;
string Symbol;
string Company;
double Price;
int Shares;
double Value;
public:
Stock();
Stock(string stockExchange, string symbol, string company,double price, int shares);
void displayStockInfo();
void setStockInfo(string stockExchange, string symbol, string company,double price, int shares);
bool operator< (const Stock & aStock) const;
}; // end Stock Class declaration
// function prototype
void cr(int nLines); // Output line spacing
template<typename T> void sort(T list[], int listSize); // sort data
int _tmain(int argc, _TCHAR* argv[])
{
// Display heading
cout << "STOCKS READ FROM FILE" << endl << endl;
// Declaration Section
int arraySize = 0; // used to count number of stock input
Stock exchanges[13]; // create an array of Stock objects
Stock tempExchanges[1];
// temp variables used to input Stock data from a file
string tempStockExchange;
string tempSymbol;
string tempCompany;
string tempPrice;
string tempShares;
ifstream inFile("stocks.txt"); // open file stream for input
// early exit
// verify file opened correctly; otherwise display error, & quit
if (inFile.fail())
{
cout << "ERROR opening data file." << endl;
return -1;
}
// read the contents of the file into an array of Stock objects
// objects are created dynamicly within the loop
while(!inFile.eof()) // Continue if not end of file
{
// input Stock record data from file
getline(inFile, tempStockExchange, ','); // read thru ,
getline(inFile, tempSymbol, ','); // read thru ,
getline(inFile, tempCompany, ','); // read thru ,
getline(inFile, tempPrice, ','); // read thru ,
getline(inFile, tempShares); // read thru newline
exchanges[arraySize].setStockInfo(tempStockExchange, tempSymbol, tempCompany,
atof(tempPrice.c_str()), atoi(tempShares.c_str()));
++arraySize; // increment number of stock input
} // end while
inFile.close(); // close the file
//Display original results
cout << "Exchange" << setw(8);
cout << "Symbol" << setw(10);
cout <<"Company" << setw(25);
cout << "Shares" << setw(10);
cout << "Price" << setw(14);
cout << "Stock Value" << endl;
for(int i = 0; i < arraySize; i++)
exchanges[i].displayStockInfo();
cr(3);
// Display heading
cout << "STOCKS SORTED BY VALUE" << endl << endl;
//Display sorted results
cout << "Exchange" << setw(8);
cout << "Symbol" << setw(10);
cout <<"Company" << setw(25);
cout << "Shares" << setw(10);
cout << "Price" << setw(14);
cout << "Stock Value" << endl;
for (int i = 0; i < 12; i++)
{
// Find the minimum in the list[i..listSize-1]
tempExchanges[0] = exchanges[i];
int currentMinIndex = i;
for (int j = i + 1; 12 > j; j++)
{
if ((exchanges[j] < tempExchanges[0])== false )
{
tempExchanges[0] = exchanges[j];
currentMinIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i)
{
exchanges[currentMinIndex] = exchanges[i];
exchanges[i] = tempExchanges[0];
}
}
for(int i = 0; i < arraySize; i++)
exchanges[i].displayStockInfo();
cr(3);
return 0; // normal exit
} // end main()
Stock::Stock()
{
// no arg Constructor
}
Stock::Stock(string stockExchange, string symbol, string company, double price, int shares)
{
StockExchange = stockExchange;
Symbol = symbol;
Company = company;
Price = price;
Shares = shares;
Value = shares * price;
}
void Stock::displayStockInfo()
{
cout << StockExchange;
cout << setw(8) << Symbol << setw(18);
cout << setw(12) << Company << setw(9);
cout << setw(15) << Shares << setw(5);
cout << setw(15) << Price << setw(5);
cout << setw(10) << Value << endl;
}
void Stock::setStockInfo(string stockExchange, string symbol, string company, double price, int shares)
{
StockExchange = stockExchange;
Symbol = symbol;
Company = company;
Price = price;
Shares = shares;
Value = shares * price;
}
// Less than
bool Stock::operator< (const Stock & aStock) const
{
return (Value < aStock.Value);
}
// Output line spacing
void cr(int nLines)
{
for (int i = 0; i < nLines; i++)
cout << endl;
}