The best way to keep the values sorted/ordered is to store in yyyymmdd or yyyy/mm/dd format.
If the values are sorted in the above format and yes, my logic should work.
It is very simple logic.
And in your code, you are mixing up the values StartDataPt in part-1 and part-2, as you are storing the location/position in first place and immediately in next loop you are replacing it with a date. And i-1 in part-2 is unknow as you are using "x" for the loop.
I am not even sure why are you giving two loops for one. If you want to locate a start date closest to the given, try similar to the end date I suggested. Meaning, replace the part-1 and part-2 with below:
(assuming you are looking for closest suitable starting date to the given)
1 2 3 4 5 6 7 8 9 10
|
for (int x = 0; x < MAX_NUM && StartDate >= d[x].dat; x++); // single-line loop
StartDatePt = d[x-1].date;
for (int i = 0; i < MAX_NUM && EndDate <= d[i].dat; i++); // single-line
EndDatePt = d[i-1].date;
|
Remove all part-1, 2, 3, and 4 and place the above code and just run it with your print loop.
Thats all.
If your date structure is not sorted, post it here and I would like to see. If possible, following the above suggested format to store the dates in. It would be easier to sort them out.
If your data is not sorted/formatted in the way I suggested, there is a quick work-around.
Change the date string format on fly and compare it as desired.
For example, assuming your date members of the structure store the values those are same of date string, create a temporary variable to combine those three date values using a stringstream like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <sstream> // include at top
//inside the function
ostringstream ostr;
StartDate = "2007/12/18"; // store in yyyy/mm/dd format
EndDate = "2007/12/31";
//in the loop
{
ostr << d[i].yr << "/";
if (d[i].month < 10) ostr << "0";
ostr << d[i].month; << "/"
if (d[i].day < 10) ostr << "0";
ostr << d[i].day;
string dateString = ostr.str(); // extract the date from formated stream
//now check here the dateString against StartDate or EndDate
}
|
Though I would not recommend strstream as it is predecated, you could use ostrstream for such concatenation, if your compiler supports. Please note strstream works with char * rather than string objects and some compilers do not support it any more.
If your date member struct is already stores the dates in yyyy/mm/dd format, then you would not need strstream proc at all. Just follow the other steps suggested.
Check it out. Good luck :)
EDIT: I just cut and pasted your code and modified it to be as suggested above;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream> // add this new one
#include <string>
//#include <stdlib.h> // may not be needed unless you used any c func
//#include <stdio.h> // ditto
using namespace std;
//using std::cout; // above namespace works for the same purpose
//using std::endl;
const int MAX_NUM=2500;
struct MyStruct
{
int month, day, yr;
float price;
string date; // the string date
long index;
};
MyStruct d[MAX_NUM];
void PriceList (string StartDate, string EndDate, float price_array[])
{
StartDate = "2007/12/18"; // change the format to be yyyy/mm/dd
EndDate = "2007/12/31";
int StartEndPt = 0; // location in the array
int EndDatePt= 0;
ostringstream ostr;
string dateString;
for (int i = 0; i < MAX_NUM; i++)
{
ostr.clear();
ostr << d[i].yr << "/";
if (d[i].month < 10) ostr << "0";
ostr << d[i].month; << "/"
if (d[i].day < 10) ostr << "0";
ostr << d[i].day;
dateString = ostr.str(); // extract the date from formated stream
//now check here the dateString against StartDate or EndDate
if (StartDate >= dateString) // search no further if StartDate is found be lower than the date string
continue;
else
break;
}
//now note the position where it stopped at last
StartDatePt = i-1;
// now do the similar for EndDate too
for (int i = 0; i < MAX_NUM; i++)
{
ostr.clear();
ostr << d[i].yr << "/";
if (d[i].month < 10) ostr << "0";
ostr << d[i].month; << "/"
if (d[i].day < 10) ostr << "0";
ostr << d[i].day;
dateString = ostr.str(); // extract the date from formated stream
//now check here the dateString against StartDate or EndDate
if (EndDate <= dateString) // search no further if EndDate found to be greater
continue;
else
break;
}
EndDatePt = i-1;
// print out the prices between the specified dates
for (int x = StartDatePt; x <= EndDatePt; x++)
{
price_array[x] = d[x].price;
cout << "The prices between " << StartDate << " and " << EndDate << " :" << price_array[x] << endl;
}
}
|