PDA

View Full Version : C programming Pay Rate


deadlock75
2004-10-19, 12:09 PM
I need some help I am not sure how to program this. I am to post to be a employer that is interested in determining the pay an individual is to recieve. The number of hours and pay rate should be read in. If the hours are over the 40 the rate is "time and a half" for those hours over. Then I have to report the total earned amount, earned for regular hours and the amount earned for overtime hours. But this is what I have so far. The program is able to calculate the standard time but the overtime is incorrect. I want the user to be able to enter the hour and the rate. But I do not how to do that I only know how to assign it.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <math.h>

#define MIN(x,y) ((x)<(y) ? (x) : (y))
#define MAX(x,y) ((x)>(y) ? (x) : (y))

#define MAX_EMPLOYEES 10
#define WAGE 6.75
#define STD_HOURS 40

#define MAX_BUFFER_SIZE 80

typedef struct {
float std;
float ot;
float total;
} WAGES;

void sighandler(int i)
{
printf("\n** Caught signal %d. Exiting...\n", i);
exit(EXIT_FAILURE);
}

int readline(char* buffer)
{
int count = 0;
char ch;

memset(buffer, 0x00, MAX_BUFFER_SIZE);
ch = getchar();

while ((ch != '\n') && (count < MAX_BUFFER_SIZE)) {
buffer[count++] = ch;
ch = getchar();
}

buffer[count] = 0x00; /* null-terminate */
return strlen(buffer);
}

int main(int argc, char* argv[])
{
int hours=0, ot=0, maxemp=0, numemp=0;
float premium=0, totalpay=0, maxpay=0;
char buf[MAX_BUFFER_SIZE];

WAGES emp[MAX_EMPLOYEES];

signal(SIGINT, sighandler);

printf("\nEnter the overtime premium percentage: ");
readline(buf);
premium = MAX(atof(buf), 1);

for (register int i=0; i < MAX_EMPLOYEES; ) {
printf("\nEnter the hours worked by employee %d: ", i+1);
readline(buf);

hours = atoi(buf);
ot = MAX(0, (hours - STD_HOURS));
hours = MIN(hours, STD_HOURS);

printf("\nStandard hours: %d\n", hours);
printf("Overtime hours: %d\n", ot);
printf("\nIs this correct? (y/n): ");
readline(buf);

if (toupper(buf[0]) == 'Y')
numemp++;
else
continue;

emp[i].std = (float)(hours * WAGE);
emp[i].ot = (float)((WAGE * premium) * ot);
emp[i].total = (float)(emp[i].std + emp[i].ot);

printf("\nWAGES:\n");
printf("Standard: %.2f\n", emp[i].std);
printf("Overtime: %.2f\n", emp[i].ot);
printf("Total: %.2f\n", emp[i].total);

totalpay += emp[i].total;

if (emp[i].total > maxpay) {
maxpay = emp[i].total;
maxemp = i+1;
}

printf("\nEnter wages for another employee? (y/n): ");
readline(buf);

if (toupper(buf[0]) == 'Y') {
i++;
continue;
}
break;
}

printf("\nTotal pay for %d employees: %.2f\n", numemp, totalpay);
printf("The max earner was employee %d with %.2f\n", maxemp, maxpay);
printf("\nWAGE LIST:\n");

for (i=0; i < numemp; i++)
printf("Employee %d: %.2f\n", i+1, emp[i].total);

return EXIT_SUCCESS;
}

WetWired
2004-10-19, 12:25 PM
Overtime hours or pay isn't right? Have you tried stepping through it in a debugger to check where the values deviate from your expectations?

deadlock75
2004-10-19, 01:40 PM
Overtime hours or pay isn't right? Have you tried stepping through it in a debugger to check where the values deviate from your expectations?
I don't know how to debug it through to find out why the overtime is a lot more than regular pay. Even if the overtime hour is 1 it will go more than the standard hour payment.

WetWired
2004-10-19, 03:26 PM
What toolset are you using?

deadlock75
2004-10-19, 05:37 PM
i dunno what is a toolset

WetWired
2004-10-19, 10:12 PM
i dunno what is a toolset
Okay, you're compiling your program somehow, how do you do that? What's the name of the program that you use to compile your program, and who makes it?

deadlock75
2004-10-20, 04:13 PM
Turbo C++ thats the compiler that I am using

WetWired
2004-10-20, 09:56 PM
Turbo C++ thats the compiler that I am using
Okay, so you're using Borland Turbo C++. I've used the IDE before, but a long time ago. Off the top of my head, Ctrl-F9 runs, F7 steps into, and F8 steps over. Some modifier (shift?) with F4 runs the program to the point where the cursor is. IIRC, you can right click a line and choose to add a breakpoint. After compiling (F9 or Alt-F9), try just hitting F8 (otherwise, set a breakpoint on the first line of main, then hit Ctrl-F9). Hitting F8 repeatedly will show you the program flow. What you need to do is to set up watches for all the variables, then step through the code and watch the values of the variables, confirming that they have the values you expect at all times. I don't recall how to set up watches, but if you look through the menus once you've started debugging, it should be easy enough to figure out.