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;
}

Your setw values in displayStockInfo() don't match your setw values in lines 81 -85, 97 - 101, so I'm not surprised things don't line up. Try using the same numbers?

I have tried that and data didn't align properly

>> I have tried that and data didn't align properly

We can't run it without the input file. You're going to have to provide the exact actual output and explain how it differs from the desired output. The Stock::displayStockInfo() function has too many setw statements in it IMO.

OK, so it looks like you attached the input and output. Output looks lined up to me. What's it supposed to look like?

OK, so it looks like you attached the input and output. Output looks lined up to me. What's it supposed to look like?

The output I posted is what it is suppose to look like, but I've played with the code for a little bit and got just about all the the data aligned.

cout << setw(10) << StockExchange << left;
	cout << setw(9) << Symbol << left;
	cout << setw(25) << Company << right;
	cout << setw(7) << Shares << right;
	cout << setw(10) << Price << right;
	cout << setw(14) << Value << left << endl;

The only problem I have is with Exchange. I attached the output that my code displays.

ExchangeSymbol Company Shares Price Stock Value

Seems like "Exchange" is lining up on the right of the column and you want it on the left. Take advantage of the "right" and "left" specifiers in iomanip. Looks like you're using setw, but not left and right:

cout << left << setw(10)<< "Exchange";

I tried what you said and it lined up perectly. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.