Solved

Get text month given integer month

Posted on 2006-06-14
Medium Priority
240 Views
I know I can write a simple function to do this, but can show me an existing library function to return the text month (i.e., Jun or June) given the integer representation (1-12).

Thanks,

Joe
0
Question by:jribble
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 19

Accepted Solution

ID: 16902879
You could do this:

// Get the current time
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );

// Hack the month member of the time object to be whatever we want..
timeinfo->tm_mon = 1;  // Adjust this to whatever month you wish to find the string representation of (0 - 11)
TCHAR szBuf[16];

// Format the time object into a string representation
_tcsftime(szBuf, 16, _T("%B"), timeinfo);

Here, szBuf is "February"

%B is a format string that returns the full current month; read more in the online docs for _tcsftime

HTH
0

LVL 8

Expert Comment

ID: 16902938
#include <iostream>

using namespace std;

const char* Month[] = {"Months:","January","Febuary","March","April",
"May","June","July","August","September","October","November",
"December"};
int main ()
{
cout << "Please enter a month (1-12):";
int GMonth;
cin >> GMonth;
cout << "The month you entered was, " << Month[GMonth] << endl;
return 0;
}

I know this isn't what you want, but I personally would just use an array like this.  That way your program
relies on a lot less for doing this.  For example, MrWad's example requires Time.h and Tchar.h and still
is based on 0-11 not 1-12.  Is there a reason you want to use built-in libraries?
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a Gâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll