I am writing a program that writes log entries to an external text file and returns them upon request. When called, the return function should output the log entries between two user- submitted dates or outputs the last 100kb of the file if that's what's requested. The logfile stores each entry like: YYYY-MM-DD Logmessage
But I can't get my program to compare only dates. It compares each string it grabs, and when it compares text to a date, it doens't behave properly.
Here's the code:
int returntouser()
{
std::stringstream returnstr;
std::string begdate, enddate;
int validdate = 0;
while (validdate == 0)
{
std::cout << "Enter beginning date in format yyyy-mm-dd: ";
std::cin >> begdate;
std::cout << "Enter ending date in format yyyy-mm-dd: ";
std::cin >> enddate;
if (begdate > enddate)
{
std::cout << "Invalid range. Please enter beginning date before ending date.\n";
validdate = 0;
}
else
{
validdate = 1;
}
}
std::ifstream logfile;
logfile.open(/*log_class.filename*/"log.txt", std::fstream::in);
std::string next;
int ctrlflag = 0;
//psuedocode>>>
while(logfile >> next)
{
if (ctrlflag == 1)
{
if (!(next < enddate))//next is still in bounds
{
returnstr << next;
//continue loop
}
else if (next > enddate)//next out of bounds
{
//return std::stringstream & returnstr;
break;
//exit
}
}
else //if ctrlflag = 0(if not yet streamed)
{
if (next < begdate)//next is not yet in bounds
{
//continue loop
}
else if (next >= begdate) //runs once only; when next is after or is begdate
{
returnstr << next;
ctrlflag = 1;
//continue loop
}
}
}
logfile.close();
}
It should when "next" is a date, compare and choose whether to proceed, but when it's not, just output the text. My test program to try and test for if it is a date is here:
int checkdateformat(std::string input)
{
int num;
const char * c = input.c_str();
for (int i=0; i<4; i++)
{
cout << c[i] << " ";
num = atoi(&c[i]);
if (isdigit(num))
{}
else
cout << "\n\nconversion failed\n\n";
return 0;
}
cout << "First four characters look good";
}
But this does not work accurately (it always says 'conversion failed' on the first character it sees. If anyone can help, that would be great.