Take program III and add structs to it, store all employee data in an array of y
ID: 3539623 • Letter: T
Question
Take program III and add structs to it, store all employee data in an array of your structures. Add a menu that will allow us to update/print employee data. All program behavior from previous assignments would still apply.
Create a structure for an employee. Load the structure with your load method, taking the struct as a parameter. Pass the struct to your print method and adjust your other methods to make use of the struct.
For the menu, we need an option to update an existing record, print a single record, and print all records, and lastly an exit the program option. The menu will be displayed after we finished loading all of the data.
Use appropriate methods.
Example program flow. it should follow this
Display load prompts.
User enters data for 3 employees.
Display menu.
User chooses to print a single employee.
Ask which employee to print (array index)
Print employee data at that index.
Display menu.
User chooses print all employees.
Print all employees in a summary view.
Display menu.
User chooses to edit an employee.
Ask which employee to edit (index)
Display all the employee fields and ask which field to edit.
User chooses name.
Load name and update employee record.
Display menu.
User chooses exit.
Program ends
================================================
this is what i did so far my menu is incompleate menu need to do all of that
if press 1 print sint employee and ask which employee to print
if press 2 print all employees
if press 3 edit the employee and ask which one to edit and this should edit the array
if press 4 it should go to system("pause"); and after that program exit
Display menu:
Choose the option:
1) Print single employee
2) To print all the employess
3) Edit an employee
4) Exit the program
================================================
heres my program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct userdata{
char name[20];
float rate;
float worked;
float basepay;
float overtimePay;
float grosspay;
float earned;
float taxespaid;
float paid;
int userintputformenu;
};
int input(struct userdata *userinput)
{
printf("Enter name: ");
scanf_s("%s", &userinput->name, 20);
if(strcmp(userinput->name, "-1") == 0)
{return -1;}
printf("Enter hourly rate: ");
scanf_s("%f", &userinput->rate);
if (userinput->rate == -1)
{return -1;}
printf("Enter hours worked: ");
scanf_s("%f", &userinput->worked);
printf(" ");
if (userinput->worked == -1)
{return -1;}
}
void calculation(struct userdata *userinput)
{
userinput->overtimePay = 0;
if (userinput->worked > 40)
{
userinput->basepay = userinput->rate *40;
userinput->overtimePay = (userinput->worked - 40) * (userinput->rate * 1.5f);
}
else{
userinput->basepay = userinput->rate * userinput->worked;
}
userinput->grosspay = userinput->basepay + userinput->overtimePay;
userinput->earned = userinput->basepay + userinput->overtimePay;
userinput->taxespaid = userinput->earned * 0.2f;
userinput->paid = userinput->earned - userinput->taxespaid;
}
void load(struct userdata *userinput){
printf("Display menu: ");
printf("Enter data for 5 employees : ");
input(&userinput[0]);
input(&userinput[1]);
input(&userinput[2]);
input(&userinput[3]);
input(&userinput[4]);
}
void menu(struct userdata *userinput){
int userintputformenu;
load(userinput);
printf("Display menu: ");
printf("Choose the option: ");
printf("1) Print single employee ");
printf("2) To print all the employess ");
printf("3) Edit an employee ");
printf("4) Exit the program ");
scanf_s("%d", &userintputformenu);
}
int main()
{
struct userdata userinput[5];
int i;
menu(userinput);
for(i=0;i<5;++i)
{
if(strcmp(userinput[i].name, "-1") == 0){
break;
}else if(userinput[i].rate ==-1 || userinput[i].worked == -1){
break;
}else{
calc(&userinput[i]);
printf(" Pay to: %s ", userinput[i].name);//prints user
printf("Hours worked: %.1f ", userinput[i].worked);//prints user's hours worked
printf("Hourly rate: $ %.2f ", userinput[i].rate);//prints user's hourlyRate
printf("Gross pay: $ %.2f ", userinput[i].grosspay);
printf("Base pay: $ %.2f ", userinput[i].basepay);
printf("Overtime pay: $ %.2f ", userinput[i].overtimePay);
printf("Taxes paid: $ %.2f ", userinput[i].taxespaid);//prints how much user paid taxes
printf("Net pay: $ %.2f ", userinput[i].paid);//prints user's paid check
}
}
system("pause");
}
=====================================================
this is how my output looks like now
Display menu:
Enter data for 5 employees :
Enter name: kavit
Enter hourly rate: 2
Enter hours worked: 50
Enter name:
john
Enter hourly rate: 9
Enter hours worked: 40
Enter name: kevin
Enter hourly rate: 5
Enter hours worked: 30
Enter name: glenn
Enter hourly rate: 8
Enter hours worked: 30
Enter name: john
Enter hourly rate: 12
Enter hours worked: 45
Display menu:
Choose the option:
1) Print single employee
2) To print all the employess
3) Edit an employee
4) Exit the program
5
Pay to: kavit
Hours worked: 50.0
Hourly rate: $ 2.00
Gross pay: $ 110.00
Base pay: $ 80.00
Overtime pay: $ 30.00
Taxes paid: $ 22.00
Net pay: $ 88.00
Pay to: john
Hours worked: 40.0
Hourly rate: $ 9.00
Gross pay: $ 360.00
Base pay: $ 360.00
Overtime pay: $ 0.00
Taxes paid: $ 72.00
Net pay: $ 288.00
Pay to: kevin
Hours worked: 30.0
Hourly rate: $ 5.00
Gross pay: $ 150.00
Base pay: $ 150.00
Overtime pay: $ 0.00
Taxes paid: $ 30.00
Net pay: $ 120.00
Pay to: glenn
Hours worked: 30.0
Hourly rate: $ 8.00
Gross pay: $ 240.00
Base pay: $ 240.00
Overtime pay: $ 0.00
Taxes paid: $ 48.00
Net pay: $ 192.00
Pay to: john
Hours worked: 45.0
Hourly rate: $ 12.00
Gross pay: $ 570.00
Base pay: $ 480.00
Overtime pay: $ 90.00
Taxes paid: $ 114.00
Net pay: $ 456.00
Press any key to continue . . .
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct userdata{
char name[20];
float rate;
float worked;
float basepay;
float overtimePay;
float grosspay;
float earned;
float taxespaid;
float paid;
int userintputformenu;
};
int input(struct userdata *userinput)
{
printf("Enter name: ");
scanf_s("%s", &userinput->name, 20);
if(strcmp(userinput->name, "-1") == 0)
{return -1;}
printf("Enter hourly rate: ");
scanf_s("%f", &userinput->rate);
if (userinput->rate == -1)
{return -1;}
printf("Enter hours worked: ");
scanf_s("%f", &userinput->worked);
printf(" ");
if (userinput->worked == -1)
{return -1;}
}
void calc(struct userdata *userinput)
{
userinput->overtimePay = 0;
if (userinput->worked > 40)
{
userinput->basepay = userinput->rate *40;
userinput->overtimePay = (userinput->worked - 40) * (userinput->rate * 1.5f);
}
else{
userinput->basepay = userinput->rate * userinput->worked;
}
userinput->grosspay = userinput->basepay + userinput->overtimePay;
userinput->earned = userinput->basepay + userinput->overtimePay;
userinput->taxespaid = userinput->earned * 0.2f;
userinput->paid = userinput->earned - userinput->taxespaid;
}
void load(struct userdata *userinput,int x){
int i;
printf("Display menu: ");
printf("Enter data for %d employees : ",x);
for(i=0;i<x;i++)
input(&userinput[i]);
}
int main()
{
int x=3;
int userintputformenu;
int i;
int z;
struct userdata userinput[20];
printf("Enter total no. of employees");
scanf_s("%d", &x);
load(userinput,x);
printf("Display menu: ");
printf("Choose the option: ");
printf("1) Print single employee ");
printf("2) To print all the employess ");
printf("3) Edit an employee ");
printf("4) Exit the program ");
scanf_s("%d", &userintputformenu);
while(userintputformenu!=4)
{
switch(userintputformenu)
{
case 1:
printf("Enter employee number");
scanf_s("%d", &i);
calc(&userinput[i]);
printf(" Pay to: %s ", userinput[i].name);//prints user
printf("Hours worked: %.1f ", userinput[i].worked);//prints user's hours worked
printf("Hourly rate: $ %.2f ", userinput[i].rate);//prints user's hourlyRate
printf("Gross pay: $ %.2f ", userinput[i].grosspay);
printf("Base pay: $ %.2f ", userinput[i].basepay);
printf("Overtime pay: $ %.2f ", userinput[i].overtimePay);
printf("Taxes paid: $ %.2f ", userinput[i].taxespaid);//prints how much user paid taxes
printf("Net pay: $ %.2f ", userinput[i].paid);//prints user's paid check
break;
case 2:
for(i=0;i<x;++i)
{
calc(&userinput[i]);
printf(" Pay to: %s ", userinput[i].name);//prints user
printf("Hours worked: %.1f ", userinput[i].worked);//prints user's hours worked
printf("Hourly rate: $ %.2f ", userinput[i].rate);//prints user's hourlyRate
printf("Gross pay: $ %.2f ", userinput[i].grosspay);
printf("Base pay: $ %.2f ", userinput[i].basepay);
printf("Overtime pay: $ %.2f ", userinput[i].overtimePay);
printf("Taxes paid: $ %.2f ", userinput[i].taxespaid);//prints how much user paid taxes
printf("Net pay: $ %.2f ", userinput[i].paid);//prints user's paid check
}
break;
case 3:
printf("Enter employee number to edit");
scanf_s("%d", &i);
printf("1)name 2)rate 3)worked");
scanf_s("%d", &z);
switch(z)
{
case 1:
printf("Enter name: ");
scanf_s("%s", &userinput[i].name, 20);
break;
case 2:
printf("Enter hourly rate: ");
scanf_s("%f", &userinput[i].rate);
break;
case 3:
printf("Enter hours worked: ");
scanf_s("%f", &userinput[i].worked);
printf(" ");
break;
}
break;
case 4:
break;
}
printf("Display menu: ");
printf("Choose the option: ");
printf("1) Print single employee ");
printf("2) To print all the employess ");
printf("3) Edit an employee ");
printf("4) Exit the program ");
scanf_s("%d", &userintputformenu);
}
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.