Thread: Struc Array

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    34

    Struc Array

    SOmeone helps me on this.. i got problem it said
    read_data identifier not found....
    search_data not found..

    here my code

    Code:
    #include <stdio.h>
    #include <conio.h> 
    #include <stdlib.h>
    struct stud
    {
    int id;
    char name[15];
    double gpa;
    char fakulti;
    };
    void main()
    {
        struct stud a[50];
        int n,reply,sid,option,i;    
        do
        {
            printf("\n 1. Insert records");
            printf("\n 2. Search record");
            printf("\n 3. exit");
            printf("\n\n Select proper option :");
            scanf("%d",&option);
            switch(option)
            {
            case 1 : // create
                printf("\nEnter Amount of data : ");
                scanf("%d",&n);
                read_data(a,n);
                break;        
            case 2 : // search
                printf("\nEnter ID : ");
                scanf("%d",&sid);
                reply = search_data(a,n,sid);
                if( reply == -1)
                    printf("\n Not found");
                else
                {
                    printf("\nID\tName\tGpa\tFakulti");
                    printf("\n%d\t%s\t%.2f",a[reply].id,a[reply].name,a[reply].gpa,a[reply].fakulti);
                }
                break;
            case 3 : exit(0);
            } //swith
        }while(1);
    } // main
    
    int read_data(struct stud a[], int n)
    {
        int i;
        printf("\nEnter %d records\n",n);
        for(i=0;i < n;i++)
        {
            printf("\nID : \t");
            scanf("%d",&a[i].id);
            printf("Name : \t");
            flushall();
            scanf("%s",a[i].name);
            printf("Marks : \t");
            scanf("%.2f",&a[i].gpa);
            printf("Fakulti : \t\n");
            flushall();
            scanf("%s",a[i].fakulti);
    
        } // for
         return 0;
    } // read data
    
    int search_data( struct stud a[], int n , int sid)
    {
        int i;
        for(i=0;i < n;i++)
        {
            if( a[i].id == sid)
            return(i);
        }//for
        return(-1);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Try putting the prototype of these functions before main.
    Code:
    int read_data(struct stud a[], int n);
    int search_data( struct stud a[], int n , int sid);
    And never declare main as returning void, main has to return something.
    Use int main instead.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    printf("\nID\tName\tGpa\tFakulti");
    printf("\n%d\t%s\t%.2f",a[reply].id,a[reply].name,a[reply].gpa,a[reply].fakulti);
    You are missing the format specifier for "fakulti".

    Code:
    scanf("%s",a[i].fakulti);
    "fakulti" is a char not a string. You have to use %c (and take care about the dangling newline character)

    Bye, Andreas
    Last edited by AndiPersti; 12-11-2012 at 03:04 PM. Reason: added link

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    i already changed as u said.. but when i run the program and insert input and once reach fakulti user input, it said struct_test.exe is stopped working.. sometime i having this kind of porblem.. btw i using visual

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Quote Originally Posted by AndiPersti View Post
    Code:
    printf("\nID\tName\tGpa\tFakulti");
    printf("\n%d\t%s\t%.2f",a[reply].id,a[reply].name,a[reply].gpa,a[reply].fakulti);
    You are missing the format specifier for "fakulti".

    Code:
    scanf("%s",a[i].fakulti);
    "fakulti" is a char not a string. You have to use %c (and take care about the dangling newline character)

    Bye, Andreas
    i already changed it.. but i still got problem.. once i enter input for fakulti, the prgram stopped working..

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Show your new program.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Code:
    #include <stdio.h>
    #include <conio.h> 
    #include <stdlib.h>
    struct stud
    {
    int id;
    char name[15];
    double gpa;
    char fakulti;
    };
    int read_data(struct stud a[], int n);
    int search_data( struct stud a[], int n , int sid);
    int main()
    {
        struct stud a[50];
        int n,reply,sid,option;    
        do
        {
            printf("\n 1. Insert records");
            printf("\n 2. Search record");
            printf("\n 3. exit");
            printf("\n\n Select proper option :");
            scanf("%d",&option);
            switch(option)
            {
            case 1 : // create
                printf("\nEnter Amount of data : ");
                scanf("%d",&n);
                read_data(a,n);
                break;        
            case 2 : // search
                printf("\nEnter ID : ");
                scanf("%d",&sid);
                reply = search_data(a,n,sid);
                if( reply == -1)
                    printf("\n Not found");
                else
                {
                    printf("\nID\tName\tGpa\tFakulti");
                    printf("\n%d\t%s\t%.2f\t%c",a[reply].id,a[reply].name,a[reply].gpa,a[reply].fakulti);
                }
                break;
            case 3 : exit(0);
            } //swith
        }while(1);
    } // main
    
    int read_data(struct stud a[], int n)
    {
        int i;
        printf("\nEnter %d records\n",n);
        for(i=0;i < n;i++)
        {
            printf("\nID : \t");
            scanf("%d",&a[i].id);
            printf("Name : \t");
            flushall();
            scanf("%s",a[i].name);
            printf("Gpa : \t");
            scanf("%.2f",&a[i].gpa);
            printf("Fakulti : ");
            flushall();
            scanf("%c",a[i].fakulti);
    
        } // for
         return 0;
    } // read data
    
    int search_data( struct stud a[], int n , int sid)
    {
        int i;
        for(i=0;i < n;i++)
        {
            if( a[i].id == sid)
            return(i);
        }//for
        return(-1);
    }

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    The problem is with this line:

    Code:
    scanf("%c",a[i].fakulti);
    Change it to:

    Code:
    scanf("%c", &(a[i].fakulti));
    It's a char, not a pointer, so the address must be passed to scanf.

    Also, I believe the dot in the specifier in this line is wrong:
    Code:
    scanf("%.2f",&a[i].gpa);
    You should be getting a warning. What did you want to do here?

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Code:
    scanf("%.2f",&a[i].gpa);
    i didnt get any error from this specifier.. i got another problem.. about the return value.. or maybe smth else.. not so sure once finishing enter the input its should display the option..
    Struc Array-screenshot_1-png

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by koplersky View Post
    Change it to:

    Code:
    scanf("%c", &(a[i].fakulti));
    You don't need those extra brackets in there. It can be just:
    Code:
    scanf("%c", &a[i].fakulti);
    I've also heard many times on these boards that it may also be useful to put a space at the start of the string to swallow any unwanted whitespace.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Thanx guys i already solved this problem.. thank for the help

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Quote Originally Posted by iMalc View Post
    You don't need those extra brackets in there.
    Yes, it's just a bad habit of mine. But you are right, thanks for the fix.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get in6_addr struc
    By saravanant in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-17-2009, 10:02 AM
  2. unable to free 1 of the struc member
    By kakarato in forum C Programming
    Replies: 8
    Last Post: 12-20-2008, 04:53 PM
  3. Struc and Switch case
    By dlwlsdn in forum C Programming
    Replies: 33
    Last Post: 11-23-2008, 07:00 PM
  4. I have some problems reading struc from file
    By samc2004 in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 02:47 PM
  5. struc link List
    By Lien in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2001, 08:31 AM