Solved

Get text month given integer month

Posted on 2006-06-14
2
236 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
2 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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