problem with loading array of class objects

This is a discussion on problem with loading array of class objects within the C++ Programming forums, part of the General Programming Boards category; I have made a stock class. I load the information from a .cvs file into array of stockObjects. I print ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Post problem with loading array of class objects

    I have made a stock class. I load the information from a .cvs file into array of stockObjects. I print out the data and then sort it by value then print it out again. Everything works but at the bottom of both list it prints one extra line of data except for the first data item wich is a string. It did this before I ever add the sort and reprinted. Code someone please tell me what I have done wrong. I am assuming that there is a problem with the way I loaded the file into the array of objects.

    Code:
    #include <iostream> // needed for standared input output
    #include <string> // needed in order to use strings
    #include <fstream> // needed for reading from files
    #include <iomanip> // needed for formatting output stream
    #include "stdafx.h" // need for standard header referecne
    
    
    
    using namespace std;
    
    // declare Stock Class
    class Stock
    {
    private:
    	// initalize stock class variables
    	string StockExchange;
    	string Symbol;
    	string Company;
    	int Shares;
    	double Price;
    	double Value;
    public: 
    	// intialize stock class member functions
    	Stock(); 
    	Stock( string exchange, string symbol, string company, int shares, double price);
    	void displayStockInfo();
    	void setStockInfo(string exchange, string symbol, string company, int shares, double price);
    	bool operator< (const Stock& second) const;
    	
    }; 
    
    /*********************************   main()  *******************************/
    int main()
    {
    	// Declaration Section 
    	int arraySize=0;		// used to count number of Stock input
    	char *endp;				// used with strtod() for index after number 
    	
    
    	Stock stockObject[13];	// create an array of Stock objects
    	Stock tempStockObject[1]; // creat temp array of stock object for sorting
    
    	// temp variables used to input Stock data from file
    	string tempExchange; 
    	string tempSymbol;
    	string tempCompany;	
    	string tempShares;
    	string tempPrice;
    	
    
    	ifstream fin("stocks.csv");		// open file stream for input
    	
    	// early exit
    	// verify file opened correctly; otherwise display error, & quit
    	if (fin.fail())
    	{
    		cout << "ERROR opening data file." << endl;
    		return -1; 
    	} // end if statement
    
    	// read the contents of the file into an array of Stock objects
    	// objects are created dynamicly within the loop 
    	while(!fin.eof())
    	{
    		// input Stock record data from file 
    		getline(fin, tempExchange, ',');		// read thru , 		 
    		getline(fin, tempSymbol, ',');		// read thru , 
    		getline(fin, tempCompany, ',');		// read thru , 
    		getline(fin, tempPrice, ',');			// read thru newline		
    		getline(fin, tempShares);	// read thru ,
    		
    		// call function setStockInfo and convert string shares to int and string Price to double
    		stockObject[arraySize].setStockInfo(tempExchange, tempSymbol, tempCompany, 
    			atoi(tempShares.c_str()), strtod(tempPrice.c_str(),&endp));
    
    		++arraySize; // increment number of stock input
    	} // end while 
    
    	fin.close(); // close the file
    
    	// print header
    	cout << "\t\t\t\t\tLance McElmurry" << endl; // print  out my name tabed over 5
    	cout << "STOCKS READ FROM FILE" << endl << endl;
    	cout << left;
    	cout << setw(10) << "Exchange" << setw(8)<< "Symbol" << setw(29) << "Company"; 
    	cout << setw(9) << "Shares" << setw(7) << "Price" << setw(11) << "Stock Value" << endl;
    
    	// Print stock info
    	for(int i=0; i < arraySize; i++)
    	{
    		stockObject[i].displayStockInfo(); // call function to print stock info 
    	}
    	cout << endl << endl;
    	cout << "STOCKS SORTED BY VALUE" << endl << endl; // print to screen
    	cout << left;
    	cout << setw(10) << "Exchange" << setw(8)<< "Symbol" << setw(29) << "Company"; 
    	cout << setw(9) << "Shares" << setw(7) << "Price" << setw(11) << "Stock Value" << endl;		
    
    	// Sort objects and load into Stock class
    	for (int i = 0; i < 12; i++)
    	{
    		// Find the minimum in the object[i..12-1]
    		tempStockObject[0] = stockObject[i];
    		int currentMinIndex = i;
    
    		for (int j = i + 1; 12 > j; j++)
    		{
    			if ((stockObject[j] < tempStockObject[0])== false )
    			{
    				tempStockObject[0] = stockObject[j];
    				currentMinIndex = j;
    			}
    		}// end for
    
    		// Swap stockObject[i] with stockObject[currentMinIndex] if necessary;
    		if (currentMinIndex != i)
    		{
    			stockObject[currentMinIndex] = stockObject[i];
    			stockObject[i] = tempStockObject[0];
    		}// end if
    	}// end sort
    	for(int i=0; i < arraySize; i++)
    	{
    		stockObject[i].displayStockInfo(); // call function to print stock info 
    	}
    	cout << endl << endl;
    
    	
    		 
    	return 0; // normal exit 
    } // end main()
    
    
    
    Stock::Stock()
    {
    	// no arg Constructor 
    } 
    
    // Define Stock stock member function (define default Stock object)
    Stock::Stock(string exchange, string symbol, string company, int shares, double price)
    {
    	// set public variables equal to private varibles defined in Stock class
    	StockExchange = exchange;	
    	Symbol = symbol;	
    	Company = company;	
    	Shares = shares;
    	Price = price;
    	Value = shares * price;
    } // end stock class member function Stock
    
    // Define displayStockInfo stock member function
    void Stock::displayStockInfo()
    {
    	cout << left;
    	cout << setw(10) << StockExchange << setw(8) << Symbol << setw(29) << Company;
    	cout << right;
    	cout << fixed; // set decimal place to fixed
    	cout << setprecision (2); // number of places after the decimal to 2 places
    	cout << setw(6) << Shares << setw(8) << Price  << setw(13) << Value << endl;	
    } // end stock class member function displayStockInfo
    
    // Define setStockInfo stock member function
    void Stock::setStockInfo(string exchange, string symbol, string company, int shares, double price)
    {
    	// set public variables equal to private varibles defined in Stock class
    	StockExchange = exchange;
    	Symbol = symbol;
    	Company = company;	 
    	Shares = shares;
    	Price = price;
    	Value = shares * price;
    	
    } // end stock class member function setStockInfo
    
    // Define overloading function for comparing Stocks
    bool Stock::operator< (const Stock& second) const
    {
    	return (Value < second.Value);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Well I am not sure what I did different. I copied my file from my tempory working file into my final assignment file and ran the program and it ran correctly. I did not do anything at all different to my code so I guess maybe the problem was in the creation of my test file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to initialize static const array member of a class
    By nimitzhunter in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2010, 02:15 AM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 07:48 PM
  3. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 03:45 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21