Solved

Get text month given integer month

Posted on 2006-06-14
2
238 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Compile GLUT with Visual Studio 2015 1 209
White board coding practice 3 95
passing data from one form to another form in c++ 27 89
Need some help with design 17 23
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

697 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