[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Get text month given integer month

Posted on 2006-06-14
2
Medium Priority
?
241 Views
Last Modified: 2010-04-01
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
Comment
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
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 2000 total points
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

by:List244
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question