473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

string Date comparison

Hi,

I have a vector<string> Date, which contains all the dates. I will be given a starting date and the ending date. I want to compare the 'staring date' with the dates in the Date vector. I am getting an error while I try to do this.

suppose Date vector has the date of the form

Date
12/31/2007
12/30/2007
12/29/2007
.
.
.
12/01/2007

I am given the staring date as 12/10/2007 and ending date as 12/17/2007. I want the index at which these dates occur. I want to compare my starting and ending strings and get the index out of it. This is what I have done so far

Expand|Select|Wrap|Line Numbers
  1. float Get_Price(string startingDate, string endingDate)
  2. {
  3.     int Lowindex, Highindex;
  4.     for( int i=0; i<Date.size(), i++)
  5.     {
  6.         if ( strcmp (startingDate, Date.at(i)) == 0 )
  7.               Lowindex = i;
  8.         if ( strcmp (endingDate, Date.at(i)) == 0 )
  9.               Highindex = i;
  10.  
  11.     }
  12. }
I am getting an error! where am I doing wrong?

Thanks
Raam
Sep 28 '07 #1
9 5555
Savage
1,764 Expert 1GB
Hi,

I have a vector<string> Date, which contains all the dates. I will be given a starting date and the ending date. I want to compare the 'staring date' with the dates in the Date vector. I am getting an error while I try to do this.

suppose Date vector has the date of the form

Date
12/31/2007
12/30/2007
12/29/2007
.
.
.
12/01/2007

I am given the staring date as 12/10/2007 and ending date as 12/17/2007. I want the index at which these dates occur. I want to compare my starting and ending strings and get the index out of it. This is what I have done so far

float Get_Price(string startingDate, string endingDate)
{
int Lowindex, Highindex;
for( int i=0; i<Date.size(), i++)
{
if ( strcmp (startingDate, Date.at(i)) == 0 )
Lowindex = i;
if ( strcmp (endingDate, Date.at(i)) == 0 )
Highindex = i;

}
}

I am getting an error! where am I doing wrong?

Thanks
Raam
You said that it's vector of strings,right?And strcmp takes as a argument a c style string which is a char array.Use c_str() to get char array and then compare ,or you can use overloaded equality operator:

e.g

if(startingDate==Date.at(i))

Savage
Sep 28 '07 #2
Ganon11
3,652 Expert 2GB
std::string has overloaded the == operator? I didn't know that! Here I am using std::string::compare(std::string other) like a fool...
Sep 28 '07 #3
Savage
1,764 Expert 1GB
std::string has overloaded the == operator? I didn't know that! Here I am using std::string::compare(std::string other) like a fool...
Yep,here's the list of all overloaded string operators.

Savage
Sep 28 '07 #4
Thank you. Initially, I tried to use If (startingDate == Date.at(i) ) but it doesn't give me the compilation error but its core dumping. Is there anyway to do this with out converting into a char string?

Thanks
Raam
Sep 28 '07 #5
Savage
1,764 Expert 1GB
Thank you. Initially, I tried to use If (startingDate == Date.at(i) ) but it doesn't give me the compilation error but its core dumping. Is there anyway to do this with out converting into a char string?

Thanks
Raam
Core dumping?

Are you sure that this statement produce it?

Perhaps error is somewhere else.

About your question you can use compare Gannon mentioned but == is based on it so the result will be the same.

Savage
Sep 28 '07 #6
To check whether its within that for loop. I tried to print out my Date vector.

for( int i=0; i<=Date.size(); i++)
{
cout<< Date.at(i)<<endl;
}

This function is printing all the dates but its never coming out of the loop. Do you have any ideas why its happening?

Thanks
Raam
Sep 28 '07 #7
Here is my full code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6. using std::cerr;
  7. using std::cout;
  8. using std::endl;
  9.  
  10. #include<fstream>
  11.  
  12. using std::ifstream;
  13. using std::vector;
  14.  
  15. #include<cstdlib> // this is exit function
  16. vector<string> Date;
  17. vector<float> Price;
  18.  
  19. // Read the values from CompFinSumAssi.txt
  20.  
  21. void writeData(string date, vector<string>& Date, vector<float>& Price, float);
  22.  
  23. int Load_Price_Data() {
  24.   ifstream indata;
  25.  
  26.   vector<float> priceVector; //variable for input price value
  27.   vector<string> dateVector; //variable for input date
  28.   float price;
  29.   string date;
  30.  
  31.   indata.open("SumAssiData.txt"); //opens the file
  32.   if(!indata) {
  33.     cerr << "Error: file could not be opened" << endl;
  34.     exit(1);
  35.   }
  36.  
  37.   // Read the SumAssiData file
  38.   indata>> date>> price;
  39.  
  40.   while( !indata.eof() ) { // keep reading until end-of-file
  41.     writeData(date, Date, Price, price);
  42.     indata>>date >> price;
  43.   }
  44.  
  45.   indata.close();
  46.   cout<<"Size of Date is: "<<Date.size()<<endl;
  47.   cout <<"End of file is reached" << endl;
  48.   return 0;
  49. }
  50.  
  51.  
  52. void writeData(string date, vector<string>& Date, vector<float>& Price, float price) {
  53.  
  54.   // vector<string> Date;
  55.   //vector<float> Price;
  56.     Date.push_back(date);
  57.     Price.push_back(price);
  58.     //cout<<Date.size()<<endl;
  59.  
  60.     //  for(int i=0; i<Date.size(); i++) {
  61.     //cout<<Date.at(i)<<endl;
  62.     //}
  63.   //cout<<" size of Date is: "<<Date.size()<<endl;
  64.  
  65. }
  66.  
  67. float Get_Price(string startingDate, string endingDate) {
  68.  
  69.   int Lowindex, Highindex;
  70.   float sumPrice, Avgindex, AveragePrice;
  71.  
  72.   cout<< " I reached here: Get_Price"<<endl;
  73.   cout<<" Date size is:"<<Date.size()<<endl;
  74.   cout<< "startingDate is:" <<startingDate<<endl;
  75.  
  76.   for(int i=0; i<=Date.size(); i++)
  77.     {
  78.       cout<<Date.at(i)<<endl;
  79.     }
  80.   cout<<" I came out of cout for loop for Date" <<endl;
  81. // This is self is not printing 
  82.  
  83.  
  84.   for(int i=0; i<=Date.size(); i++){
  85.     cout<<"I am inside the for loop"<<endl;
  86.  
  87.     if(startingDate == Date.at(i) ) {
  88.       cout<<" I am inside the if statement"<<endl;
  89.       // if( strcmp( startingDate, Date.at(i) ) == 0 )
  90.       Lowindex = i;
  91.       cout<<" Low index is: "<< Lowindex<<endl; 
  92.     }
  93.  
  94.     if(endingDate == Date.at(i) ) {
  95.       cout<< " I am inside the endingDate if"<<endl;
  96.       // if( strcmp( endingDate, Date.at(i) > == 0 )
  97.       Highindex = i;
  98.       cout<<" high Index is: "<<Highindex<<endl;
  99.     } else
  100.       cout<< " No date found: "<<endl;
  101.   }
  102.  
  103.   sumPrice = 0;
  104.   for(int j=Lowindex; j<=Highindex; j++) {
  105.     sumPrice = sumPrice + Price.at(j);
  106.   } 
  107.  
  108.   Avgindex = Highindex-Lowindex;
  109.   AveragePrice = sumPrice/Avgindex;
  110.  
  111.   cout<< "Average price is: "<<AveragePrice<<endl;
  112.   return AveragePrice;
  113. }
  114.  
  115. int main() {
  116.   Load_Price_Data();
  117.   string startingDate, endingDate;
  118.   startingDate = "8/16/2004";
  119.   endingDate = "9/15/2004";
  120.   Get_Price(startingDate, endingDate);
  121. }
Sep 28 '07 #8
Ganon11
3,652 Expert 2GB
Don't use "i <= Date.size();" This will run past the last element in your vector. If your vector has three strings (say "12/01/89", "12/02/89", and "12/03/89"), then Date.size() returns 3, but these elements are stored in Date[0], Date[1], and Date[2], respectively. Using "i = 0; i <= Date.size()" will mean i becomes 0, 1, 2, and 3 - but Date[3] doesn't exist.

Use i < Date.size() instead.
Sep 28 '07 #9
Thanks, It worked.

Raam
Sep 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Donnal Walter | last post by:
I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15) >>> print x == y True How can these two...
2
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
3
by: andrew | last post by:
Hi: I am already using TreeMap to massage records in my export file such that each record has a unique key combination ( LastName + FirstName + Member Key) . Thus I am sorting the records by...
1
by: .Net Sports | last post by:
I need to parse the date I'm getting from this sqlstring to compare it with today's date. If it's before today's date, i need to show the viewers one page. If it is after today's date, then I need...
6
by: John A Grandy | last post by:
how are people dealing with the situation where a function accepts a String representation of a date ... but a control on the page or form returns a Date value ... strangely, these Date values...
3
by: Tiya | last post by:
Hi there !!! I would like to know how to compare dates in javascript. var sdate = new Date(theform.SubmissionDate.value); var odate = new Date(theform.StartDate.value); var todaysdate = new...
7
by: joeyej | last post by:
How do I compare today's date with this string (in my inc file) so that I can set an alert if date choice i.e. May 15, 2006 not at least greater than two days from current date? <option...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
2
by: rhaazy | last post by:
I need to know how I can format a string in C# to get the current date/ time, so that I can do a comparison against a date time column in MS SQL Server 2005. The date/time column in the database...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.