Initializing date of birth as int in constructor with a leading zero

I have a program that initializes the date of birth as an integer in the constructor in the format MM/DD/YYYY (12041987). The only issue I'm having is the leading zero in the month. If I use a date such as 04061941, I receive a runtime error due to the compiler thinking it's an octal.

I'm currently using an if statement to pad the output with a zero if the Month < 10 but I'm trying to understand if there is a way to actually keep the initial zero in the program and keep it as an integer. I just need to be pointed in the right direction. Thank you.

Here's a snippet of my code, trimmed to what I'm having issues with.

1
2
3
4
5
  // the constructor initializing the date of birth
  PatientInformation smith(04061941);

  // printing the date of birth
  smith.printPatientInformation();
Keep in mind that the int values are converted by the compiler into binary representation. All binary bits to the left of the value are zero. There is no concept of "saving leading zeroes" with int or long values.

You could pass the birthdate as a string and then parse it apart.

Or you can specify the format when you output the value.
You haven't shown any output code either, so we don't know if you're using printf or cout. So can't make specific recommendatiuons.
Thank you for your reply. Currently I'm using cout to output. I'm somewhat unfamiliar with printf but can I utilize that to bypass the error I'm getting while compiling? I can parse it out without issue, it's just jumping over the initial problem of actually getting the program to compile with a leading zero integer.
You have not shown any code exhibiting an error nor shown the error messages you're getting. If you want help with compile errors, you need to do both.

You missed my point. Leading zeroes on an integer are NOT significant (except to the compiler which interprets a leading zero as meaning an octal number). Consider the following decimal numbers:
10
010
0010
All are stored in binary as 00001010. It does not matter how many leading zeroes they have, all are stored the same.

Since you're using cout, you need to look at <iomanip>.
http://www.cplusplus.com/reference/iomanip/
In particular, setw and setfill.


Last edited on
Apologies for not being more specific in the OP.

From the header file the constructor is initialized as such:

1
2
3
4
PatientDemographicInformation(String medicalRecordNo, String firstName, 
	char middleInitial, String lastName, String streetAddress1, String streetAddress2,
	String city, String state, String zip5,String zip4, String homeAreaCode, String homePhoneNo, 
	char gender, int dateOfBirth);


The test program is using the current values in the constructor...:

1
2
PatientDemographicInformation smith("123456789", "Robert", 'A', "Smith", "CS Department, Building #4, Room #403", 
	"101 West End Boulevard", "Pittsburgh", "PA", "15224", "3899", "550", "7400586", 'M', 01011967);


...and printing this information via the following function:

smith.printPatientDemographicInformation();

Here's the full function from the header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
void PatientDemographicInformation::printPatientDemographicInformation(  )
{
	cout << "----------------------PATIENT INFORMATION----------------------" << endl;
	cout << "Medical Record No.: " << getPatientMedicalRecordNo( ) << endl;
	cout << "    Patient's Name: " << getPatientName( ) << endl;
	cout << "           Address: "; printPatientAddress( );
	cout << setw(33) << getPatientPhoneNumber( );
	cout << endl << endl;
	cout << "Gender: " << getPatientGender( );
	cout << "     Date of Birth: "; getPatientDateOfBirth( ); 
	cout << "     Age: " << getPatientAge( ) << endl;
	cout << "---------------------------------------------------------------" << endl;
}


The error I receive when compiling is: "error C2041: illegal digit '9' for base '8'"

I have to use the int entered as 01011967 and output it as MM/DD/YYYY using the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        int birthMonth;
	int birthDay;
	int birthYear;

	birthMonth = (patientDateOfBirth / 1000000);
	birthDay = ((patientDateOfBirth % 1000000) / 10000);
	birthYear = (patientDateOfBirth % 10000);

	if(birthMonth < 10)
	{
		cout << '0' << birthMonth << "/" << birthDay << "/" << birthYear;
	}
	else
	{
		cout << birthMonth << "/" << birthDay << "/" << birthYear;


Currently I'm leaving the lead zero out of the constructor (1011967) and it works flawlessly (you'll notice the workaround I have for months less than 10 in the previous snippet is to pad with a zero) however my instructor informed me that it would be incorrect and that there is another way.

I understand what an octal is and I also understand that generally the leading zero is not significant, but in this particular scenario I need to keep the zero as part of the month as it's a required part of my assignment.

I'm not looking for someone to do this for me, I just need to be pointed in the right direction.

Thank you.

See the link to <iomanip> I posted above.
Hint: You want a width of 2 and a fill of '0'.
I still receive the same error because the int is defined in the constructor as 01011967 so the program will not even run when the leading zero is present as it thinks I'm trying to use an octal. If I remove the leading zero and use what you suggested...

cout << setfill('0') << setw(2) << birthMonth << "/" << birthDay << "/" << birthYear;

.. it works. But I need to keep that lead zero in the constructor.

But I need to keep that lead zero in the constructor.

Why? If you keep it, you're telling the compiler that number is octal and of course the 9 is not a valid octal digit. You can't have it both ways.

Are you allowed to pass the birthdate as a string? If so, then you can parse apart the string.
I cannot change the data type of the date of birth from int to String if that's what you're referring to.

So I assume correctly when I say that it is literally impossible to do what I am describing? I believe I'll just stick to setfill and setw in that case.
Topic archived. No new replies allowed.