By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

string Date comparison

P: 8
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
Share this Question
Share on Google+
9 Replies


Savage
Expert 100+
P: 1,764
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
Expert 2.5K+
P: 3,652
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
Expert 100+
P: 1,764
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

P: 8
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
Expert 100+
P: 1,764
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

P: 8
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

P: 8
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
Expert 2.5K+
P: 3,652
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

P: 8
Thanks, It worked.

Raam
Sep 28 '07 #10

Post your reply

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