Hi,
The following code I've been working on (as hobbyist) compiles okay, buy at runtime it fails to output the dates that it is supposed to extract from a csv file. I've been staring at this forever and no progress.
I am thinking that the problem occurs before the date verification function, because when the program gets to cout << "This is the first date in the database" and cout << dataArray[0].date << endl, it does not output the date. This means that the first element in the array is empty, which means that the array did not populate for some reason.
Would appreciate any suggestions to identify the problem.
Thanks
TR
// Dataprep.cpp : Defines the entry point for the console application
#include "stdafx.h"
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
#include <cstring>
using namespace std;
int NumLines;
void Error (int);
int Open_Price_Data_File (char* filename);
struct Bar* Define_Array (const char*);
void Parse_Output_To_File (struct Bar* dataArray, const char*);
void Date_Verification ();
void ExitScreen ();
//Structure Declaration and Array Initialization
struct Bar
{
string date; // date
double open; // opening price
double high; // high price
double low; // low price
double close; // closing price
};
struct Bar *bararray;
//Error catching function
void Error(int errorcode)
{
if(errorcode == 1) {
cout << "Error in opening file...";
}
else cout << "OK";
}
//Function to open price data file
int Open_Price_Data_File (char* filename)
{
cout << "Enter filename in the following format c: backslash filename.csv: ";
cin >> filename;
ifstream infile;
cout << "Reading from the price data file" << endl;
infile.open(filename);
if(!infile.is_open()){
Error (1);
}
else return 0;
infile.close();
return 1;
}
// Function to count number of lines in file & define the array of structures accordingly
struct Bar* Define_Array (const char* filename)
{
ifstream infile;
infile.open(filename);
if(!infile.is_open())
{
Error (1);
}
else
{
string line;
while(getline(infile, line))
NumLines++;
cout << "This is the total number of lines in the source file: ";
cout << NumLines << endl;
Bar *bararray = NULL;
bararray = new Bar[NumLines];
infile.close();
return bararray;
}
}
// String Parsing to Skip Commas
void Parse_Output_To_File (struct Bar *dataArray, char* filename)
{
ofstream outfile;
outfile.open ("c:\\Outputfile.txt");
if (!outfile.is_open())
{
cout << "Error in opening file for writing!";
}
else return;
ifstream redo;
redo.open(filename);
int i = 0;
while (!redo.fail())
{
int j = 0;
string data, token;
getline(redo, data);
istringstream isstream (data);
while (getline(isstream,token,','))
{
outfile << token << " ";
if(j == 0) {
dataArray[i].date = token;
j++;
continue;
}
if(j == 1) {
dataArray[i].open = atof( token.c_str());
j++;
continue;
}
if(j == 2) {
dataArray[i].high = atof( token.c_str());
j++;
continue;
}
if(j == 3) {
dataArray[i].low = atof( token.c_str());
j++;
continue;
}
if(j == 4) {
dataArray[i].close = atof(token.c_str ());
j++;
continue;
}
}
i++ outfile << endl;
}
redo.close() outfile.close();
}
// Date Verification Function
void Date_Verification (struct Bar *dataArray)
{
cout << "This is the first date in the database: ";
cout << dataArray[0].date << endl;
cout << "This is the last date in the database: ";
cout << dataArray[NumLines - 1].date << endl;
cout << "Enter the start date for the analysis in the mm/dd/yyyy format: ";
string startdate;
cin >> startdate;
cout << "The startdate you entered is: " << startdate << endl;
int i;
for (i = 0; i <= NumLines -1;)
{
if (!strcmp(startdate.c_str(), dataArray[i].date.c_str()) == 0)
{
i++; if (i == NumLines -1)
cout << "Incorrect start date " << endl;
}
else
{
cout << "Start date exists in the database" << endl;
break;
}
}
cout << "Enter the end date for the analysis in the mm/dd/yyyy format: ";
string enddate;
cin >> enddate;
cout << "The enddate you entered is: " << enddate << endl;
for (i = 0; i <= NumLines -1;)
{
if (!strcmp(enddate.c_str(), dataArray[i].date.c_str()) == 0)
{
i++;
if (i == NumLines -1)
out << "Incorrect end date" << endl;
}
else
{
cout << "End date exists in the database" << endl;
break;
}
}
}
// User Prompt to Retain Command Window
void ExitScreen ()
{
char ch;
cout << "Please press Q or q to quit: ";
cin >> ch;
if (ch=='Q' && ch=='q')
{
cout << "Exiting...";
}
else
cout << "Please press Q or q to quit: ";
}
//Main Program
int main ()
{
char filename [80];
Open_Price_Data_File (filename);
struct Bar* Array;
Array = Define_Array (filename);
Parse_Output_To_File (Array, filename);
Date_Verification (Array);
ExitScreen ();
return 0;
}