I am making a program that asks users to enter their date of birth, then outputs it. It uses exception classes. However, the issue I seem to be having is that the input is somehow being lost or translated into undesired numbers. Code:
dob::dob() {
cout << "Enter your date of birth in format MM-DD-YYYY" << endl;
//cin.getline(monthChar, 10);
cin.get(monthChar, 10, '-');
cin.ignore();
cin.get(dayChar, 03, '-');
cin.ignore();
cin.get(yearChar, 04);
cout << dayChar[0];
cout << dayChar[1];
cout << monthChar[0];
cout << monthChar[1];
cout << " " << endl;
day = dayChar[0] * 10 + dayChar[1];
month = monthChar[0] * 10 + monthChar[1];
year = yearChar[0] * 1000 + yearChar[1] * 100 + yearChar[2] * 10 + yearChar[3];
cout << month << day << year;}
when entering 12-12-1996 for input, I get 12 2 5054055270 for output. I read this link for get and seems like I followed all the steps correctly so I miss what my mistake is. Actually, midway through writing this post, I realize why I'm getting the weird numbers at the end, it's because I'm multiplying a char value by 10, but how do I cast to int? is it just static_cast for the char? I still need to answer why my monthChar char at monthChar[0] is replaced by a space. Thanks.
EDIT: New Code:
dob::dob() {
cout << "Enter your date of birth in format MM-DD-YYYY" << endl;
//cin.getline(monthChar, 3);
cin.get(monthChar, 3, '-');
cin.ignore();
cin.get(dayChar, 3, '-');
cin.ignore();
cin.get(yearChar, 5);
cout << dayChar[0];
cout << dayChar[1];
cout << monthChar[0];
cout << monthChar[1];
cout << " " << endl;
day = (dayChar[0]-'0') * 10 + dayChar[1]-'0';
month = (monthChar[0] - '0') * 10 + monthChar[1] - '0';
year = (yearChar[0] - '0') * 1000 + (yearChar[1] - '0') * 100 + (yearChar[2] - '0') * 10 + yearChar[3] - '0';
cout << month << day << year;
}
When I update the code with suggestions from the answers/comments, I get the correct day and year but not month. For input 05-27-1996 I get output
27 5
-45271996
What's going on with the month?