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
9 5555
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
std::string has overloaded the == operator? I didn't know that! Here I am using std::string::compare(std::string other) like a fool...
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
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
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
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
Here is my full code: - #include <iostream>
-
#include <string>
-
#include <vector>
-
-
using namespace std;
-
using std::cerr;
-
using std::cout;
-
using std::endl;
-
-
#include<fstream>
-
-
using std::ifstream;
-
using std::vector;
-
-
#include<cstdlib> // this is exit function
-
vector<string> Date;
-
vector<float> Price;
-
-
// Read the values from CompFinSumAssi.txt
-
-
void writeData(string date, vector<string>& Date, vector<float>& Price, float);
-
-
int Load_Price_Data() {
-
ifstream indata;
-
-
vector<float> priceVector; //variable for input price value
-
vector<string> dateVector; //variable for input date
-
float price;
-
string date;
-
-
indata.open("SumAssiData.txt"); //opens the file
-
if(!indata) {
-
cerr << "Error: file could not be opened" << endl;
-
exit(1);
-
}
-
-
// Read the SumAssiData file
-
indata>> date>> price;
-
-
while( !indata.eof() ) { // keep reading until end-of-file
-
writeData(date, Date, Price, price);
-
indata>>date >> price;
-
}
-
-
indata.close();
-
cout<<"Size of Date is: "<<Date.size()<<endl;
-
cout <<"End of file is reached" << endl;
-
return 0;
-
}
-
-
-
void writeData(string date, vector<string>& Date, vector<float>& Price, float price) {
-
-
// vector<string> Date;
-
//vector<float> Price;
-
Date.push_back(date);
-
Price.push_back(price);
-
//cout<<Date.size()<<endl;
-
-
// for(int i=0; i<Date.size(); i++) {
-
//cout<<Date.at(i)<<endl;
-
//}
-
//cout<<" size of Date is: "<<Date.size()<<endl;
-
-
}
-
-
float Get_Price(string startingDate, string endingDate) {
-
-
int Lowindex, Highindex;
-
float sumPrice, Avgindex, AveragePrice;
-
-
cout<< " I reached here: Get_Price"<<endl;
-
cout<<" Date size is:"<<Date.size()<<endl;
-
cout<< "startingDate is:" <<startingDate<<endl;
-
-
for(int i=0; i<=Date.size(); i++)
-
{
-
cout<<Date.at(i)<<endl;
-
}
-
cout<<" I came out of cout for loop for Date" <<endl;
- // This is self is not printing
-
-
-
for(int i=0; i<=Date.size(); i++){
-
cout<<"I am inside the for loop"<<endl;
-
-
if(startingDate == Date.at(i) ) {
-
cout<<" I am inside the if statement"<<endl;
-
// if( strcmp( startingDate, Date.at(i) ) == 0 )
-
Lowindex = i;
-
cout<<" Low index is: "<< Lowindex<<endl;
-
}
-
-
if(endingDate == Date.at(i) ) {
-
cout<< " I am inside the endingDate if"<<endl;
-
// if( strcmp( endingDate, Date.at(i) > == 0 )
-
Highindex = i;
-
cout<<" high Index is: "<<Highindex<<endl;
-
} else
-
cout<< " No date found: "<<endl;
-
}
-
-
sumPrice = 0;
-
for(int j=Lowindex; j<=Highindex; j++) {
-
sumPrice = sumPrice + Price.at(j);
-
}
-
-
Avgindex = Highindex-Lowindex;
-
AveragePrice = sumPrice/Avgindex;
-
-
cout<< "Average price is: "<<AveragePrice<<endl;
-
return AveragePrice;
-
}
-
-
int main() {
-
Load_Price_Data();
-
string startingDate, endingDate;
-
startingDate = "8/16/2004";
-
endingDate = "9/15/2004";
-
Get_Price(startingDate, endingDate);
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |