Hi im trouble with this program question Program is done in C MUST USE FGETS INS
ID: 3802905 • Letter: H
Question
Hi im trouble with this program question
Program is done in C
MUST USE FGETS INSTEAD OF SCANF
Multipurpose Payroll System Write a program that calculates pay for either an hourly paid worker or a salaried worker. Hourly paid workers are paid their hourly pay rate times the number of hours worked. Salaried workers are paid their regular salary plus any bonus they may have earned. The program should declare two structures for the following data:
Hourly Paid:
Name
Gender
HoursWorked
HourlyRate
Salaried:
Name
Age
Salary
Bonus
The program should also declare a union with two members. Each member should be a structure variable: one for the hourly paid worker and another for the salaried worker. The program should ask the user whether he or she is calculating the pay for an hourly paid worker or a salaried worker. Regardless of which the user selects, the appropriate member of the union will be used to store the data that will be used to calculate the pay.
input Validation:
a) Do not accept negative numbers.
b) Do not accept values greater than 80 for HoursWorked.
c) Ensure to securely process numeric values
Explanation / Answer
//include basic headerfiles
#include<stdio>
#include<conio>
#include<string>
#include<stdlib>
//Creating structure for hourly paid workers
struct hpaid
{
char hname[100];
char hgender[100];
int hworked;
int hrate;
};
//Creating structure for salaried worker
struct spaid
{
char sname[100];
int sage;
int salary;
int sbonus;
};
//Creating union
union work
{
hpaid hour;
spaid sal;
};
//Main Function
int main()
{
clrscr() //clears output screen everytime u run the programme
FILE *file;
file=fopen("pay.txt",w); // Creating file pay.txt to store data and opening it in read and write mode
int choice;
char ch;
union work pay; //object for union
do
{
clrscr();
printf" Press 1. to calculate pay for an hourly paid worker.";
printf(" Press 2. to calculate pay for a salaried worker";
scanf("%d",&choice);
if(choice==1)
{
printf(" Enter the name of the worker:");
gets(pay.hour.hname);
fputs(file," %s",pay.hour.hname); //storing the name of the worker into the file pay.txt
printf(" Enter the gender of the worker:");
gets(pay.hour.hgender);
fputs(file,", %s",pay.hour.hgender); //storing gender in the same line seperated by a comma and a tab
label1:
{
printf(" Enter number of hours worked by the worker:");
scanf("%d",&pay.hour.hworked);
if(pay.hour.hworked<0)
{
printf(" Cannot enter negative values.");
goto label1;
}
else
{
if (pay.hour.hworked>80)
{
printf(" Cannot be greater than 80");
goto label1;
}
else
{
fputs(file," %d",pay.hour.hworked);
}
}
}
label2:
{
printf(" Enter rate per hour worked:");
scanf("%d",&pay.hour.hrate);
if(pay.hour.hrate<0)
{
printf(" Cannot enter negetive value");
goto label2;
}
else
fputs(file," %s",pay.hour.hrate);
}
}
else
{
if(choice==2)
{
printf(" Enter the name of the worker:");
gets(pay.sal.sname);
fputs(file,"%s",pay.sal.sname); //storing the name of the worker into the file pay.txt
printf(" Enter age of the worker:");
gets(pay.sal.sage);
fputs(file,", %s",pay.sal.sage); //storing gender in the same line seperated by a comma and a tab
label3:
{
printf(" Enter salary of the worker:");
scanf("%d",&pay.sal.salary);
if(pay.sal.salary<0)
{
printf(" Cannot enter negative values.");
goto label3;
}
else
{
fputs(file," %d",pay.sal.salary);
}
}
label4:
{
printf(" Enter bonus:");
scanf("%d",&pay.sal.sbonus);
if(pay.sal.sbonu<0)
{
printf(" Cannot enter negetive values");
goto label4;
}
else
fputs(file,(", %d",pay.sal.sbonus);
}
}
else
printf(" You entered wrong choice");
}
printf(" Do yo want to Continue (Y/N):");
scanf("%c",&ch);
}
while(ch=="Y");
fclose (file);
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.