Write a Program to perform Weekly Department Salary Report program

FratBro23
Category:
Programming
Price: $5 USD

Question description

Whenever a user inputs anything besides a "single" word, the program crashes. I tried using fgets and sscanf, but then it doesnt prompt the user to input anything at all.. Help~ Below is my code. Another question is, how to align the output in a straight line? Everytime the digit is different, the alignment screws up.

My code

/* A Program to perform Weekly Department Salary Report program*/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main(void)

{

FILE *f;

    f = fopen("c:\\class\\pay.txt","w");

    if (f == NULL)

{

    printf("Error opening file!\n");

    exit(1);

}

char dname[21],ename[100][21],sname[100][21],name[100][21],uantity[5];

char artist[21];

int credit;

int i =0,j,len,bal;

float stotal[100],total[100],finaltot, hwage[100],otime[100],owage[100],gross[100],trwork=0,towork=0,trwage=0,towage=0,totwage=0,percent=0;

char pay;

    char exit ='y';

char output[500] = "";

 

 

printf("Mountain Pacific Corporation\n");

printf("Department Salary Program\n\n");

 printf("Please enter the name of the department:  ");

    scanf("%s",&dname);

do

{

/*

    printf("Enter employee first name#%d:  ",i+1);

    scanf("%s",&ename[i]);

    printf("Enter employee second name#%d:  ",i+1);

    scanf("%s",&sname[i]);

     

strcat(ename[i]," ");

    strcat(ename[i],sname[i]);

   

    len = strlen(ename[i]);

     

if (len < 21) 

for (j = 1; j < (21 - len); j++)

strcat(ename[i],"");

   

*//// The above code still doesn't work.

printf("Enter employee #%d: ",i+1);

scanf("%s",ename[i]);

//scanf("%[^ \n]s",ename[i]);  

//scanf("%i[^\n]", ename[i]);

    printf("Enter the hourly wage of %s :",ename[i]);

    scanf("%f",&hwage[i]);

    printf("Enter total number of hours: ");

    scanf("%f",&total[i]);

    printf("\n\nThank you. Process another employee? ");

    scanf("%s",&pay);

    i = i + 1;

   

}

while (pay == 'y' || pay == 'Y');

         

    fprintf(f,"\n\nMountain Pacific Corporation");

fprintf(f,"\n\nDepartment Salary Program");

fprintf(f,"\n\nDepartment: %s",&dname);

fprintf(f,"\n\nEmployee                Reg Hrs        Overtime Hrs       Gross\n");

fprintf(f,"-----------------------------------------------------------------\n");

 for (j=0;j<i;j++)

{

     

    if (total[j] <= 40)

{

     stotal[j] = total[j];

     otime[j] = 0;

}

     else

{

     stotal[j] = 40;

     otime[j] = total[j]-40;

}

     owage[j] = hwage[j] * 1.5;

     gross[j]= hwage[j] * stotal[j] + otime[j] * hwage[j]*1.5;

     fprintf(f,"%s  %10.1f ($%.2f)    %4.1f ($%.2f)     $%4.2f\n",ename[j],stotal[j],hwage[j],otime[j],owage[j],gross[j]);    

}

fprintf(f,"-----------------------------------------------------------------\n");

for (j=0;j<i;j++)

{

    trwork = trwork + stotal[j];

    trwage = trwage + hwage[j]*stotal[j];

    towork = towork + otime[j];

    towage = towage + owage[j] * otime[j];

    totwage = totwage + gross[j];

}

percent = (towork / (towork + trwork)) * 100;

fprintf(f,"\nTotal Regular Hours Worked: %.1f",trwork);

fprintf(f,"\nTotal Overtime Hours Worked: %.1f",towork);

fprintf(f,"\nPercentage of Total Hours That Are Overtime: %.1f %",percent);

fprintf(f,"\n\n\nTotal Regular Wages: $%.2f",trwage);

fprintf(f,"\nTotal Overtime Wages: $%.2f",towage);

fprintf(f,"\n\n\nTotal Wages This Period: $%.2f",totwage);

printf("\n");

 getch();

 return 0;

}


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: UC Berkeley
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1819 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors