Thread: array problem(pl. helpppppp!)

  1. #1
    Unregistered
    Guest

    Unhappy array problem(pl. helpppppp!)

    Hi there!

    i am newbie and trying to explore vC++ by self teach method.I am stuck on arrays.The query is if user inputs a month number the output should be month name.For eg. If 8 is typed in, the output will be august.I have to use 2D array..I suppose.But cant get any further.....
    Any help is really appreciated..
    thankx

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    To declare an array of null terminated strings use...

    char Months[12][20];

    ...the first index is the month you want, the second is the number of characters in each string, (20 may be to many, it would be in English - so set this appropriately).

    You can either initialise the array when you create it...

    char Months[12][20] = {"January", "February", ... etc.

    ...or individually...

    strcpy(Months[0],"January");

    ... you should be able to do the rest with this start.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    the first one is the number of the month. you don't have to fill in the number 12 (but it is not wrong if you do)because you initialize it imediatly. there isn't a month name longer then 8 chars so 10 is a save value

    Code:
    char months[][10]={"January","February","March","April","May","June","July","August","Septmber","October","November","December"};
    
    printf("The name of the %d month is %s",3,months[3]);

  4. #4
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Since you are just starting, I assume you will be using a hard-coded array?
    Code:
    const string MonthName[] = {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",     // my favorite
        "August",
        "September",
        "October",
        "November",
        "December"
    };
    Now, since arrays are zero based (array of size 12 would start at 0 and end with 11), you would want to take the number input by the user and subtract 1 for the array position of that month. You should also check to insure the value will fall within the bounds of your array.
    Code:
    int Number = 0;
    
        cin >> Number;
    
        if ((Number > 0) && (Number <= 12))
            cout << MonthName[Number - 1] << endl;
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  5. #5
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Sorry, my post followed others obviously. I also misread and thought you said you could not use a 2d array. My bad. Just reference adrianxw or maes for the proper syntax on the array.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    We must all have been typing at the same time! Well, Unregistered, at least one of these should help!!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    I must be the slowest typer.because I wrote the shortest message and came secondly.

    can I ask a favour of you guys:next time wait 10min before you press "submit reply" So I can be the first

  8. #8
    Unregistered
    Guest

    Thumbs up thankx a billion

    thank u, thank u and thank u......everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM