HELP! I have any trouble with function in C!

Discussion in 'Programming' started by alfeds, Dec 18, 2011.

  1. #1
    I got a task of programming like this : I have 2 files, datafile.txt and payfile.txt. datafile.txt contains the data in the form Employee ID, Pay Rate, and Hours Worked.

    The rule is :
    if hours worked<=40, the wages=hours_worked*pay_rate.
    else the wages=(40*pay rate)+(hours worked-40)*1.5* pay rate.

    data input must be taken from datafile.txt. from left to right, they are employee id, pay rate ($), and hours worked, respectively :
    1017032001#50#15
    1017032002#70.3#23
    1017032003#100#8
    1017032004#20#24
    1017032005#43#10
    1017032006#250#4
    1017032007#22.5#50
    1017032008#10.5#24
    1017032009#30#68
    1017032010#22#34

    The output is in form
    Employee ID : ---
    Pay Rate : ---
    Hours Worked : ---
    Wages : ---

    on the screen and payfile.txt.


    I have done with the coding, but my program's not running properly. I made it in linked-list form, and the program has a menu. The problem is, whatever choice I had entered, the program stopped.

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>

    void input_data ();
    void print_data ();

    struct employee
    {
    char employee_id[80];
    float pay_rate;
    int hours_worked;
    float wages;
    struct employee *next;
    };

    struct employee *head=NULL;
    struct employee *current=NULL;
    struct employee *tail=NULL;

    /*==========End of variable declaration==========*/

    /*==========Main menu==========*/
    int main (void)
    {
    char test='\0';

    printf("==========MENU==========\n");
    printf("1. Input data\n");
    printf("2. Print out all of data\n");
    printf("3. Exit\n");
    printf("========================\n\n");
    printf("Please enter your choice (1-3) : ");
    scanf(" %c", &test);
    system("cls");

    switch (test)
    {
    case 1 : input_data; break;
    case 2 : print_data; break;
    case 3 : return 0; break;
    }

    return 0;
    }

    void input_data ()
    {
    char ch;
    char temp_pay_rate[50], temp_hours_worked[50] ;
    int count, space=0;
    FILE *pfile=NULL;
    char *filename="C:\\Dev-Cpp\\datafile.txt";

    /*==========End of variable declaration==========*/

    pfile=fopen(filename, "r");

    if (pfile!=NULL)
    {
    do
    {
    current=(struct employee*)malloc(sizeof(struct employee));

    count=0;
    ch=fgetc(pfile);
    do
    {
    if (ch=='#')
    {
    space++;
    goto end;
    }
    switch (space)
    {
    case 0 : current->employee_id[count]=ch; break;
    case 1 : temp_pay_rate[count]=ch; break;
    case 2 : temp_hours_worked[count]=ch; break;
    }
    count++;
    end:
    ch=fgetc(pfile);
    } while (ch!='\n');

    current->pay_rate=atof(temp_pay_rate);
    current->hours_worked=atoi(temp_hours_worked);
    if(current->hours_worked<=40) current->wages=current->hours_worked*current->pay_rate;
    else current->wages=(40*current->pay_rate)+(current->hours_worked-40)*1.5*current->pay_rate;
    current->next=NULL;

    if (head==NULL)
    {
    head=tail=current;
    tail->next=NULL;
    }
    else
    {
    tail->next=current;
    tail=current;
    }

    } while (ch!=EOF);
    }
    else printf("Empty file. No data.\n");

    fclose(pfile);
    printf("Press any key to continue...");
    getch(); main();
    return;
    }

    void print_data()
    {
    FILE *pfile=NULL;
    char *filename="C:\\Dev-Cpp\\payfile.txt";
    char answer;

    pfile=fopen(filename, "w");
    printf("\tWAGES OF EMPLOYEE");
    fputs("\tWAGES OF EMPLOYEE", pfile);

    current=head;
    while(current!=NULL)
    {
    printf("\nEmployee ID : %s\n", current->employee_id);
    fputs("\nEmployee ID : ", pfile); fputs(current->employee_id, pfile);
    printf("Pay rate : %.2f\n", current->pay_rate);
    fputs("\nPay rate : ", pfile); fprintf(pfile, "%.2f", current->pay_rate);
    printf("Hours worked : %i\n", current->hours_worked);
    fputs("\nHours worked : ", pfile); fprintf(pfile, "%i", current->hours_worked);
    printf("Wages : %.2f\n", current->wages);
    fputs("\nWages : ", pfile); fprintf(pfile, "%.2f", current->wages);
    fputs("\n", pfile);

    current=current->next;
    }

    fclose(pfile);
    printf("\nDo you want to open payfile.txt?");
    scanf(" %c", &answer);
    if (tolower(answer)=='y') system("start C:\\Dev-Cpp\\payfile.txt");
    else main();

    return;
    }


    Can you help me solve this problem?
    Please forgive my bad English.
     
    alfeds, Dec 18, 2011 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,830
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #2
    looks like an assignment, not a real world problem.
     
    sarahk, Dec 18, 2011 IP