I’m running through some programming exercises and one involves implementing a date class object from scratch.
Overall the class design and implementation part of it was a piece of cake but the trouble came along during date input and copying to private data members.
The date input method must be in the format MM/DD/YY and class data members must be three int types (month, day, year) for the exercise.
So my implementation consists of taking input using cin to a character array and then converting those array elements to int data types for the class data members.
Initially I thought to take each number column separately, multiply the tens column by ten and add the ones column in to get the int but that didn’t pan out since your still doing mathematical operands on a character data type.
I did find a solution, but its a kludge and I feel there’s gotta be a better, more eloquent way of doing this.
Also as a side note, I was wondering if someone could tell me why I get a stack overflow error during runtime when I reduce my char array size to 8 from 9. I thought char arrays only needed the size + 1 for the \0 character. By my count I should be good with temp[8] for a total of 9 spots (2 day 2 month 2 year and 2 / and 1 \0 =9) I’m sure I’m missing something here. Anyways i’m using VC++ 2008 for my compiler if that matters.
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::cin;
//---------------------------------------------------------------------------
class date {
private:
int day, month, year;
public:
date() : day(0), month(0), year(0) {};
void getDate(char*);
void showDate();
int chartoint(char);
};
int date::chartoint(char a) {
switch(a) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
};
};
void date::getDate(char dArray[]) {
day=(chartoint(dArray[3])*10+chartoint(dArray[4]));
month=(chartoint(dArray[0])*10+chartoint(dArray[1]));
year=(chartoint(dArray[6])*10+chartoint(dArray[7]));
};
void date::showDate()
{
cout << std::setiosflags(std::ios::fixed | std::ios::showpoint);
(month<10) ? cout << std::setw(2) << std::setfill('0') << month : cout << month;
cout << "/";
(day<10) ? cout << std::setw(2) << std::setfill('0') << day : cout << day;
cout << "/";
(year<10) ? cout << std::setw(2) << std::setfill('0') << year : cout << year;
cout << endl;
};
//---------------------------------------------------------------------------
int main()
{
date a;
char temp[9];
cout << "Enter Date (mm/dd/yy): ";
cin >> temp;
a.getDate(temp);
a.showDate();
return 0;
}
Extremely simple solution (although C-style) would be to read
intdirectly fromchar*by using functionsscanf():C++ solution would use
std::stringinstead ofchar*, thus body of main should look like this:then your
getDatemethod could take reference tostd::stringobject, that is not going to change, in other wordsconst std::string&. For reading values fromstd::stringyou could constructstd::istringstreamobject then:Also note that method
chartointis redundant andshowDate()could be simple like this: