A wee bit of help

Reply

Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A wee bit of help

 
1
  #11
May 26th, 2008
Argh.

OK. You are parsing a standard CSV file, with a header record.

Handle it thus:
  1. #include <algorithm> // for min() and max()
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. ifstream csvfile( "foo.csv" );
  7. if (!csvfile)
  8. {
  9. complain();
  10. return;
  11. }
  12.  
  13. string target_date = "07/01/1999";
  14.  
  15. string record;
  16.  
  17. // skip the header record
  18. getline( csvfile, record );
  19.  
  20. // examine all remaining records
  21. unsigned record_number = 0;
  22. while (getline( csvfile, record ))
  23. {
  24. stringstream fields( record );
  25. string field;
  26. for (unsigned field_number = 0; getline( fields, field, ',' ); field_number++)
  27. switch (field_number)
  28. {
  29. case 0:
  30. if (field != target_date) goto l_next_record; // valid!
  31. break;
  32.  
  33. case 1: // we don't care what hour for avg, min, or max of anything
  34. break;
  35.  
  36. case 2:
  37. // Convert the field to a number (complain if it isn't)
  38. int temperature;
  39. if (!(stringstream( field ) >> temperature))
  40. {
  41. complain();
  42. break;
  43. }
  44. // If this is the very first record, initialize our min and max
  45. if (record_number == 0)
  46. {
  47. min_temperature := temperature;
  48. max_temperature := temperature;
  49. }
  50. min_temperature := min( min_temperature, temperature );
  51. max_temperature := max( max_temperature, temperature );
  52. break;
  53.  
  54. case ...
  55. }
  56. l_next_record:
  57. record_number++;
  58. }
  59.  
  60. csvfile.close();

Hope this helps.
Last edited by Duoas; May 26th, 2008 at 1:23 pm. Reason: fixed bloomin typo
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 23
Reputation: WesFox13 is an unknown quantity at this point 
Solved Threads: 0
WesFox13 WesFox13 is offline Offline
Newbie Poster

Re: A wee bit of help

 
0
  #12
May 26th, 2008
Hey Duoas, I tried to do what you did but:
fatal error C1903: unable to recover from previous error(s); stopping compilation

And the file I am getting the data from is a text file and here's my code as it was when I asked the question
  1. ///
  2. ///
  3. ///
  4. ///
  5. ///
  6. /// Program: Weather data processing
  7. /// Created by: Wesley Montgomery
  8. /// This program anlizes weather data accessed from a file and finds the average of Temperature, Humidity, and other facts.
  9. ///
  10. ///
  11. ///
  12.  
  13. #include <fstream>
  14. #include <iostream>
  15. #include <string>
  16. #include <cmath>
  17. #include <stdlib.h>
  18. #include <algorithm> // for min() and max()
  19. #include <sstream>
  20.  
  21. #define MAX_SIZE 4589
  22.  
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27. int pressure, temp, windspeed, humidity, rainfall, avgwindspeed, peakwindspeed, totalrainfall;
  28. bool done; // exit flag
  29. char selection; //Menu Selection
  30. string file; // Name of datafile
  31. ifstream weatherfile; //datafile stream
  32.  
  33.  
  34. // Initialize exit flag
  35. done = false;
  36.  
  37. while (!done)
  38. {
  39. // Output menu list
  40. cout << "\n\n";
  41. cout << "************************\n\n";
  42. cout << " Weather Data Analysis: \n";
  43. cout << " 1) Select Data File to be loaded\n";
  44. cout << " 2) Daily Min/max of Temp,Pressure and humidity\n";
  45. cout << " 3) Daily Max Wind Speed\n";
  46. cout << " 4) Avg Pressure, Humidity, and Windspeed for rainy days\n";
  47. cout << " 5) Total Daily Rainfall\n";
  48. cout << " 6) Exit Program\n";
  49. cout << "************************\n\n";
  50.  
  51. // Input data
  52. cout << "Selection -> ";
  53. cin >> selection;
  54. cout << "\n\n";
  55.  
  56. // Process input
  57. if (selection=='1')
  58. {
  59. cout << "Insert Data file:";
  60. cin >> file;
  61. weatherfile.open (file.c_str(), ios::in);
  62. cout << "\n\n";
  63. }
  64. else if (selection=='2')
  65. {
  66. while (!weatherfile.eof())
  67. {
  68. }
  69. }
  70. else if (selection=='3')
  71. {
  72. while (!weatherfile.eof())
  73. {
  74. }
  75.  
  76. }
  77. else if (selection=='4')
  78. {
  79. while (!weatherfile.eof())
  80. {
  81. if (rainfall > 0)
  82. {
  83. }
  84. }
  85.  
  86. }
  87. else if (selection=='5')
  88. {
  89. while (!weatherfile.eof())
  90. {
  91. }
  92. }
  93. else if (selection=='6')
  94. {
  95. cout << "Goodbye. Thank you for using my program.\n\n";
  96. done = true; // flag for exit
  97. }
  98. else
  99. {
  100. cout << "** Invalid Selection. **\n\n";
  101. }
  102.  
  103. } // end of while loop
  104.  
  105. return (0);
  106. }
Now is there any problems with this skeketon of a code that makes what you suggested not work?
Last edited by WesFox13; May 26th, 2008 at 2:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,950
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: 515
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: A wee bit of help

 
0
  #13
May 26th, 2008
Originally Posted by WesFox13 View Post
Hey Duoas, I tried to do what you did but:
fatal error C1903: unable to recover from previous error(s); stopping compilation


Now is there any problems with this skeketon of a code that makes what you suggested not work?
Duoas didn't give you a full functioning program. There is no main function, nor is there a complain() function, so it's not going to compile. Also, you'll probably have to change := in his code to = (may depend on the compiler, there may be a compiler he is using that accepts := ). If you tried to compile Duoas's code exactly as he listed it, it's not going to work. You have to take some of it and execute it in your option 2 (finding min and max) code block.
Last edited by VernonDozier; May 26th, 2008 at 2:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A wee bit of help

 
0
  #14
May 26th, 2008
Oops! I switch between C++ and Delphi a lot... so those :=s should be =s... (sorry )

You will also have to do the following after each time you look through the entire file:
  1. weatherfile.clear();
  2. weatherfile.seekg( 0 );
That turns off the EOF bit (which would prevent further attempts to read the file) and resets the read pointer to the beginning of the file.

BTW, is this homework? (Because if it isn't I can show you something that will make organizing your program much easier, but it is pretty advanced so if it is homework your teacher won't like it and I'll suggest something else.)
Last edited by Duoas; May 26th, 2008 at 3:05 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 23
Reputation: WesFox13 is an unknown quantity at this point 
Solved Threads: 0
WesFox13 WesFox13 is offline Offline
Newbie Poster

Re: A wee bit of help

 
0
  #15
May 26th, 2008
Yes Duoas this is homework, which is why I said in a earlier post that I don't want the work done for me just maybe an explination or a little bit of help to make my program work.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 23
Reputation: WesFox13 is an unknown quantity at this point 
Solved Threads: 0
WesFox13 WesFox13 is offline Offline
Newbie Poster

Re: A wee bit of help

 
0
  #16
May 26th, 2008
Btw here's what I have so far in my code
  1. ///
  2. ///
  3. ///
  4. ///
  5. ///
  6. /// Program: Weather data processing
  7. /// Created by: Wesley Montgomery
  8. /// This program anlizes weather data accessed from a file and finds the average of Temperature, Humidity, and other facts.
  9. ///
  10. ///
  11. ///
  12.  
  13. #include <fstream>
  14. #include <iostream>
  15. #include <string>
  16. #include <cmath>
  17. #include <stdlib.h>
  18. #include <algorithm> // for min() and max()
  19. #include <sstream>
  20.  
  21. #define MAX_SIZE 4589
  22.  
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27. int pressure, temp, windspeed, humidity, rainfall, peakwindspeed, min_temp, max_temp, totalrainfall;
  28. bool done; // exit flag
  29. char selection; //Menu Selection
  30. string file; // Name of datafile
  31. ifstream weatherfile; //datafile stream
  32.  
  33.  
  34. // Initialize exit flag
  35. done = false;
  36.  
  37. while (!done)
  38. {
  39. // Output menu list
  40. cout << "\n\n";
  41. cout << "************************\n\n";
  42. cout << " Weather Data Analysis: \n";
  43. cout << " 1) Select Data File to be loaded\n";
  44. cout << " 2) Daily Min/max of Temp,Pressure and humidity\n";
  45. cout << " 3) Daily Max Wind Speed\n";
  46. cout << " 4) Avg Pressure, Humidity, and Windspeed for rainy days\n";
  47. cout << " 5) \n";
  48. cout << " 6) Exit Program\n";
  49. cout << "************************\n\n";
  50.  
  51. // Input data
  52. cout << "Selection -> ";
  53. cin >> selection;
  54. cout << "\n\n";
  55.  
  56. // Process input
  57. if (selection=='1')
  58. {
  59. cout << "Insert Data file:";
  60. cin >> file;
  61. weatherfile.open (file.c_str(), ios::in);
  62. cout << "\n\n";
  63. }
  64. else if (selection=='2')
  65. {
  66. while (!weatherfile.eof())
  67. {
  68. string target_date = "04/18/2008";
  69.  
  70. string record;
  71.  
  72. // skip the header record
  73. getline( weatherfile, record );
  74.  
  75. // examine all remaining records
  76. unsigned record_number = 0;
  77. while (getline( weatherfile, record ))
  78. {
  79. stringstream fields( record );
  80. string field;
  81. for (unsigned field_number = 0; getline( fields, field, ',' ); field_number++)
  82. switch (field_number)
  83. {
  84. case 0:
  85. if (field != target_date) goto l_next_record; // valid!
  86. break;
  87.  
  88. case 1: // we don't care what hour for avg, min, or max of anything
  89. break;
  90.  
  91. case 2:
  92. // Convert the field to a number (complain if it isn't)
  93. int temp;
  94. if (!(stringstream( field ) >> temp))
  95. {
  96. cout << "Not a Temperature." << endl;
  97. break;
  98. }
  99. // If this is the very first record, initialize our min and max
  100. if (record_number == 0)
  101. {
  102. min_temp = temp;
  103. max_temp = temp;
  104. }
  105. min_temp = min( min_temp, temp);
  106. max_temp = max( max_temp, temp);
  107. break;
  108. }
  109. l_next_record:
  110. record_number++;
  111. }
  112. cout << "The Maximum Temperature is" << max_temp << "degrees." << endl;
  113. cout << "The Minimum Temperature is" << min_temp << "degrees." << endl;
  114.  
  115. }
  116. }
  117. else if (selection=='3')
  118. {
  119. while (!weatherfile.eof())
  120. {
  121. }
  122.  
  123. }
  124. else if (selection=='4')
  125. {
  126. while (!weatherfile.eof())
  127. {
  128. if (rainfall > 0)
  129. {
  130.  
  131. }
  132. }
  133.  
  134. }
  135. else if (selection=='5')
  136. {
  137. while (!weatherfile.eof())
  138. {
  139.  
  140. }
  141. }
  142. else if (selection=='6')
  143. {
  144. cout << "Goodbye. Thank you for using my program.\n\n";
  145. done = true; // flag for exit
  146. }
  147. else
  148. {
  149. cout << "** Invalid Selection. **\n\n";
  150. }
  151. weatherfile.close();
  152.  
  153. } // end of while loop
  154.  
  155. return (0);
  156. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A wee bit of help

 
0
  #17
May 26th, 2008
OK then, I'll give you the simpler suggestion. (Don't worry, I won't do your work for you.) It doesn't have the same coolness factor, but it is just as effective.

Since you have several options that require you to parse the file once for each option, that means you will have to repeat a lot of code to do basically the same thing in your already very large, hard to read (because it is so big) main() function.

What I suggest that you do is make another function that handles all the parsing of the file. All you have to do is tell it which field number you are interested in, and the function will search through the file and compute the minimum, maximum, and average for the selected field.

You may also want to control which subset of records are accessed based upon the date. For example, above in my example I only accessed records where field 0 contained the date "07/01/1999".

So, given those options (pronounced arg-u-ments ) we can write a function prototype:
  1. void read_file(
  2. ifstream& file, // The open and ready-to-read csv data file
  3. string targetdate, // The date we are interested in (see note below)
  4. int fieldindex, // The index of the field we are interested in
  5. double& min, // Where to put the minimum of the indexed field
  6. double& max, // Where to put the maximum
  7. double& avg // Where to put the average
  8. );
You can then, inside the function, gather all the required information (min,max,avg) and return them via the reference arguments. You'll have to fill in the body of the function. (Don't forget to reset the file at the end of the function.)

Now, to find the daily min/max temp for a specific day, you can just say:
  1. string targetdate = "01/07/1999"; // whatever the target day is
  2. double min_temp, max_temp, avg_temp;
  3. read_file( weatherfile, targetdate, min_temp, max_temp, avg_temp );
  4. cout << targetdate << ": ";
  5. cout << setw(10) << setprecision(2) << min_temp;
  6. cout << setw(10) << setprecision(2) << max_temp;

note
This seems a fairly intense project. Is the data file you posted the entire datafile, or are other days involved (like "01/08/1999")?

If it is just one day you can forget all that stuff about the target date.

But if it is more than one day then you'll have to either loop on the date or ask the user which date he is interested in.


The point
The whole reason I suggest this is to break things up into smaller, individual things. When you can concentrate on doing one thing (or less than three things) right at a time, and not have to worry about anything else, then it is much easier to see how your program works and fits together.

The main() function should direct the show, but avoid doing all the dirty work. Make sense?

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 23
Reputation: WesFox13 is an unknown quantity at this point 
Solved Threads: 0
WesFox13 WesFox13 is offline Offline
Newbie Poster

Re: A wee bit of help

 
0
  #18
May 26th, 2008
This is the entire datafile I'm using, it encompasses 3 days of data.
Attached Files
File Type: txt April18-April20-2008.txt (18.5 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 23
Reputation: WesFox13 is an unknown quantity at this point 
Solved Threads: 0
WesFox13 WesFox13 is offline Offline
Newbie Poster

Re: A wee bit of help

 
0
  #19
May 26th, 2008
This is the entire datafile I'm using, it encompasses 3 days of data.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A wee bit of help

 
0
  #20
May 26th, 2008
Joy. So, when the user presses '2' is the program supposed to ask for the day or just list the min & max temp for each day in the file?

[edit] and if I'm quick enough on this edit, does your teacher expect you to split the date up into numbers or can you just assume that the user will input a complete and properly constructed date?
Last edited by Duoas; May 26th, 2008 at 7:43 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2342 | Replies: 38
Thread Tools Search this Thread



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

©2003 - 2010 DaniWeb® LLC