I have been writing a calendar program and the next stage is to ammend the zeller function so that the user can enter the month in conventional format. for example the user can enter 1 or january.
anyone have any ideas please?
no errors the program works fine so far but i dont know how to do the next stage
i need to know what i have to do so that the user can enter the month number or the month in conventional format eg. january,february etc.
but right now the user can only enter it in number format,
any idea of what i can do to let me enter the actual name?
easiest route for a beginner would be calling the "isalpha()" or "isdigit()" functions, if then statement to determine which of two Zeller functions to use. The only difference between them being one works with number input, the other works with a string input.
There are other ways, but this seems to be the easiest. Just ask questions if any of that didn't make sense
There should only be one Zeller function. The Zeller function only works with integers.
The trick is to properly input the month. What you need is a function that gets the month from the user. The function should recognize whether the user input a number or a string like "April" or "apr" which must be turned into a number.
To determine whether or not the input value is a number or not, you can simply try converting one way first, and the other way second. If both fail then the user input something invalid.
constchar* month_name_abbreviations[] = ...;
// Returns the month as a number in [1, 12].
// If the input was not valid, returns zero.
int get_month()
{
int month;
cout << "Enter the month you were born> ";
if (cin >> month)
cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); // Get rid of the ENTER key
else
{
// The input of an integer failed.
// Try to read it as a month name (just like in the other thread).
cin.clear();
string s;
getline( cin, s );
s += "---";
s.erase( 3 );
transform( ... );
month = find( ... ) - month_name_abbreviations + 1;
}
// If the user's input is not valid, return zero.
if ((0 > month) || (month > 12))
return 0;
// Otherwise return the proper month in [1, 12].
return month;
}
Hope this helps.
[edit] BTW, the month-name-to-integer trick is pretty slick. Your teacher will know that you didn't make it up yourself. Best to own up to getting help on the internet. [/edit]