Array of structures not returning elements

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 3
Reputation: toneranger is an unknown quantity at this point 
Solved Threads: 0
toneranger toneranger is offline Offline
Newbie Poster

Array of structures not returning elements

 
0
  #1
Nov 7th, 2009
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


  1. // Dataprep.cpp : Defines the entry point for the console application
  2.  
  3. #include "stdafx.h"
  4. #include <vector>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <string>
  8. #include <sstream>
  9. #include <math.h>
  10. #include <cstring>
  11. using namespace std;
  12. int NumLines;
  13. void Error (int);
  14. int Open_Price_Data_File (char* filename);
  15. struct Bar* Define_Array (const char*);
  16. void Parse_Output_To_File (struct Bar* dataArray, const char*);
  17. void Date_Verification ();
  18. void ExitScreen ();
  19.  
  20.  
  21.  
  22. //Structure Declaration and Array Initialization
  23. struct Bar
  24. {
  25. string date; // date
  26. double open; // opening price
  27. double high; // high price
  28. double low; // low price
  29. double close; // closing price
  30.  
  31. };
  32.  
  33. struct Bar *bararray;
  34.  
  35. //Error catching function
  36. void Error(int errorcode)
  37. {
  38. if(errorcode == 1) {
  39. cout << "Error in opening file...";
  40. }
  41. else cout << "OK";
  42. }
  43.  
  44.  
  45. //Function to open price data file
  46. int Open_Price_Data_File (char* filename)
  47. {
  48. cout << "Enter filename in the following format c: backslash filename.csv: ";
  49. cin >> filename;
  50. ifstream infile;
  51. cout << "Reading from the price data file" << endl;
  52. infile.open(filename);
  53. if(!infile.is_open()){
  54. Error (1);
  55. }
  56. else return 0;
  57. infile.close();
  58. return 1;
  59. }
  60.  
  61.  
  62.  
  63. // Function to count number of lines in file & define the array of structures accordingly
  64. struct Bar* Define_Array (const char* filename)
  65. {
  66.  
  67. ifstream infile;
  68. infile.open(filename);
  69. if(!infile.is_open())
  70. {
  71. Error (1);
  72. }
  73. else
  74. {
  75. string line;
  76. while(getline(infile, line))
  77. NumLines++;
  78. cout << "This is the total number of lines in the source file: ";
  79. cout << NumLines << endl;
  80. Bar *bararray = NULL;
  81. bararray = new Bar[NumLines];
  82. infile.close();
  83. return bararray;
  84. }
  85.  
  86.  
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93. // String Parsing to Skip Commas
  94. void Parse_Output_To_File (struct Bar *dataArray, char* filename)
  95. {
  96. ofstream outfile;
  97. outfile.open ("c:\\Outputfile.txt");
  98. if (!outfile.is_open())
  99. {
  100. cout << "Error in opening file for writing!";
  101. }
  102. else return;
  103.  
  104. ifstream redo;
  105. redo.open(filename);
  106. int i = 0;
  107.  
  108. while (!redo.fail())
  109. {
  110. int j = 0;
  111. string data, token;
  112. getline(redo, data);
  113. istringstream isstream (data);
  114. while (getline(isstream,token,','))
  115. {
  116. outfile << token << " ";
  117. if(j == 0) {
  118. dataArray[i].date = token;
  119. j++;
  120. continue;
  121. }
  122.  
  123. if(j == 1) {
  124. dataArray[i].open = atof( token.c_str());
  125. j++;
  126. continue;
  127. }
  128.  
  129. if(j == 2) {
  130. dataArray[i].high = atof( token.c_str());
  131. j++;
  132. continue;
  133. }
  134.  
  135. if(j == 3) {
  136. dataArray[i].low = atof( token.c_str());
  137. j++;
  138. continue;
  139. }
  140.  
  141. if(j == 4) {
  142. dataArray[i].close = atof(token.c_str ());
  143. j++;
  144. continue;
  145. }
  146.  
  147.  
  148. }
  149.  
  150.  
  151. i++ outfile << endl;
  152. }
  153. redo.close() outfile.close();
  154.  
  155. }
  156.  
  157. // Date Verification Function
  158.  
  159. void Date_Verification (struct Bar *dataArray)
  160. {
  161. cout << "This is the first date in the database: ";
  162. cout << dataArray[0].date << endl;
  163. cout << "This is the last date in the database: ";
  164. cout << dataArray[NumLines - 1].date << endl;
  165. cout << "Enter the start date for the analysis in the mm/dd/yyyy format: ";
  166. string startdate;
  167. cin >> startdate;
  168. cout << "The startdate you entered is: " << startdate << endl;
  169.  
  170. int i;
  171. for (i = 0; i <= NumLines -1;)
  172. {
  173. if (!strcmp(startdate.c_str(), dataArray[i].date.c_str()) == 0)
  174. {
  175. i++; if (i == NumLines -1)
  176. cout << "Incorrect start date " << endl;
  177. }
  178. else
  179. {
  180. cout << "Start date exists in the database" << endl;
  181. break;
  182. }
  183. }
  184.  
  185. cout << "Enter the end date for the analysis in the mm/dd/yyyy format: ";
  186. string enddate;
  187. cin >> enddate;
  188. cout << "The enddate you entered is: " << enddate << endl;
  189. for (i = 0; i <= NumLines -1;)
  190. {
  191. if (!strcmp(enddate.c_str(), dataArray[i].date.c_str()) == 0)
  192. {
  193. i++;
  194. if (i == NumLines -1)
  195. out << "Incorrect end date" << endl;
  196. }
  197. else
  198. {
  199. cout << "End date exists in the database" << endl;
  200. break;
  201. }
  202.  
  203. }
  204.  
  205. }
  206.  
  207.  
  208.  
  209.  
  210. // User Prompt to Retain Command Window
  211. void ExitScreen ()
  212. {
  213. char ch;
  214. cout << "Please press Q or q to quit: ";
  215. cin >> ch;
  216. if (ch=='Q' && ch=='q')
  217. {
  218. cout << "Exiting...";
  219. }
  220. else
  221. cout << "Please press Q or q to quit: ";
  222. }
  223.  
  224.  
  225. //Main Program
  226. int main ()
  227. {
  228.  
  229.  
  230. char filename [80];
  231. Open_Price_Data_File (filename);
  232. struct Bar* Array;
  233. Array = Define_Array (filename);
  234. Parse_Output_To_File (Array, filename);
  235. Date_Verification (Array);
  236. ExitScreen ();
  237. return 0;
  238. }
Last edited by toneranger; Nov 7th, 2009 at 1:49 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #2
Nov 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: toneranger is an unknown quantity at this point 
Solved Threads: 0
toneranger toneranger is offline Offline
Newbie Poster
 
0
  #3
20 Days Ago
Originally Posted by VernonDozier View Post
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.

Hi Vernon,

Thanks for your help. I tried inserting these lines:

cout << token;
break;

right after the { outfile << token << " "; //line 117

but nothing happens when I run the program. It just goes right through to the next function, which is the date verification function... I tried inserting a cout at the beginning of the Parse_Output_To_File function and it doesn't even print that... weird. Not sure what to do next, since I'm a real newbie at troubleshooting.

Thanks much
TR.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
1
  #4
18 Days Ago
I formatted the code to make it easier to read and added a few lines - see "Added by VD" in comments. cin.get() pauses so you have time to read. Press the "Enter" key to move on. Delete later. Add the semicolons I mention.

Make sure this displays what it is supposed to display. If it does not, everything you do afterwards relies on bad data.

  1. // Dataprep.cpp : Defines the entry point for the console application
  2.  
  3. #include "stdafx.h"
  4. #include <vector>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <string>
  8. #include <sstream>
  9. #include <math.h>
  10. #include <cstring>
  11. using namespace std;
  12. int NumLines;
  13. void Error (int);
  14. int Open_Price_Data_File (char* filename);
  15. struct Bar* Define_Array (const char*);
  16. void Parse_Output_To_File (struct Bar* dataArray, const char*);
  17. void Date_Verification ();
  18. void ExitScreen ();
  19.  
  20.  
  21.  
  22. //Structure Declaration and Array Initialization
  23. struct Bar
  24. {
  25. string date; // date
  26. double open; // opening price
  27. double high; // high price
  28. double low; // low price
  29. double close; // closing price
  30.  
  31. };
  32.  
  33. struct Bar *bararray;
  34.  
  35. //Error catching function
  36. void Error(int errorcode)
  37. {
  38. if(errorcode == 1) {
  39. cout << "Error in opening file...";
  40. }
  41. else cout << "OK";
  42. }
  43.  
  44.  
  45. //Function to open price data file
  46. int Open_Price_Data_File (char* filename)
  47. {
  48. cout << "Enter filename in the following format c: backslash filename.csv: ";
  49. cin >> filename;
  50. ifstream infile;
  51. cout << "Reading from the price data file" << endl;
  52. infile.open(filename);
  53. if(!infile.is_open()){
  54. Error (1);
  55. }
  56. else return 0;
  57. infile.close();
  58. return 1;
  59. }
  60.  
  61.  
  62.  
  63. // Function to count number of lines in file & define the array of structures accordingly
  64. struct Bar* Define_Array (const char* filename)
  65. {
  66. ifstream infile;
  67. infile.open(filename);
  68. if(!infile.is_open())
  69. {
  70. Error (1);
  71. }
  72. else
  73. {
  74. string line;
  75. while(getline(infile, line))
  76. NumLines++;
  77. cout << "This is the total number of lines in the source file: ";
  78. cout << NumLines << endl;
  79. Bar *bararray = NULL;
  80. bararray = new Bar[NumLines];
  81. infile.close();
  82. return bararray;
  83. }
  84. }
  85.  
  86.  
  87.  
  88.  
  89. // String Parsing to Skip Commas
  90. void Parse_Output_To_File (struct Bar *dataArray, char* filename)
  91. {
  92. ofstream outfile;
  93. outfile.open ("c:\\Outputfile.txt");
  94. if (!outfile.is_open())
  95. {
  96. cout << "Error in opening file for writing!";
  97. }
  98. else return;
  99.  
  100. ifstream redo;
  101. redo.open(filename);
  102. int i = 0;
  103.  
  104. while (!redo.fail())
  105. {
  106. int j = 0;
  107. string data, token;
  108. getline(redo, data);
  109. istringstream isstream (data);
  110. while (getline(isstream,token,','))
  111. {
  112. outfile << token << " ";
  113. cout << "i = " << i << ", j = " << j << ", token = " << token << endl; // added by VD
  114. cin.get (); // added by VD
  115. if(j == 0)
  116. {
  117. dataArray[i].date = token;
  118. j++;
  119. continue;
  120. }
  121.  
  122. if(j == 1)
  123. {
  124. dataArray[i].open = atof( token.c_str());
  125. j++;
  126. continue;
  127. }
  128.  
  129. if(j == 2) {
  130. dataArray[i].high = atof( token.c_str());
  131. j++;
  132. continue;
  133. }
  134.  
  135. if(j == 3) {
  136. dataArray[i].low = atof( token.c_str());
  137. j++;
  138. continue;
  139. }
  140.  
  141. if(j == 4) {
  142. dataArray[i].close = atof(token.c_str ());
  143. j++;
  144. continue;
  145. }
  146.  
  147. cout << "j = " << j << ". None of the if statements executed." << endl; // added by VD.
  148. cin.get (); // added by VD.
  149. }
  150.  
  151.  
  152. i++ // add semicolon - VD
  153. outfile << endl;
  154. }
  155. redo.close() // add semicolon - VD
  156. outfile.close();
  157. }
  158.  
  159. // Date Verification Function
  160.  
  161. void Date_Verification (struct Bar *dataArray)
  162. {
  163. cout << "This is the first date in the database: ";
  164. cout << dataArray[0].date << endl;
  165. cout << "This is the last date in the database: ";
  166. cout << dataArray[NumLines - 1].date << endl;
  167. cout << "Enter the start date for the analysis in the mm/dd/yyyy format: ";
  168. string startdate;
  169. cin >> startdate;
  170. cout << "The startdate you entered is: " << startdate << endl;
  171.  
  172. int i;
  173. for (i = 0; i <= NumLines -1;)
  174. {
  175. if (!strcmp(startdate.c_str(), dataArray[i].date.c_str()) == 0)
  176. {
  177. i++;
  178. if (i == NumLines -1)
  179. cout << "Incorrect start date " << endl;
  180. }
  181. else
  182. {
  183. cout << "Start date exists in the database" << endl;
  184. break;
  185. }
  186. }
  187.  
  188. cout << "Enter the end date for the analysis in the mm/dd/yyyy format: ";
  189. string enddate;
  190. cin >> enddate;
  191. cout << "The enddate you entered is: " << enddate << endl;
  192. for (i = 0; i <= NumLines -1;)
  193. {
  194. if (!strcmp(enddate.c_str(), dataArray[i].date.c_str()) == 0)
  195. {
  196. i++;
  197. if (i == NumLines -1)
  198. out << "Incorrect end date" << endl;
  199. }
  200. else
  201. {
  202. cout << "End date exists in the database" << endl;
  203. break;
  204. }
  205. }
  206. }
  207.  
  208.  
  209.  
  210.  
  211. // User Prompt to Retain Command Window
  212. void ExitScreen ()
  213. {
  214. char ch;
  215. cout << "Please press Q or q to quit: ";
  216. cin >> ch;
  217. if (ch=='Q' && ch=='q')
  218. {
  219. cout << "Exiting...";
  220. }
  221. else
  222. cout << "Please press Q or q to quit: ";
  223. }
  224.  
  225.  
  226. //Main Program
  227. int main ()
  228. {
  229. char filename [80];
  230. Open_Price_Data_File (filename);
  231. struct Bar* Array;
  232. Array = Define_Array (filename);
  233. Parse_Output_To_File (Array, filename);
  234. Date_Verification (Array);
  235. ExitScreen ();
  236. return 0;
  237. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: toneranger is an unknown quantity at this point 
Solved Threads: 0
toneranger toneranger is offline Offline
Newbie Poster
 
0
  #5
17 Days Ago
Hi Vernon,

I was careful to add the code you suggested. I also made sure the semi colons were there (they were in the original code). Very strange, but I get exactly the same result, i.e., prints:

Reading from the price data file
This is the total number of lines in the source file: 18066
This is the first date in the database:
This is the last date in the database:
Enter the start date for the analysis in the mm/dd/yyyy format:

My inferences so far:
#1 If it's still not printing the first date then it means that the array of structures did not populate.

#2 If it didn't print the j cout you suggested it means that it's not even reading from the file? i.e., the getline (redo,data) isn't doing its job?

#3 I know that the output file is opened (see it on my C drive) but nothing is sent to it because the file is empty.

#4 I also know that the problem is not the source file because I opened it up and there whole 18066 lines are there.

So if the data source is not the problem it has to be the code?

Thanks much
TR
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
1
  #6
17 Days Ago
If you don't get any of the lines that I added as cout statements, that means that they aren't being executed even once, so it's breaking somewhere, and very early.

Whether the code is the problem or the input file is the problem, I can't say since I haven't seen the input file. Did you upload it? If so, I don't see it. I don't need an 18,000 line input file. A much shorter one will suffice, but give at least a few lines. That would be the fastest way. That way we can run it, see if the file matches the program, etc.

Actually, regardless of the file, this code (starting at line 92) seems seriously off and is very likely the culprit. In fact, since your output doesn't show any error messages, it certainly IS the culprit.

  1. ofstream outfile;
  2. outfile.open ("c:\\Outputfile.txt");
  3. if (!outfile.is_open())
  4. {
  5. cout << "Error in opening file for writing!";
  6. }
  7. else return;

You are returning when the file opens SUCCESSFULLY (i.e. when you DO NOT get an error. Assuming everything works the way it is supposed to, you're returning at line 98, before you even open the input file. I can't imagine that you intend to do that. I imagine that you instead want this:

  1. ofstream outfile;
  2. outfile.open ("c:\\Outputfile.txt");
  3. if (!outfile.is_open())
  4. {
  5. cout << "Error in opening file for writing!";
  6. return;
  7. }

Get rid of the "else" statement altogether. Relocate the return statement. You want to short-circuit the function if you get an error, I would imagine, not if everything is working fine?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC