DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Poor Dumb Student Guest

## dates in C++

im a fresher reading computing and learning C++ and id like to know if
there's any simple way of returning the name of a MONTH if given a
specific number instead a string of 12 if statements like

if(month = 1)
cout << january
if month = 2
cout << feb
etc.......
im sure there's one in visual basic so is there a corresponding one in C++.

2. Danny Kalev Guest

## Re: dates in C++

you can use a map or a an array of 12 pairs where each pair contains a
number and a string:

struct month_pair
{
int num;
char name[12];
};

int main()
{
month_pair months[12]={{0,{"January"}}, {1,{"February"}}, ...};

int no=1;
cout << months[no].name; // February

}

Poor Dumb Student wrote:
>
> im a fresher reading computing and learning C++ and id like to know if
> there's any simple way of returning the name of a MONTH if given a
> specific number instead a string of 12 if statements like
>
> if(month = 1)
> cout << january
> if month = 2
> cout << feb
> etc.......
> im sure there's one in visual basic so is there a corresponding one in C++.
>

3. Maria Angelis Guest

## Re: dates in C++

"Poor Dumb Student" <ctzcmp@comp.leeds.ac.uk> wrote:
>
>im a fresher reading computing and learning C++ and id like to know if
>there's any simple way of returning the name of a MONTH if given a
>specific number instead a string of 12 if statements like
>
>if(month = 1)
> cout << january
>if month = 2
> cout << feb
>etc.......
>im sure there's one in visual basic so is there a corresponding one in C++.
>

Dumb not really...its called "opening a C++ book and reading it...lolol"

Okey
here we go
int main
#include <stdio.h>
#include <string.h>
#include <iostream.h>

namespace std;
#define m 12
int main
{
enum month{January, February, March.....};
const string asc[m]={"January", "February"....};
unsigned int i;
month m;
string s;
for(i=o;i<12;i++)
{
if(s==asc[i])
{
return((month)i);
break;
}
}
cout<<"you have inputed an illegal string";
return 0;
}

By the way enum is the coolest tool in C++. Check it out in a book..

4. maria Guest

## Re: dates in C++

Danny Kalev <dannykk@inter.net.il> wrote:
>you can use a map or a an array of 12 pairs where each pair contains a
>number and a string:
>
>struct month_pair
>{
> int num;
> char name[12];
>};
>
>int main()
>{
>month_pair months[12]={{0,{"January"}}, {1,{"February"}}, ...};
>
>int no=1;
>cout << months[no].name; // February
>
>}
>what is this ???

an array with what?
my cat could have done this

highly unprofessional!!
>
>Poor Dumb Student wrote:
>>
>> im a fresher reading computing and learning C++ and id like to know if
>> there's any simple way of returning the name of a MONTH if given a
>> specific number instead a string of 12 if statements like
>>
>> if(month = 1)
>> cout << january
>> if month = 2
>> cout << feb
>> etc.......
>> im sure there's one in visual basic so is there a corresponding one in

C++.
>>

5. venkat Guest

## Re: c++

hi
Send me some web links to learn c++

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

 FAQ Latest Articles Java .NET XML Database Enterprise