(structure+array+pointer)to make a simple database

This is a discussion on (structure+array+pointer)to make a simple database within the C Programming forums, part of the General Programming Boards category; Hi,guys!!now i wish to make a program that can intput data into the array of structure......firstly, i can input the ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    (structure+array+pointer)to make a simple database

    Hi,guys!!now i wish to make a program that can intput data into the array of structure......firstly, i can input the data and then print it at once,but when i come back to the choice function,it seems can't print more a record ,hope some people can help me !!
    I have attached the file named by a2obackup.c

    /***The program is under below***/





    #include<stdio.h>
    #define SIZE 10 /*The number of record is 10*/

    struct name{
    char first[30];
    char last[30];
    };

    struct datehire{
    char day[2];
    char month[2];
    char year[4];
    };
    struct employee{

    struct name allname;
    char id[20];
    char salary[20];
    struct datehire alldate;
    };
    typedef struct employee Employee;

    void choice();
    void addrecord(Employee *);
    void changerecord(Employee *);
    void loada(Employee *);
    void loadall(Employee *);



    main(void){

    choice();

    return 0;
    }

    void choice(){ /*choice function that used to led the user to choice the function*/

    Employee employee_list[SIZE];
    int choice;

    int rubbish;

    do{
    printf(" <--------Welcome to employee database system--------> \n\n");
    printf(" Please choose the number of function that you want->\n\n");
    printf(" <1>Add Record/Records\n");
    printf(" <2>Change a Record\n");
    printf(" <3>Load A record to screen\n");
    printf(" <4>Load ALL record to screen\n");
    printf(" <5>Exit\n");
    printf("\n\n\nPlease enter:->");
    scanf("%d",&choice);

    switch(choice){
    case 1:
    addrecord(employee_list);
    break;

    case 2:
    changerecord(employee_list);
    break;
    case 3:
    loada(employee_list);
    break;
    case 4:
    loadall(employee_list);
    break;
    case 5:
    printf("Exited,thanks you to using our system!\n");
    printf("press any key and then press the enter to exit\n");
    scanf("%d",&rubbish);
    break;
    default:
    printf("Error input!Please input the number between 1-5");
    scanf("%d",&rubbish);
    }

    }while (choice!=5);

    }
    void addrecord(Employee *employee_list){/*add a record*/
    int count=0;
    int display=0;



    printf("Enter employee name(First name)->");
    scanf("%s",employee_list[count].allname.first);

    printf("Enter employee name(Last name)->");
    scanf("%s",employee_list[count].allname.last);

    printf("Enter the the employee id->");
    scanf("%s",employee_list[count].id);

    printf("Enter the employee salary->");
    scanf("%s",employee_list[count].salary);

    printf("Please enter the date of hire->");
    scanf("%s%s%s",&employee_list[count].alldate.day,&employee_list[count].alldate.month,&employee_list[count].alldate.year);

    printf("\n");


    printf("The record [%d] employee first name -> %s \n",display+1,employee_list[count].allname.first);
    printf("The record [%d] employee last name -> %s\n",display+1,employee_list[count].allname.last);
    printf("The record [%d] employee id ->%s \n",display+1,employee_list[count].id);
    printf("The record [%d] employee salary -> %s\n",display+1,employee_list[count].salary);
    printf("The record [%d] employee hire of date-> %s/%s/%s\n",display+1,employee_list[count].alldate.day,employee_list[count].alldate.month,employee_list[count].alldate.year);
    printf("\n");

    count=count+1;
    display=display+1;


    }



    void changerecord(Employee *employee_list){ /*change the existed record*/
    int choice1;


    char check;
    printf("What record do you want to change?Enter the number of record please->");
    scanf("%d",&choice1);

    printf("You want to change record %d\n",choice1);
    --choice1;
    printf("Enter employee name(First name) to change the orignal record:\n");
    scanf("%s",employee_list[choice1].allname.first);

    printf("Enter employee name(Last name) to change the orignal:\n");
    scanf("%s",employee_list[choice1].allname.last);

    printf("Enter the the employee id to change the orignal\n");
    scanf("%s",employee_list[choice1].id);

    printf("Enter the employee salary to change the orignal\n");
    scanf("%s",employee_list[choice1].salary);

    printf("Please enter the date of hire\n");
    scanf("%s%s%s",&employee_list[choice1].alldate.day,&employee_list[choice1].alldate.month,&employee_list[choice1].alldate.year);


    printf("The orignal have changed,Do you want to check it?Y or N\n");
    scanf("%s",&check);
    if((check=='Y')||(check=='y')){
    printf("The record [%d] first name-> %s \n",choice1+1,employee_list[choice1].allname.first);
    printf("The record [%d] last name-> %s\n",choice1+1,employee_list[choice1].allname.last);
    printf("The record [%d] id-> %s\n",choice1+1,employee_list[choice1].id);
    printf("The record [%d] salary-> %s\n",choice1+1,employee_list[choice1].salary);
    printf("The record [%d] date of hire-> %s/%s/%s\n",choice1+1,employee_list[choice1].alldate.day,employee_list[choice1].alldate.month,employee_list[choice1].alldate.year);
    printf("End of change record");
    printf("\n\n\n\n\n\n");

    }




    }
    void loada(Employee *employee_list)
    {
    int choice1;
    printf("What record do you want to load to screen?Enter the number of record please->");
    scanf("%d",&choice1);
    choice1-1;

    printf("%s ",employee_list[choice1].allname.first);
    printf("%s\n",employee_list[choice1].allname.last);
    printf("%s\n",employee_list[choice1].id);
    printf("%s\n",employee_list[choice1].salary);
    printf("%s/%s/%s\n",employee_list[choice1].alldate.day,employee_list[choice1].alldate.month,employee_list[choice1].alldate.year);

    }
    void loadall(Employee *employee_list){

    }
    Attached Files Attached Files

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    Well when I compile it, I get these messages

    D:\code>gcc -Wall zzz.c
    zzz.c:32: warning: return type defaults to `int'
    zzz.c: In function `addrecord':
    zzz.c:103: warning: char format, different type arg (arg 2)
    zzz.c:103: warning: char format, different type arg (arg 3)
    zzz.c:103: warning: char format, different type arg (arg 4)
    zzz.c: In function `changerecord':
    zzz.c:146: warning: char format, different type arg (arg 2)
    zzz.c:146: warning: char format, different type arg (arg 3)
    zzz.c:146: warning: char format, different type arg (arg 4)
    zzz.c: In function `loada':
    zzz.c:171: warning: statement with no effect

    The last one is interesting
    choice1-1;

    Perhaps you meant
    choice1 = choice - 1;

    The other problems are where you have say
    char ch;
    scanf( "%s", &ch );

    Which should minimally be
    char ch[2];
    scanf( "%s", ch );

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Salem,
    OT question:
    How do you redirect output from gcc? I can't seem to get that to work. Every other program redirects fine...

    example:
    C:\>foo.exe >read.txt
    The world is waiting. I must leave you now.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    gcc writes errors on stderr

    This works in a win2k console, it may also work in an NT console
    gcc -Wall zzz.c 2> errr

    This will cause stderr to be written to the errr file

    For win9x, things are a little trickier
    In your djgpp/bin directory, there's a program called redir

    Use it like so
    redir -e errr gcc -Wall zzz.c

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    http://www.cygwin.com/ml/cygwin/1999-02/msg00603.html

    I have mingw instead of DJGPP, I got it working though. Thanks Salem.
    The world is waiting. I must leave you now.

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    For the Linux Console (gcc):
    gcc blah.c -o blah 2> /path/to/file/here

    note u are sending it through stream 2 or STDERR so it will only put the errors that the program reports into /path/to/file/here

    it may also do this for windows, but windows does not have an STDERR stream by default.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Simple question about array of chars
    By Chewy in forum C Programming
    Replies: 9
    Last Post: 04-12-2004, 06:13 AM
  3. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 06:05 PM
  4. how to make a poiner array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:12 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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