Thread: help with array

  1. #1
    Unregistered
    Guest

    Unhappy help with array

    why this program not work??

    -----
    #include <stdio.h>
    struct employee_rec
    {
    int emp_num , i,a,b;
    float salary,count,tax;
    char name[20], dpart[20], text [30];

    for(i=1;i>=1;++i)
    } employee[10];

    main(){
    n=0;
    system("clear");
    menu();
    scanf("%d",&choice);
    switch(choice){

    case 1:add_record(); break;
    case 2:display_record(); break;
    case 3:search_record(); break;
    case 4:delete_record(); break;
    }
    add_record(){

    for(i=1;i>=1;++i){
    printf (" \001\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005\001\n");
    printf (" | BLENZZER (M) SDN BHD |\n");
    printf (" | Human Resource Department |\n");
    printf (" \001\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005\001\n");
    printf (" ********************************************");

    printf("Enter Employee Name : ");
    scanf("%[^\n]", &employee[a].name);
    printf("Enter Employee Number : ");
    scanf("%d\n", &employee[a].emp_num);
    printf("Enter Employee Salary : ");
    scanf("%f\n", &employee[a].salary);
    printf("Enter Employee Department :");
    scanf("%s\n", &employee[a].dpart);
    if (salary>=1200)
    {
    tax=salary*0.1;
    printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~\n");
    printf("Notice:This Employee Need pay Tax!! ");
    printf("\nTotal Tax: %7.2f",tax);
    count=salary-tax;
    printf("\nTotal Salary after Deducted for Tax is: %7.2f",count);
    printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~");

    }
    else
    count=salary;

    }
    display_record(){
    int a;
    for (a=0; a<=10; a++){
    printf (" \001\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005\001\n");
    printf (" | BLENZZER (M) SDN BHD |\n");
    printf (" | Human Resource Department |\n");
    printf (" \001\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005\001\n");
    printf (" ********************************************");
    printf("Employee Name is %s\n", employee[a].name);
    printf("Employee No. is %d\n", employee[a].emp_num);
    printf("Employee Department is %s\n", employee[a].dpart);
    printf("Employee Salary Is %f\n", employee[a].count);
    }

    search_record()
    {
    int no;
    printf("Enter employee Number.\n");
    scanf("%d", &no);
    for (a=0; a<=10; a++){
    if (no==employee[a].emp_num){
    printf (" \001\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005\001\n");
    printf (" | BLENZZER (M) SDN BHD |\n");
    printf (" | Human Resource Department |\n");
    printf (" \001\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005\001\n");
    printf (" ********************************************");
    printf("Employee Name is %s\n", employee[a].name);
    printf("Employee No. is %d\n", employee[a].emp_num);
    printf("Employee Department is %s\n", employee[a].dpart);
    printf("Employee Salary Is %f\n", employee[a].count);
    a=5;
    }
    delete_record()
    {
    int no;
    printf("Enter Employee Number.\n");
    scanf("%d", &no);
    for (a=0; a<=10; a++){
    if (no==employee[a].emp_num){
    printf (" \001\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005\001\n");
    printf (" | BLENZZER (M) SDN BHD |\n");
    printf (" | Human Resource Department |\n");
    printf (" \001\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005-\005\001\n");
    printf (" ********************************************");
    printf("%s", employee[a].name);
    printf("%d", employee[a].emp_num);
    printf("%s", employee[a].dpart);
    printf("%f", employee[a].count);
    for(b=a;b<10;b++){
    employee[b].name=employee[b+1].name;
    employee[b].emp_num=employee[b+1].emp_num;
    employee[b].dpart=employee[b+1].dpart;
    employee[b].salary=employee[b+1].count;}
    else

    printf("Sorry No Such Records!!");
    printf("-Please Check Again-"); }

  2. #2
    When I first compiled this I got 30 errors. so lots of things went wrong.
    here are some pointers:
    when you open a '{' make sure you do it on a new line, so you can easely see where you forgot '}' to close one.
    you always have to declare variables.
    you can't copy strings with string1=string2, you have to use strcpy(string1,string2);
    also don't write your program at once. when you finished a piece (ex: add_record), compile it and see what is wrong



    I've elliminated the errors for you (there still are some warnings)
    I dont know if this is how your program should work.(I just compiled it, didn't run it)
    But compare it with what you wrote

    BTW: you forgot your menu function.

  3. #3
    ooops here is the file

  4. #4
    I've looked at your program but I don't think it runs as you would like it to. change this in my program:

    1)when you ask to user to choose what he wants(after the menu)
    Code:
    choice=getch();
    switch(choice)
    { 
    case '1':
    case '2':  //and so on
    2)the input thing in add_record
    Code:
    printf("Enter Employee Name : ");
    gets(employee[i].name); 
    printf("Enter Employee Number : "); 
    scanf("%d", &employee[i].emp_num); 
    printf("Enter Employee Salary : "); 
    scanf("%f", &employee[i].salary); 
    printf("Enter Employee Department :"); 
    gets(employee[i].dpart);
    also take a look at the for-loops
    I dont think you mean: for(int i=1;i>=1;++i)
    but for(int i=0; i<10;i++)
    an array starts from 0 not 1.
    so you have
    employee[0]
    ...
    employee[9]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM