Solved

Get text month given integer month

Posted on 2006-06-14
2
235 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
Comment Utility
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
Comment Utility
#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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now