In: Computer Science
Write a C program that prompts the user to enter some information about up to 20 individuals (think of a way to welcome and prompt the user). It must be stored in a structure. Once data is entered, the program output it as shown in sample run below. Your program should include a structure with a tag name of: “information”. It should contain the following data as members: a struct to store employee's name, defined as: struct name fullname e.g. a float to store employee's pay_rate, a float to store employee's hours, a float to store employee's retirement percentage, a struct to store a hire date, defined as: struct date the data type struct name will consist of: char last_name[20]; char first name [15]; char middle initial[1]; and the data type struct date: int yyyy; int mm; int dd; You need to define an array of type: struct information. You can call this array: employees[20] or whatever name you wish. Use a 21% tax rate. The dialog with the user must be as follows:
Whatever Title you want Here
How many employees do you wish to process? 2
Employee #1: Enter first name: Minnie
Enter last name: Mouse
Enter middle initial:
Enter hours worked: 40
Enter pay_rate: 33.50
Enter 401K percentage:.03
Enter hire date (mm/dd/yyyy): 01/02/1993
Employee #2:
Enter first name: Johnny
Enter last name: Carson
Enter middle initial: M Enter hours worked: 30
Enter pay_rate:
50 Enter 401K percentage: .025
Enter hire date (mm/dd/yyyy): 11/10/1942 /
*After the entries a report will come out. You may design it yourself or use this sample as a template. Make sure decimals align!! */
Jim's Employees Payroll Report – 9/22/2020 --------------------------------------------------------
Name Hire Date Hrs Rate Gross Pay Taxes 401K Net Pay Mouse Minnie 01/02/1993 40.00 33.50 1340.00 281.40 40.20 1018.40 Johnny M Carson 11/17/1942 30.00 50.00 1500.00 315.00 37.50 1147.50 Total Payroll 70.00 83.50 2840.00 596.40 77.70 2165.90 Note:
The black text represents the "output" from your program and is shown for clarity only here. Also note that what the user types in is indicated by the blue area above. I also did not show examples of data validation – which you can handle in your own way. Hints/other requirements: • Use safer_gets to read in character array data. • You should use %di (instead of %i) as the format specifier for the date fields because if you enter an integer item starting with the number 0 (zero), C will interpret that number as "octal", which will cause erroneous results. For this reason, I recommend that you use %d in the scanf statement when prompting the user for any int type data. • You do not need to use user-defined functions or string functions in this program (however, feel free to if you'd like to). • You are not permitted to use pointers in this program. (That will be in another assignment!) • You may use some string functions from the notes and chat sessions • You need to perform validation on all the fields entered except the name. • The date can have any label you wish, like birth date, hire date, etc. • For 3 points extra credit you can have the report the current date. Good luck! (Carry On by Crosby, Stills, Nash & Young plays in the background…) (my apologies for any typos found in the description above)
#include <stdio.h>
#include<string.h>
struct information {
struct fullname
{
char last_name[20];
char first_name [15];
char middle_initial[1];
}fn;
float pay_rate,hours,retirement_percentage;
struct date{
int yyyy;
int mm;
int dd;
}dt;
}employee[20]; //array of structure to hold up to 20 records
int valid_date(int d,int m,int y) // this code can be written in main() also
{
//int d,m,y;
int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int valid = 0;
// leap year checking, if ok add 29 days to february
if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
daysinmonth[1]=29;
// days in month checking
if (m<13)
{
if( d <= daysinmonth[m-1] )
valid=1;
}
if (valid==1)
printf("It is a valid date!\n");
else
printf("It's not valid date!");
return valid;
}
int main()
{
int i,n,d,m,y,valid=1;
printf("\nWecome to employee Database\n");
printf("\nHow many employees do you wish to process?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEmployee #%d: Enter first name:",i+1);
scanf("%s",employee[i].fn.first_name); //gets() can be used to input
printf("\nEnter last name:");
scanf("%s",employee[i].fn.last_name);
printf("\n Enter middle initial:");
scanf("%s",employee[i].fn.middle_initial);
printf("\nEnter hours worked:");
scanf("%f",&employee[i].hours);
if(employee[i].hours<0)
{
printf("invalid:hours should be >=0");
valid =0;break;
}
printf("\n Enter pay_rate:");
scanf("%f",&employee[i].pay_rate);
if(employee[i].pay_rate<=0)
{
printf("invalid:pay_rate should be >0");
valid =0;break;
}
printf("\nEnter 401K percentage:");
scanf("%f",&employee[i].retirement_percentage);
if(!(employee[i]. retirement_percentage >0 && employee[i]. retirement_percentage<1 ))
{
printf("invalid: retirement_percentage should be in between 0 and 1");
valid =0;break;
}
printf("\nEnter hire date (mm/dd/yyyy):");
scanf("%d/%d/%d",&m,&d,&y);
if(valid_date(d,m,y)==1)
{
employee[i].dt.mm=m;
employee[i].dt.dd=d;
employee[i].dt.yyyy=y;
}
else{ valid=0;break;}
}
if(valid==1)
{
printf("\nName\t Hire_Date\t Hrs\t Rate\t Gross_Pay\t Taxes\t 401K_per\t Net_Pay\n");
for(i=0;i<n;i++)
{
float gross_pay=employee[i].pay_rate *employee[i].hours;
float taxes=(gross_pay * 21)/100;
float per=gross_pay * employee[i].retirement_percentage;
float net_pay=gross_pay-taxes-per;
printf("%s %s %s\t",employee[i].fn.first_name,employee[i].fn.middle_initial,employee[i].fn.last_name);
if(employee[i].dt.mm<10)
{ printf("0%d/", employee[i].dt.mm);}
else { printf("%d/", employee[i].dt.mm);}
if(employee[i].dt.dd<10)
{ printf("0%d/", employee[i].dt.dd);}
else { printf("%d/", employee[i].dt.dd);}
printf("%d\t", employee[i].dt.yyyy);
printf("%.2f\t%.2f\t%.2f\t%.2f\t",employee[i].hours,employee[i].pay_rate,gross_pay,taxes);
printf("%.2f\t%.2f\n",per,net_pay);
}
}
return 0;
}