我创建模板向量来读取我的infile txt(courseofsale.txt)并通过csv文件(output.csv)输出。一切都是正确的,但最高价格和最低价格的时机是错误的 The highest price timing must be like this
while the lowest price timing must be like this
但是我得到16:57作为两者的时间。 这是我的主要cpp
// MainTest.cpp
//
// Class that runs the main program modules.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "StockT.h"
#include "VectorT.h"
void highestPrice();
void lowestPrice();
void outputFile();
VectorT<StockT> V1(4000);
StockT S1;
DateT d1;
TimeT t1;
using namespace std;
//----------------------------------------------------------------------------
//Main Method
int main()
{
ifstream inData;
inData.open("courseofsales.txt");
string line;
string sDay, sMonth, sYear, sHour, sMinute,sSecond,sCondition;
double price,value;
int volume;
//Reading the text file
if (inData.is_open())
{
inData.ignore(500, '\n');
inData.ignore(500, '\n');
while (getline(inData, line, '\n'))
{
while (!inData.eof())
{
sDay = line.substr(0, 2);
sMonth = line.substr(3, 2);
sYear = line.substr(6, 4);
sHour = line.substr(11, 2);
sMinute = line.substr(14, 2);
sSecond = line.substr(16, 2);
inData.ignore(50, '\t');
inData >> price;
inData.ignore(50, '\t');
inData >> volume;
inData.ignore(50, '\t');
inData >> value;
inData.ignore(50, '\t');
getline(inData, sCondition);
S1.setAll(price, volume, value, sDay, sMonth, sYear, sHour, sMinute, sSecond);
V1.push_back(S1);
}
}
inData.close(); //closing text file
}
else
cout << "Unable to open file";
int choice;
do {
cout << "Please enter your choice: " << endl << endl;
cout << "1. Retrieve highest share price of the day" << endl;
cout << "2. Retrieve lowest share price of the day" << endl;
cout << "3. Generate output file" << endl;
cout << "4. Exit the program" << endl;
cout << "Your choice: ";
cin >> choice;
cout << endl;
if (choice == 1)
{
cout << endl << "This is the highest bid of the day: " << endl;
highestPrice(); //calls the method for retrieving highest price
cout << endl;
}
else if (choice == 2)
{
cout << endl << "This is the lowest bid of the day: " << endl;
lowestPrice(); //calls the method for retrieving lowest price
cout << endl;
}
else if (choice == 3)
{
cout << endl << "Check your file." << endl;
outputFile(); //calls the method to create output.csv
}
else if (choice == 4)
exit(0); //exit the program
else
{
cout << endl << "This is an invalid choice. Enter again. " << endl;
}
}while(choice != 1 || choice != 2 || choice != 3 || choice != 4); //Looping for user input till the program exits
system("PAUSE");
return 0;
}
//----------------------------------------------------------------------------
//Retrieve the highest share price.
void highestPrice()
{
double HP = V1.at(0).getPrice();
for (int i = 0; i < V1.size(); i++)
{
if (V1.at(i).getPrice() > HP)
{
HP = V1.at(i).getPrice();
}
}
for (int j = 0; j < V1.size(); j++)
{
if (V1.at(j).getPrice() == HP)
{
cout << "Date and Time of transaction: " << V1.at(j).getDate() << V1.at(j).getTime()<< endl;
cout << "Highest price: " << V1.at(j).getPrice() << endl;
break;
}
}
cout << endl;
}
//----------------------------------------------------------------------------
//Retrieve the lowest share price.
void lowestPrice()
{
double LP = V1.at(0).getPrice();
for (int i = 0; i < V1.size(); i++)
{
if ((V1.at(i).getPrice() < LP) && (V1.at(i).getPrice() != 0))
{
LP = V1.at(i).getPrice();
}
}
for (int j = 0; j < V1.size(); j++)
{
if (V1.at(j).getPrice() == LP)
{
cout << "Date and Time of transaction: " << V1.at(j).getDate() << V1.at(j).getTime()<< endl;
cout << "Lowest price: " << V1.at(j).getPrice() << endl;
break;
}
}
cout << endl;
}
//----------------------------------------------------------------------------
//Creates the output file
void outputFile()
{
ofstream outData;
outData.open("output.csv");
string line;
int c1 = 0;
if (outData.is_open())
{
for (int i = 0; i < V1.size(); i++)
{
outData << V1.at(i).getDate() << "," << setw(5);
outData << V1.at(i).getTime() << "," << setw(5);
outData << V1.at(i).getPrice() << "," << setw(5);
outData << V1.at(i).getVolume() << "," << setw(5);
outData << V1.at(i).getValue();
outData << '\n';
}
outData.close();
}
else
cout << "Unable to write file";
}
答案 0 :(得分:0)
考虑一下,如果16:57是最后一项的时间,StockT
中的复制构造函数是坏的,只创建浅拷贝,而你以需要深拷贝的方式使用它(在V1.push_back(S1);
)。
这是我在这里的最后一条评论,因为您没有提供MCVE,也看起来您不知道如何调试代码或了解它。只需重做一些基础编程课程并阅读有关数据结构和内存的内容,以便更好地了解内部发生的事情,并学习如何使用调试器进行检查。