1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
const char* months[] =
{
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec"
};
int main()
{
string user_input;
cout << "Please type the name of your favorite month> " << flush;
getline( cin, user_input );
// We're only interested in the first three characters
user_input += "---"; // (but make sure there actually are three...)
user_input.erase( 3 );
// Convert user's input to lowercase
transform(
user_input.begin(),
user_input.end(),
user_input.begin(),
ptr_fun <int, int> ( tolower )
);
// Find the index of the matching month
int month = find(
months,
months +12,
user_input
)
- months;
if (month < 12)
cout << "The month number is " << (month +1) << ".\n";
else
cout << "That was not a month name.\n";
return 0;
}
| |