Thread: structure with array

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    Question structure with array

    It don't get the float value?
    i don't know what happens?
    Code:
    # include<stdio.h>
    struct pay
    {
       int id;
       char name[20];
       float rate;
       };
    
        void main()
         {
    	  struct pay emp[5];
    	  int i;
    clrscr();
    for(i=0;i<5;++i)
    {
    scanf("%d",&emp[i].id);
    scanf("%s",&emp[i].name);
    scanf("%f",&emp[i].rate);
    }
    for(i=0;i<5;++i)
    {
    printf("\n%d\t",emp[i].id);
    printf("%s\t",emp[i].name);
    printf("%f",emp[i].rate);
    }
    printf("\n");
    }
    AbHHinaay

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    Works okay for me, what's your input like?

  3. #3
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    it return the error

    scanf don't get float linking
    abnormal program termination.
    AbHHinaay

  4. #4
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    I've heard about this problem, if it's what it sounds like, either get a newer compiler or check the manual for a workaround. I think the workaround is something like this
    Code:
    # include<stdio.h>
    struct pay
    {
        int id;
        char name[20];
        float rate;
    };
    
    linkfloat(float *fp){
        /*DON"T CALL!*/
        float f = *fp;
        linkfloat(&f);
    }
    
    void main()
    {
        struct pay emp[5];
        int i;
        clrscr();
        for(i=0;i<5;++i)
        {
            scanf("%d",&emp[i].id);
            scanf("%s",&emp[i].name);
            scanf("%f",&emp[i].rate);
        }
        for(i=0;i<5;++i)
        {
            printf("\n%d\t",emp[i].id);
            printf("%s\t",emp[i].name);
            printf("%f",emp[i].rate);
        }
        printf("\n");
    }

  5. #5
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    [B]It's works!!!!
    THANX Pioneer THANX !!!
    AbHHinaay

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void main()
    No, it's int main(void) and return 0; at the end.

    >>scanf("%s", ....
    I'd advise against using scanf() to read strings. it does no length validation. Lookup fgets() instead, but be warned that scanf() will leave input in the buffer that will cause problems with fgets(), so you need to be a bit careful if you use both in the same program.

    >>clrscr();
    This is a non-standard function, and will probably come in a non-standard header file (conio.h maybe) which you don't appear to have included.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM