gpt4 book ai didi

C++ 任何人都可以通过重载 >> 运算符帮助我将 txt 文件读入类中吗?

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:35 25 4
gpt4 key购买 nike

主要类:

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

STOCKLIST.TXT 文件:

ABC 123.45 130.95 132.00 125.00 120.50 10000

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

最佳答案

您的代码行 cin >> stock; 请求来自标准输入的输入。要从文件中读取,您需要在 istream 对象和您的 stockObject 对象上使用 >> 运算符,即 inFile >> 库存;。 (假设其他一切正常,但我没有检查)。

关于C++ 任何人都可以通过重载 >> 运算符帮助我将 txt 文件读入类中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16684479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com