0

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by raptr_dflo
0

In your first block of code (reading the logfile), since the date should always be the first thing in a line, you may want to consider looping over getline(logfile, line) instead of over logfile >> next . Within the loop, you can create a stringstream for line, and grab the date out of the beginning of that.

As far as your checkdateformat() function, you are unclear on the purpose and use of isdigit(); use it on a character to determine if the character is one of '0', ..., '9', along with a test for intervening '-' characters at the correct position. Once you're sure the format is valid, you should no longer need to use atoi() to grab integers (unless you want to check that months and days are in allowable ranges). Since you order your years, months and days in correct order by decreasing significance, simply comparing strings is sufficient.

Don't forget to call checkdateformat() as needed from within your other block of code before comparing one date against another.

Edited by raptr_dflo: typo

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.