I wrote a program that calculates the weekly pay for five people for my C programming class, but I'm having trouble with it when I try to compile and debug it. At first it would prompt me for the name, hours worked, and hourly rate, but would output $0 for everything. And now, (I don't know what I did), it won't even execute the program. Instead, I'll just get this error message: An unhandled exception of type 'System.AccessViolationException' occurred in hourlyratearray.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Below is the code for my program. What am I doing wrong?
Thanks in advance!
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
int i;
double hourlyRate[5], hoursWorked[5], taxesOwed[5], overTime[5], netPay[5], grossPay[5], basePay[5];
char name[5];
// gathers info
for (i=0; i<5; i++)
{
printf("Enter name.\n");
scanf("%s", &name[i]);
printf("What is their hourly rate?\n");
scanf("%f", &hourlyRate[i]);
printf("How many hours did they work this week?\n");
scanf("%f", &hoursWorked[i]);
// processes input
if( name[i] == -1 || hourlyRate[i] == -1 || hoursWorked[i] == -1)
{
printf("please reenter a nonnegative number");
break;
}
if(hoursWorked[i] > 40)
{
basePay[i] = hourlyRate[i] * hoursWorked[i];
overTime[i] = (hoursWorked[i] - 40) * 1.5 * hourlyRate[i];
taxesOwed[i] = ((hourlyRate[i]*40) + overTime[i])*.2;
netPay[i] = (hourlyRate[i]*40) + overTime[i] - taxesOwed[i];
printf("%s: \n", name[i]);
printf("Hours worked: %f\n", hoursWorked[i]);
printf("Hourly rate: $%.2f\n", hourlyRate[i]);
printf("Base pay: $%.2f\n", basePay[i]);
printf("Overtime pay: $%.2f\n", overTime[i]);
printf("Taxes paid: $%.2f\n", taxesOwed[i]);
printf("Net pay: $%.2f\n", netPay[i]);
}
else
{
basePay[i] = hourlyRate[i] * hoursWorked[i];
taxesOwed[i] = (hourlyRate[i]*40) * .2;
netPay[i] = (hourlyRate[i]*40) - taxesOwed[i];
printf("%s: \n", name[i]);
printf("Hours worked: %f\n", hoursWorked[i]);
printf("Hourly rate: $%.2f\n", hourlyRate[i]);
printf("Base pay: $%.2f\n", basePay[i]);
printf("Taxes paid: $%.2f\n", taxesOwed[i]);
printf("Net pay: $%.2f\n", netPay[i]);
}
}
return 0;
}