Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
***I am Writing a program for my cs1 class that is supposed to take dates from an input file and output if the date is wrong or write. If the format is wrong it will put "Invalid format" if the date is wrong(not possible) then it will display "Invalid date". I the date has correct format and date then it should display the date from the input file ect. I am new to functions and am having problems. Can anyone help me with the code I have written so far below? ***

C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string header (void);
bool ValidFormat (int, int, int);
bool ValidDate (string);
void parser (string, int *, int *, int *);
int length = 10;
int month, day, year;
int main()
{
	
	string dates; // this string will be used for any dates in the program
	ifstream infile;
	ofstream outfile;
	infile.open("glasgow_ian_prog4.dat");
	outfile.open("glasgow_ian_dates.txt");
	
	outfile << header(); // this is used in displaying my header
	while (infile >> dates)
	{
		if (ValidDate(dates) == false)
			outfile << "Invalid Format" << endl;
		if (ValidFormat(month, day, year) == false && ValidDate(dates) == true)
			outfile << "Invalid Date" << endl;
		if (ValidFormat(month, day, year) == true && ValidDate(dates) == true)
			outfile << dates << endl;
		parser(dates, & month, & day, & year); // this is here for the parser and will read in the string and integers
	}
	outfile << header();// this is used in displaying my header2
	system("pause");
	return 0;
}

string header(void)
{
	return "********************************Ian Glasgow********************************\n";
}

bool ValidDate(string dates)
{
	bool correctFormat = false;//must be set to false because we are always finding anything wrong even if there is something true.
	if (dates[2] == '/' && dates[5] == '/' && length == 10) 
	if (correctFormat == true);
	return correctFormat;
}

bool ValidFormat(int month, int day, int year)
{
	if (year < 1582)
		return false;
	if (year == 1582 && month < 10)
			return false;
	if (year == 1582 && month == 10 && day < 15)
		return false;
	if (month > 12 || month < 1) 
		return false;
	if ((day > 28 || day < 1) && month == 2 && year % 4 != 0) //this checks not on leap year for february 
		return false;
	if ((day > 29 || day < 1) && year % 4 == 0 && month == 2)// this checks to see if days are correct for leap years
		return false;
	if ((day > 30 || day < 1) && (month == 4))// checking these months for days over 30
		return false;
	if ((day > 30 || day < 1) && (month == 6))
		return false;
	if ((day > 30 || day < 1) && (month == 9))
		return false;
	if ((day > 30 || day < 1) && (month == 11))
		return false;
	if ((day > 31 || day < 1) && month == 1)// checking if days are more than 31 for these months
		return false;
	if ((day > 31 || day < 1) && month == 3)
		return false;
	if ((day > 31 || day < 1) && month == 5)
		return false;
	if ((day > 31 || day < 1) && month == 7)
		return false;
	if ((day > 31 || day < 1) && month == 8)
		return false;
	if ((day > 31 || day < 1) && month == 10)
		return false;
	if ((day > 31 || day < 1) && month == 12)
		return false;
	else
		return true;
}

void parser(string date, int *month, int *day, int *year)
{
	int m1, m2;
	int d1, d2;
	int y1, y2, y3, y4;
	m1 = date[0] - '0';//setting the space values to place ignoring the /
	m2 = date[1] - '0';
	*month = m1 * 10 + m2;// 
	d1 = date[3] - '0';
	d2 = date[4] - '0';
	*day = d1 * 10 + d2;
	y1 = date[6] - '0';
	y2 = date[7] - '0';
	y3 = date[8] - '0';
	y4 = date[9] - '0';
	*year = y1 * 1000 + y2 * 100 + y3 * 10 + y4;
}
Posted
Comments
Richard MacCutchan 13-Nov-14 3:46am    
You need to combine some of your conditions. For example, why test if day < 1 more than once? Similarly if day is 31 then you only need to check if month is 1, 3, 5, 7, 8, 10 or 12, and that can be done in one compound statement.

The ValidDate function is obviously not doing what it should do. Using indenting you should see it (and your compiler should also show a warning when set to higher warning level):
C++
bool correctFormat = false;
if (dates[2] == '/' && dates[5] == '/' && length == 10)
    if (correctFormat == true);
return correctFormat;


The names of the ValidDate and ValidFormat functions are interchanged (each does the job of the other).

Your leap year calculation is not correct. There are special rules for every 100th and 400th year.

Overall you might think about different solutions to parts of your program. E.g. there are standard library functions to convert strings to numbers.
 
Share this answer
 
Comments
Member 11229837 13-Nov-14 4:09am    
One the ValidDate function, what I am not seeing a solution to the formatting. And yes, I seem to get the warning... :P
Jochen Arndt 13-Nov-14 4:46am    
There are multiple solutions:
You may have a look at the string parsing function sscanf() and the string to number conversion function strtoul(). With strtoul() use the endptr parameter to check the termination character. Using these you can make a function that performs both checks for format and valid date. Or just parse the string yourself checking for digits, number of digits, and valid separators.

When working on Unix systems, there is also the strptime() function. But I guess your solution should be OS independant.

Please note that I only give you general hints because this is some kind of homework that should be done by you. But you are welcome to ask specific questions when you got stuck somewhere (like you have done already).
Member 11229837 13-Nov-14 5:07am    
Thank you for your help. I will research this. I do need to have four seperate functions though as part of the rubric.
See this Stack Overflow question: "C++ check if a date is valid"[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900