How do I make this code short by for looping?

This is a discussion on How do I make this code short by for looping? within the C Programming forums, part of the General Programming Boards category; This code prints the Months and how many days it has. I want to make my code short, I thought ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    How do I make this code short by for looping?

    This code prints the Months and how many days it has.
    I want to make my code short, I thought that I use some for loop and make the name of the months in array but I cant think how.
    If I make the jan.month to a for loop and make it i.month, i dont get how i will change the month name...
    Can anyone help me a bit?

    Code:
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    struct item 
    {
           char month[80];
           int date;
    }
    
    disp(struct item it)
    {
         printf("%s %d \n", it.month, it.date);
    }
     
    int main()
    {
         struct item jan;struct item feb;struct item mar;
         struct item apr;struct item may;struct item jun;
         struct item jul;struct item aug;struct item sep;
         struct item oct;struct item nov;struct item dec;
         
         strcpy( jan.month, "January");
         strcpy( feb.month, "February");
         strcpy( mar.month, "March");
         strcpy( apr.month, "April");
         strcpy( may.month, "May");
         strcpy( jun.month, "June");
         strcpy( jul.month, "July");
         strcpy( aug.month, "August");
         strcpy( sep.month, "September");
         strcpy( oct.month, "October");
         strcpy( nov.month, "November");
         strcpy( dec.month, "December");
         jan.date=30;
         feb.date=28;
         mar.date=30;
         apr.date=28;
         may.date=30;
         jun.date=28;
         jul.date=30;
         aug.date=28;
         sep.date=30;
         oct.date=28;
         nov.date=30;
         dec.date=28;
         disp(jan);
         disp(feb);
         disp(mar);
         disp(apr);
         disp(may);
         disp(jun);
         disp(jul);
         disp(aug);
         disp(sep);
         disp(oct);
         disp(nov);
         disp(dec);
         system("pause");
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    I would use a typical count++ and set it to = 0 set 1 = jan, 2 = feb etc

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    How about an array?
    Code:
    struct item months[] = {
        { "January", 31 },
        // etc
    };
    Then later on
    Code:
    disp( months[1] );  // february
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Good suggestions so far... here's one more...
    Code:
    char mNames[13][] = {"Error","January","February","March","April","May","June","July","August","September","October","November","December"}
    
    int mDays[13] = {-1,31,28,31,30,31,30,31,31,31,31,30,31}
    
    // get month number from someplace...
    
    printf("%s has %d days",mName[Month],mDays[Month]);
    With input from the user... you should be able to do this in about 20 lines.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    thanks for all the replies.

    here's my finished code, i think it is fine now, isnt it?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct item 
           {
            char month[80];
            int date;
            }calendar[12]={{"January" ,30},{ "February" ,28},{"March",30},{"April",28},{"May",30},{"June",28},{"July",30},{"August",28},{"September",30},{"October",28},{"November",30},{"December",28}};;
    
    void disp(struct item it)
    {
         printf("%s %d \n", it.month, it.date);
    }
     
    main()
    {
         int i;
         for(i=0;i<12;i++)
         {
          disp( calendar[i] );
          }
         system("pause");
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well you should probably line-wrap your array initialisation line - it's way too long.

    main() should explicitly return int, and have a return 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    That will work fine and also,why do you think of using initialization list?
    There can be other options...
    In the end.... good luck.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by Salem View Post
    Well you should probably line-wrap your array initialisation line - it's way too long.

    main() should explicitly return int, and have a return 0;
    i should put return int and return 0 in the main()?
    i dont really know much about those return function much, what does return int do?
    return 0 ends the program, am i right?

    thanks for the reply

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's primarily to make your compiler (and posters here) happy. Every function has a return type. main should return an int. Thus, any time you have a function that comes to an end, that has a non-void return type, you should return an appropriate value. That leads us to:
    Code:
    int main( /* optional arguments */ )
    {
        ...do stuff...
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 01:56 AM
  2. How to Make This code more efficient
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 04-12-2010, 01:29 AM
  3. Does managed code make people stupid?
    By zacs7 in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 07-28-2008, 04:10 PM
  4. Replies: 7
    Last Post: 07-30-2005, 11:28 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21