1

主要课程:

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

股票标题:

#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

股票类:

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

库存清单.TXT 文件:

ABC 123.45 130.95 132.00 125.00 120.50 10000

我知道它有很多代码,但我运行它没有错误,但它似乎卡在我在主类的文件中读取并尝试将文件内容设置为 stockObject 任何关于我做错了什么的信息或帮助太好了,现在还不确定该怎么做。

4

1 回答 1

1

您的线路cin >> stock;要求来自标准输入的输入。要从文件中读取,您需要在对象和您的对象上使用>>运算符,即. (假设其他一切正常,我没有检查)。istreamstockObjectinFile >> stock;

于 2013-05-22T06:07:18.160 回答