DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5

Thread: dates in C++

  1. #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++.

    please help!!!

  2. #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;
    //read month number
    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++.
    >
    > please help!!!


  3. #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++.
    >
    >please help!!!



    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. #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;
    >//read month number
    >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++.
    >>
    >> please help!!!



  5. #5
    venkat Guest

    Re: c++

    hi
    Send me some web links to learn c++



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links