2013-05-22 9 views
1

主類:C++任何人都可以幫助我通過重載>>運算符來將txt文件讀入類中嗎?

#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部首:

#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類:

#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 

我知道它有很多C噢,但我運行這個沒有錯誤,但它似乎卡住了我在主要類中的文件中讀取,並嘗試設置文件內容stockObject任何信息或幫助什麼即時通訊做錯了將是偉大的不確定是什麼在這一點上做。

+0

[請勿在頭文件中使用'namespace std'。](http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers) – syam

回答

0

您的線路cin >> stock;要求輸入標準輸入。要從文件中讀取,您需要在istream對象和stockObject對象上使用>>運算符,即inFile >> stock;。 (假設一切正常,我沒有檢查)。

+0

謝謝!它的工作大部分現在我只需要找出爲什麼數字是錯誤的大聲笑 –

相關問題