Question

MAIN CLASS:

#include <iostream>
#include <fstream>
#include <string>
#include "stockObject.h"

using namespace std;

int main()
{
    string line;
    stockObject stock("",0.0,0.0,0.0,0.0,0.0,0);

    ifstream inFile ("stockList.txt");
    if(inFile.is_open())
    {
        while (inFile.good() )
        {
            //getline(inFile, line);
            //cout << line ;
            cin >> stock;   
        }
        inFile.close();

    }
    else
    {
        cout << "Unable to open file" << endl;
    }
    cout << stock;

    system("Pause");
    return 0;
}

STOCKOBJECT HEADER:

#ifndef H_StockObject
#define H_StockObject
#include <string>
#include <iostream>
using namespace std;
class stockObject
{
    friend ostream& operator<< (ostream&, stockObject &);
    //allows the << operater to be used by the stockObject class

    friend istream& operator>> (istream&, stockObject &);
    //allows the >> operater to be used by the stockObject class

public:

    stockObject(string = "", double = 0.0, double = 0.0, double = 0.0, double = 0.0, double = 0.0, int = 0);
    // constructor for the object

    void setInfo(string, double, double, double, double, double, int);
    // function to set the info for the stock objects

    void printStock();
    // function to print the info that needs to be displayed by the stock object

    void showPrice() const;
    // displays the prices of stock objects

    double calculateGainLoss(double const, double const);
    //calculates the gain or loss of a stock object

    bool operator> (const stockObject&) const;
    bool operator< (const stockObject&) const;
    // allows for the comparasion of two stock objects
    double priceClose;
    double pricePrevious;

private:
    //declare all the variables for a stock object
    string stockSymbol;
    int numShares;
    double priceOpen;

    double priceHigh;
    double priceLow;

    double percentGainLoss;
};


#endif

STOCKOBJECT CLASS:

#include "stockObject.h"

istream& operator>> (istream& isObject, stockObject& stock)
{
    isObject >> stock.stockSymbol >> stock.priceOpen >> stock.priceClose >> stock.priceHigh >> stock.priceLow >> stock.pricePrevious >> stock.numShares;

    return isObject;
}
ostream& operator<<(ostream& osObject, stockObject& stock)
{
    osObject << "Stock Symbol: " << stock.stockSymbol << ", Open: " << stock.priceOpen << ", Close: " << stock.priceClose << ", High: " << stock.priceHigh << ", Low: " << stock.priceLow << ", Previous Close: " << stock.pricePrevious << ", Percent Gain: " << stock.calculateGainLoss(stock.priceClose, stock.pricePrevious) << ", Volume: " << stock.numShares;
    return osObject;
}
stockObject::stockObject(string stockSymbol, double open, double close, double high, double low, double prevClose, int gainLoss)
{
    setInfo(stockSymbol, open, close, high, low, prevClose, gainLoss);
}
void stockObject::setInfo(string stockSymbol, double open, double close, double high, double low, double prevClose, int gainLoss)
{
    this->stockSymbol=stockSymbol;
    priceOpen=open;
    priceClose=close;
    priceHigh=high;
    priceLow=low;
    pricePrevious=prevClose;
    percentGainLoss=gainLoss;
}
void stockObject::printStock()
{
    cout << "Stock Symbol: " << stockSymbol << ", Open: " << priceOpen << ", Close: " << priceClose << ", High: " << priceHigh << ", Low: " << priceLow << ", Previous Close: " << pricePrevious << ", Percent Gain: " << calculateGainLoss(priceClose, pricePrevious) << ", Volume: " << numShares;
}
double stockObject::calculateGainLoss(double close ,double prevClose)
{
    return ((close-prevClose)/prevClose);
}

STOCKLIST.TXT FILE:

ABC 123.45 130.95 132.00 125.00 120.50 10000

I know its a lot of code but i run this with no errors but it seems to be stuck where i read in the file in the main class and try to set the file contents to the stockObject any info or help on what im doing wrong would be great not really sure what to do at this point.

Was it helpful?

Solution

Your line cin >> stock; asks for input from the standard input. To read from the file you would need to use the >> operator on the istream object and your stockObject object, i.e. inFile >> stock;. (Assuming everything else works correctly, which I didn't check).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top