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;
}
Attachments
Exchange   Symbol     Company                 	     Shares  	Price  	Stock Value  
NASDAQ     CSCO       Cisco Systems Inc          	100  	19.60       1960.00  
NASDAQ     MSFT       Microsoft Corporation       	 50  	25.74       1287.00  
NASDAQ     INTC       Intel Corporation          	250  	21.09       5272.00  
NASDAQ     QQQQ       PowerShares QQQ Trust      	500  	51.75      25875.00  
NASDAQ     HGSI       Human Genome Sciences Inc  	100  	25.32       2532.00  
NASDAQ     GOOG       Google Inc                 	150    586.05      87907.00  
NASDAQ     AAPL       Apple Inc                  	400    302.84     121136.00  
NYSE       MMM        3M Company                 	100  	84.14       8414.00  
NYSE       GE         General Electric Company   	100  	15.83       1583.00  
NYSE       SI         Siemens AG (ADR)           	200    113.96      22792.00  
NYSE       DIS        The Walt Disney Company    	150  	37.22       5583.00  
NYSE       UTX        United Technologies Corp   	300  	73.52      22056.00 


SORTED RESULTS

Exchange   Symbol     Company                	     Shares   	Price  	Stock Value  
NASDAQ     APPL       Apple Inc                  	400    302.84     121136.00  
NASDAQ     GOOG       Google Inc                 	150    586.05      87907.00  
NASDAQ     QQQQ       PowerShares QQQ Trust      	500   	51.75      25875.00  
NYSE       SI         Siemens AG (ADR)           	200    113.96      22792.00  
NYSE       UTX        United Technologies Corp   	300   	73.52      22056.00  
NYSE       MMM        3M Company                 	100   	84.14       8414.00  
NYSE       DIS        The Walt Disney Company    	150   	37.22       5583.00  
NASDAQ     INTC       Intel Corporation          	250   	21.09       5272.00  
NASDAQ     HGSI       Human Genome Sciences Inc  	100   	25.32       2532.00  
NASDAQ     CSCO       Cisco Systems Inc          	100   	19.60       1960.00  
NYSE       GE         General Electric Company   	100   	15.83       1583.00  
NASDAQ     MSFT       Microsoft Corporation      	 50   	25.74       1287.00
NASDAQ,CSCO,Cisco Systems Inc,19.60,100
NASDAQ,MSFT,Microsoft Corporation,25.74,50
NASDAQ,INTC,Intel Corporation,21.09,250
NASDAQ,QQQQ,PowerShares QQQ Trust,51.75,500
NASDAQ,HGSI,Human Genome Sciences Inc,25.32,100
NASDAQ,GOOG,Google Inc,586.05,150
NASDAQ,AAPL,Apple Inc,302.84,400
NYSE,MMM,3M Company,84.14,100
NYSE,GE,General Electric Company,15.83,100
NYSE,SI,Siemens AG (ADR),113.96,200
NYSE,DIS,The Walt Disney Company,37.22,150
NYSE,UTX,United Technologies Corp,73.52,300

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

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?

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.

Attachments
ExchangeSymbol     Company                 	     Shares  	Price  	Stock Value  
NASDAQ    CSCO       Cisco Systems Inc          	100  	19.60       1960.00  
NASDAQ    MSFT       Microsoft Corporation       	 50  	25.74       1287.00  
NASDAQ    INTC       Intel Corporation          	250  	21.09       5272.00  
NASDAQ    QQQQ       PowerShares QQQ Trust      	500  	51.75      25875.00  
NASDAQ    HGSI       Human Genome Sciences Inc  	100  	25.32       2532.00  
NASDAQ    GOOG       Google Inc                 	150    586.05      87907.00  
NASDAQ    AAPL       Apple Inc                  	400    302.84     121136.00  
NYSE      MMM        3M Company                 	100  	84.14       8414.00  
NYSE      GE         General Electric Company   	100  	15.83       1583.00  
NYSE      SI         Siemens AG (ADR)           	200    113.96      22792.00  
NYSE      DIS        The Walt Disney Company    	150  	37.22       5583.00  
NYSE      UTX        United Technologies Corp   	300  	73.52      22056.00 


SORTED RESULTS

Exchange  Symbol     Company                	     Shares   	Price  	Stock Value  
NASDAQ    APPL       Apple Inc                  	400    302.84     121136.00  
NASDAQ    GOOG       Google Inc                 	150    586.05      87907.00  
NASDAQ    QQQQ       PowerShares QQQ Trust      	500   	51.75      25875.00  
NYSE      SI         Siemens AG (ADR)           	200    113.96      22792.00  
NYSE      UTX        United Technologies Corp   	300   	73.52      22056.00  
NYSE      MMM        3M Company                 	100   	84.14       8414.00  
NYSE      DIS        The Walt Disney Company    	150   	37.22       5583.00  
NASDAQ    INTC       Intel Corporation          	250   	21.09       5272.00  
NASDAQ    HGSI       Human Genome Sciences Inc  	100   	25.32       2532.00  
NASDAQ    CSCO       Cisco Systems Inc          	100   	19.60       1960.00  
NYSE      GE         General Electric Company   	100   	15.83       1583.00  
NASDAQ    MSFT       Microsoft Corporation      	 50   	25.74       1287.00

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";
This article has been dead for over six months. Start a new discussion instead.