// 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; }
Can't test it since you haven't provided the input file. My best advice for now is that you need to make sure you're reading in the data in the first place. It looks like the first thing read in on line 114 is the first date, which you say is your problem. In additon to displaying it to the output file on line 116, add a line so that it displays token to the screen too. That way you see a nice list of all the tokens. See if they are correct. If not, that tells you where to go next. it's not reading the tokens correctly. If the tokens ARE being read correctly, the next step is to make sure that the tokens get into your array correctly before leaving Parse_Output_To_File. More cout statements (take them out later). You need to see exactly what's going on where to help nail down the problem. Some organized cout statements that are deleted later, along with pauses if necessary, will do wonders.
// 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 << " "; cout << "i = " << i << ", j = " << j << ", token = " << token << endl; // added by VD cin.get (); // added by VD 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; } cout << "j = " << j << ". None of the if statements executed." << endl; // added by VD. cin.get (); // added by VD. } i++ // add semicolon - VD outfile << endl; } redo.close() // add semicolon - VD 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; }
ofstream outfile; outfile.open ("c:\\Outputfile.txt"); if (!outfile.is_open()) { cout << "Error in opening file for writing!"; } else return;
ofstream outfile; outfile.open ("c:\\Outputfile.txt"); if (!outfile.is_open()) { cout << "Error in opening file for writing!"; return; }
| DaniWeb Message | |
| Cancel Changes | |