Hi my project have many csv files. Which is located in the same directory as Datafolder/IAG/day1.csv, Datafolder/IAG/day2.csv and Datafolder/IAG/day3.csv
. Same for Datafolder/CBA/day1.csv, Datafolder/CBA/day2.csv and Datafolder/CBA/day3.csv
and Datafolder/NAB/day1.csv, Datafolder/NAB/day2.csv and Datafolder/NAB/day3.csv
.
my goal is user can input which day's data they want and input. Once they input it will ask the user to enter either 1(to get the highest price value) or 2(lowest price value). But in my code I dont why once the user select the day and get the highest price it is stored as that value. Even though the user select another day and look for the highest price it is not actually displaying that day's highest value but the previous day's highest value which copied over again The following is codes. Thanks in advance guys.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "vector.h"
#include "stock.h"
#include <map>
using namespace std;
int counter = 0;
double highestPrice=0, lowestPrice=0;
ofstream outfile;
string sFileName;
string record;
Stock S1;
Vector<Stock> data;
int option;
ifstream codeindex;
ifstream IAGsales;
ifstream CBAsales;
ifstream NABsales;
ifstream* Tempsales = 0;
ifstream infile;
map<string,Vector<Stock>> mapIAG;
map<string,Vector<Stock>> mapCBA;
map<string,Vector<Stock>> mapNAB;
map<string,Vector<Stock>> mapTemp;
map<string,Vector<Stock>>::iterator it;
double price1, price2;
Time time1, time2;
Date date1;
string sXCode,sInputDate;
int main()
{
void initializing();
codeindex.open("Datafolder/code_index.txt");
if(! codeindex)
{
cout << codeindex <<" : File not found!!" << endl;
system("PAUSE");
exit(1);
}
do
{
initializing();
time1.initialize();
highestPrice = (data.getvalue(0)).getprice();//Initialise highest price
lowestPrice=0;
lowestPrice = (data.getvalue(0)).getprice();//Initialise lowest price
(data.getvalue(0)).gettime(time1, data.getvalue(0));//Initialise time1
switch (option)
{
case 1:
{
Date datehigh;
for(int i = 1 ; i < counter ; i ++)
{
if((data.getvalue(i)).getprice() > highestPrice)
{
highestPrice = (data.getvalue(i)).getprice();
(data.getvalue(i)).gettime(time1, data.getvalue(i));//get the time when highest price first occurred
(data.getvalue(i)).getdate(datehigh, data.getvalue(i));
}
}
cout << "Date: " << datehigh << endl;
cout << "Highest price : " << highestPrice << endl;
cout << "Start time(s): " << endl;
cout << time1 ;
for(int i = 0 ; i < counter ; i ++)
{
if( (data.getvalue(i)).getprice() == highestPrice)
{
(data.getvalue(i)).gettime(time2, data.getvalue(i));//get the time when highest price occurred
if(time2 != time1)//avoid duplicated time
{
(data.getvalue(i)).printtime();
(data.getvalue(i)).gettime(time1, data.getvalue(i));
//(data.getvalue(i)).getdate(datehigh, data.getvalue(i));
}
}//End of if( (data.get(i)).getprice() == highestPrice)
}//End of for(int i = 0 ; i < count ; i ++)
//main();
//initialize();
//(data.getvalue(0)).printdate();
break;
}
case 2:
{
Date datelow;
for(int i = 1 ; i < counter ; i ++)
{
if(((data.getvalue(i)).getprice() < lowestPrice) && ((data.getvalue(i)).getprice() > 0))
{
lowestPrice = (data.getvalue(i)).getprice();
(data.getvalue(i)).gettime(time1, data.getvalue(i));//get the time when lowest price first occurred
(data.getvalue(i)).getdate(datelow, data.getvalue(i));
}
}
int j =0;
//(data.getvalue(j)).printdate();
cout << "Date: " << datelow << endl;
cout << "Lowest price: " << lowestPrice << endl;
cout << "Start time(s): " << endl;
cout << time1<<endl;
for(int i = 0 ; i < counter ; i ++)
{
if( (data.getvalue(i)).getprice() == lowestPrice)
{
(data.getvalue(i)).gettime(time2, data.getvalue(i));//get the time when highest price occurred
if(time2 != time1)//avoid duplicated time
{
(data.getvalue(i)).printtime();
(data.getvalue(i)).gettime(time1, data.getvalue(i));
}
}//End of if( (data.get(i)).getprice() == lowestPrice)
}//End of for(int i = 0 ; i < count ; i ++)
//main();
//initialize();
break;
}
case 3:
{
outfile.open("output.csv");
if(! outfile)
{
cout << "Can not open outfile" << endl;
exit(1);
}
cout << "Data processing" << endl;
outfile << fixed << showpoint << setprecision(2);
outfile << "Date,Start time,Price of share,Volume of shares traded,Total value of shares traded" << endl;
price1 = data.getvalue(counter-1).getprice();//get price for the last row
double nVolumeOfTrades = data.getvalue(counter-1).getvolume();
double ntotalValueOfShares = data.getvalue(counter-1).getvalue();
// Get Date of trading
data.getvalue(counter-1).getdate(date1,data.getvalue(counter-1));
// Get Start time
data.getvalue(counter-1).gettime(time1,data.getvalue(counter-1));
for(int i = counter-2; i > -1 ; i --)
{
price2 = data.getvalue(i).getprice();
if(price2 != 0 && price1 != price2)
{
// Write values to file.
outfile << date1 << "," << time1 << "," << price1 << "," << nVolumeOfTrades << "," << ntotalValueOfShares << endl;
// Get Date of trading
data.getvalue(i).getdate(date1,data.getvalue(i));
// Get Start time
data.getvalue(i).gettime(time1,data.getvalue(i));
price1 = data.getvalue(i).getprice();
nVolumeOfTrades = 0;
ntotalValueOfShares = 0;
}
nVolumeOfTrades += data.getvalue(i).getvolume();
ntotalValueOfShares += data.getvalue(i).getvalue();
}
cout << "Data processed" << endl;
outfile.close();
//initialize();
//main();
break;
}
case 4:
{
break;
}
default:
{
cout << "Invalid option" << endl;
cout<<"Choose from options 1 to 4: "<<endl;
cout << "Enter 1 to get the highest stock price & time:" << endl;
cout << "Enter 2 to get the lowest stock price & time:" << endl;
cout << "Enter 3 to output the csv file: " << endl;
cout << "Enter 4 to exit from the program:" << endl;
cin >> option;
break;
}
}
if(option==4)
{
break;
}
cout << "\n\n" << endl;
}
while(1);
return 0;
}
void initializing()
{
// Present the user with Input options
cout<<"Enter the code of transaction:" <<endl;
while(! codeindex.eof()){
getline(codeindex,record);
cout<<record<<endl;
}
cin>>sXCode;
codeindex.clear();
codeindex.seekg(0,ios::beg);
while(sXCode!="IAG" && sXCode!="CBA" && sXCode!="NAB"){
cout<<"Wrong input .. Please enter again \n";
cin>>sXCode;
}
// Get code file name.
if(sXCode=="IAG")
{
if(IAGsales.is_open()==true){
IAGsales.close();
}
IAGsales.open("Datafolder/IAG/sales_index.txt");
if(!IAGsales)
{
cout << IAGsales<<" :File not found!!" << endl;
system("PAUSE");
exit(1);
}
// Read IAG file names.
while(!IAGsales.eof())
{
getline(IAGsales, sFileName);
if(sFileName.size() > 0)
{ //cout<<"before: "<< sFilePath <<endl;
string sFilePath = "Datafolder/IAG/";
sFilePath = sFilePath.append(sFileName);
cout<<"after append: "<< sFilePath <<endl;
// Read the csv file
infile.open(sFilePath);
if(! infile)
{
cout << sFileName<<": File not found!!" << endl;
exit(1);
}
getline(infile, record);//first line
getline(infile, record);//second line
while(! infile.eof())
{
getline(infile, record);
if(record.size() > 0)
{
S1.readfile(record, S1);
data.insert(S1);
counter ++;
}
}//while(! infile.eof())
infile.close();
// Insert to Map
mapIAG.insert(pair<string,Vector<Stock>>(sFileName,data));
}
}
IAGsales.clear();
IAGsales.seekg(0,ios::beg);
Tempsales = &IAGsales;
//cout<<"test1";
//IAGsales.close();
}
else if(sXCode=="CBA")
{
if(CBAsales.is_open()==true){
CBAsales.close();
}
CBAsales.open("Datafolder/CBA/sales_index.txt");
if(!CBAsales)
{
cout << CBAsales<<" : File not found!!" << endl;
system("PAUSE");
exit(1);
}
// Read IAG file names.
while(!CBAsales.eof())
{
getline(CBAsales, sFileName);
if(sFileName.size() > 0)
{
string sFilePath = "Datafolder/CBA/";
sFilePath = sFilePath.append(sFileName);
// Read the csv file
infile.open(sFilePath);
if(! infile)
{
cout << sFileName<<": File not foud!!!" << endl;
exit(1);
}
getline(infile, record);//first line
getline(infile, record);//second line
while(! infile.eof())
{
getline(infile, record);
if(record.size() > 0)
{
S1.readfile(record, S1);
data.insert(S1);
counter ++;
}
}//while(! infile.eof())
infile.close();
// Insert to Map
mapIAG.insert(pair<string,Vector<Stock>>(sFileName,data));
}
}
CBAsales.clear();
CBAsales.seekg(0,ios::beg);
Tempsales = &CBAsales;
//cout<<"test2";
}
else
{
if(NABsales.is_open()==true){
NABsales.close();
}
NABsales.open("Datafolder/NAB/sales_index.txt");
if(!NABsales)
{
cout << NABsales<<" : File not found!!!!" << endl;
system("PAUSE");
exit(1);
}
// Read IAG file names.
while(!NABsales.eof())
{
getline(NABsales, sFileName);
if(sFileName.size() > 0)
{
string sFilePath = "Datafolder/NAB/";
sFilePath = sFilePath.append(sFileName);
// Read the csv file
infile.open(sFilePath);
if(! infile)
{
cout << sFileName<<": File not foud!!" << endl;
exit(1);
}
getline(infile, record);//first line
getline(infile, record);//second line
while(! infile.eof())
{
getline(infile, record);
if(record.size() > 0)
{
S1.readfile(record, S1);
data.insert(S1);
counter ++;
}
}//while(! infile.eof())
infile.close();
// Insert to Map
mapIAG.insert(pair<string,Vector<Stock>>(sFileName,data));
}
}
NABsales.clear();
NABsales.seekg(0,ios::beg);
Tempsales = &NABsales;
//cout<<"test3";
}
highestPrice = (data.getvalue(0)).getprice();//Initialise highest price
lowestPrice = (data.getvalue(0)).getprice();//Initialise lowest price
(data.getvalue(0)).gettime(time1, data.getvalue(0));//Initialise time1
cout<<"Enter the date of transaction:" <<endl;
while(!Tempsales->eof()){
getline(*Tempsales,record);
cout<<record<<endl;
}
cin>>sInputDate;
cout<<"Choose from options 1 to 4: "<<endl;
cout << "Enter 1 to get the highest stock price & time:" << endl;
cout << "Enter 2 to get the lowest stock price & time:" << endl;
cout << "Enter 3 to output the csv file: " << endl;
cout << "Enter 4 to exit from the program:" << endl;
cin >> option;
// Get the vector from Map based on Date.
if(sXCode=="IAG")
{
it = mapIAG.find(sInputDate);
data = it->second;
}
else if(sXCode=="CBA")
{
it = mapIAG.find(sInputDate);
data = it->second;
}
else
{
it = mapIAG.find(sInputDate);
data = it->second;
}
}
Stock is a Vector.