How would I go about converting a Month Name to a Month Number. For example, convert Jan to 1 and Feb to 2, etc. The Month Name is input from the keyboard and a variable called month is of type int in a void function. So I would need to convert the Month Name to a Month Number within the function so it can make month = 1 if the input from the keyboard is Jan. Thanks for any help that you can give me.
//**************
void MonthType::InputMonthName(
istream& ins )
//**************
{
// Reads month as char from the keyboard
cout << "Enter the first three letters of the month name: ";
ins >> month;
switch ( month )
{
case 'J':
month = 1;
break;
case 'F':
month = 2;
break;
case 'M':
month = 3;
break;
case 'A':
month = 4;
break;
case 'S':
month = 9;
break;
case 'O':
month = 10;
break;
case 'N':
month = 11;
break;
case 'D':
month = 12;
break;
case 'y':
month = 5;
break;
case 'n':
month = 6;
break;
case 'l':
month = 7;
break;
case 'g':
month = 8;
break;
}
} // End function InputMonthName( istream& ins )
This is a class function variable and here in the other part that this function uses.
//**************
MonthType::MonthType( char firstLetter, char secondLetter, char thirdLetter )
//**************
{
// Parameterized constructor function for three characters of month
month = firstLetter, secondLetter, thirdLetter;
switch ( firstLetter )
{
case 'J':
month = 1;
break;
case 'F':
month = 2;
break;
case 'M':
month = 3;
break;
case 'A':
month = 4;
break;
case 'S':
month = 9;
break;
case 'O':
month = 10;
break;
case 'N':
month = 11;
break;
case 'D':
month = 12;
break;
}
switch ( thirdLetter )
{
case 'y':
month = 5;
break;
case 'n':
month = 6;
break;
case 'l':
month = 7;
break;
case 'g':
month = 8;
break;
}
} // End function MonthType( char, char, char )
This is the output that I get.
Result of passing May to monthName's char constructor:
The month number is: 5
The month name is: May
Result of loading monthName's month field via characters from the keyboard:
Enter the first three letters of the month name: Jan
The month number is: 5
The month name is: May