Thread: Do I use struct correctly?

  1. #1
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18

    Question Do I use struct correctly?

    I am trying to write a program which will determine the user's zodiac sign when she or he inputs his or her birth date. I am trying to use struct for the first time on this program. But, it fails to work. I am not really sure what causes this since I am still new on struct. My compiler tells me something like error: expected expression before .. token. What does that mean? is there something wrong on how I use struct?

    I read somewhere that you're not allowed to assign a member of struct a value. Is that true?

    Here is my code.

    Code:
    #include <stdio.h>
    struct zodiac
    {
        char name[20];
        int date1, date2, mon1, mon2;
    };
    main()
    {
        int d, m, y, a;
        struct zodiac bintang[12]; 
        bintang[0]={"Capricorn",  22, 12, 19, 1};
        bintang[1]={"Aquarius", 20, 1, 18, 2};
        bintang[2]={"Pisces", 19, 1, 20, 3};
        bintang[3]={"Aries", 21, 3, 19, 4};
        bintang[4]={"Taurus", 20, 4, 20, 5};
        bintang[5]={"Gemini", 21, 5, 20, 6};
        bintang[6]={"Cancer", 21, 6, 22, 7};
        bintang[7]={"Leo", 23, 7, 22, 8};
        bintang[8]={"Virgo", 23, 8, 22, 9};
        bintang[9]={"Libra", 23, 9, 22, 10};
        bintang[10]={"Scorpio", 23, 10, 21, 11};
        bintang[11]={"Sagitarius", 22, 11, 21, 12};
        printf("Input your birth date in following format (dd mm yyyy): ");
        scanf("%d %d %d", &d, &m, &y);
        for(a=0; a<12; a++)
        {
            if((d>=bintang[a].date1 && m==bintang[a].mon1)||(d<=bintang[a].date2 && m==bintang[a].mon2))
            printf("You are a %s.", bintang[a].name);
        }
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Your problem is not really a struct issue, but an issue to how to initialize an array statically.
    Let's use this simple example.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int a[3];
            a[0] = {0};
            a[1] = {1};
            a[2] = {2};
    
            return 0;
    }
    This will produce similar errors as the ones you have
    Code:
    px.c: In function ‘main’:
    px.c:6:9: error: expected expression before ‘{’ token
    px.c:7:9: error: expected expression before ‘{’ token
    px.c:8:9: error: expected expression before ‘{’ token
    You can do that without the brackets, like
    Code:
    a[0] = 0;
    ...
    Well, since we are playing with arrays, for loop comes in handy.

    Or, if you really want to use brackets, you should do that
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int a[] = {0, 1, 2};
    
            return 0;
    }
    Notice that I did not declare the size of the array. Compiler finds it automatically, by counting how many entries I gave for the array. If I had declared the size of the array, equal to three, then it would be the same for us. Notice that I use
    Code:
    int main(void)
    {
        ...
        return 0;
    }
    instead of
    Code:
    main()
    {
         ....
    }
    They are pretty close, but we use to write it by the first way. It is more correct. Probably your teacher hasn't get you into the concept of functions, thus leaving main() alone, which is ok - imho- for beginners. I am sure he will explain it, when the time comes. Your teachers knows better from anybody else, when its time to introduce his students to a concept, since he has a plan for the course he is lecturing.

    If you feel uncomofmortable with structs, see this simple example, which however, has functions in the code. In other words, if you haven't been taught functions yet, leave the link for future reference.

    So, the code should be
    Code:
    #include <stdio.h>
    struct zodiac
    {
        char name[20];
        int date1, date2, mon1, mon2;
    };
    
    int main(void)
    {
        int d, m, y, a;
        struct zodiac bintang[] = {
            {"Capricorn",  22, 12, 19, 1},
            {"Aquarius", 20, 1, 18, 2},
            {"Pisces", 19, 1, 20, 3},
            {"Aries", 21, 3, 19, 4},
            {"Taurus", 20, 4, 20, 5},
            {"Gemini", 21, 5, 20, 6},
            {"Cancer", 21, 6, 22, 7},
            {"Leo", 23, 7, 22, 8},
            {"Virgo", 23, 8, 22, 9},
            {"Libra", 23, 9, 22, 10},
            {"Scorpio", 23, 10, 21, 11},
            {"Sagitarius", 22, 11, 21, 12}
        };
        printf("Input your birth date in following format (dd mm yyyy): ");
        scanf("%d %d %d", &d, &m, &y);
        for(a=0; a<12; a++)
        {
            if((d>=bintang[a].date1 && m==bintang[a].mon1)||(d<=bintang[a].date2 && m==bintang[a].mon2))
            printf("You are a %s.", bintang[a].name);
        }
    }
        return 0;   /* You forgot to return 0 from your main(). Don't do that, main()'s feelings get hurt when you do that. :) */
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    I see "Ophiuchus" didn't make it into your array.

    Are you using the real dates, or the popularly incorrect ones?

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,750
    This is not relevant at all: Queen Live Aid Full Video HQ - YouTube

  5. #5
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Thank you very much, std10093! From your explanation, I have developed my previous code into a better one. And this is what I got:

    Code:
    // created by M0513027
    #include <stdio.h>
    struct zodiac
    {
        char zodiacName[10];
        int date1, mon1, date2, mon2;
    };
    struct month
    {
        char monthName[30];
        int date;
    };
    main()
    {
        int d, m, y, a, c=0;
        struct zodiac bintang[]=
        {
            {"Aquarius", 20, 1, 18, 2},
            {"Pisces", 19, 2, 20, 3},
            {"Aries", 21, 3, 19, 4},
            {"Taurus", 20, 4, 20, 5},
            {"Gemini", 21, 5, 20, 6},
            {"Cancer", 21, 6, 22, 7},
            {"Leo", 23, 7, 22, 8},
            {"Virgo", 23, 8, 22, 9},
            {"Libra", 23, 9, 22, 10},
            {"Scorpio", 23, 10, 21, 11},
            {"Sagitarius", 22, 11, 21, 12},
            {"Capricorn",  22, 12, 19, 1}
        };
        struct month bulan[]=
        {
            {"January", 31},
            {"February", 28},
            {"March", 31},
            {"April", 30},
            {"May", 31},
            {"June", 30},
            {"July", 31},
            {"August", 31},
            {"September", 30},
            {"October", 31},
            {"November", 30},
            {"December", 31},
            {"January"}
        };
        puts("Input your birth date in the following format (dd mm yyyy): ");
        scanf("%d %d %d", &d, &m, &y);
        if(y%4==0 && y%100!=0 || y%400==0)
            bulan[1].date=bulan[1].date+1;
        for(a=0; a<12; a++)
        {
            if(d>=1 && d<=bulan[a].date && d>=bintang[a].date1 && m==bintang[a].mon1)
            {
                printf("\nYou were born on %d %s %d.\n", d, bulan[a].monthName, y);
                printf("You are a/an %s.\n", bintang[a].zodiacName);
                c++;
            }
            else if(d>=1 && d<=bulan[a].date && d<=bintang[a].date2 && m==bintang[a].mon2)
            {
                printf("\nYou were born on %d %s %d.\n", d, bulan[a+1].monthName, y);
                printf("You are a/an %s.\n", bintang[a].zodiacName);
                c++;
            }
        }
        if(c==0)
            puts("\nSorry, your input is invalid.");
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Why is "January" in index 12 of bulan? From your for loop which accesses this table, it looks like you will never access this last member:

    Code:
    for(a=0; a<12; a++)

  7. #7
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    But, if I delete that part, the program will not run well. I mean if I input 5 1 2010. The output will be "You were born on 5 Aquarius 2010."... That's what I have tried. I have to put "January" in index 12 again because as you can see in line 61, it's bulan[a+1]. I hope you get what I mean.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char** within struct not printing correctly.
    By aj74 in forum C Programming
    Replies: 1
    Last Post: 11-12-2011, 11:11 AM
  2. How to correctly use if/else if?????? Please Help
    By ambellina in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2011, 05:30 PM
  3. How To Correctly Output of Struct Objects
    By NuNn in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 08:32 AM
  4. Am I doing these correctly?
    By newbcore in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 01:39 AM
  5. Is this used correctly?
    By MuzicMedia in forum C++ Programming
    Replies: 7
    Last Post: 03-08-2008, 10:07 PM

Tags for this Thread