Printf function pass by reference - c

I'm trying to make a simple program with C that gets name, hourly rate and hours for input and calculates gross pay, tax, net pay etc, but I got stuck as I was making a function that prints out results.
I created two loops: one loop that generates overtime, gross pay, tax and net pay. Another loop that prints out the generated data. First one works fine. It generates data just fine, but as I run the program, it simply ignores the second loop and just finish the program.
It is just a simple void function that has no return value, and yet I'm stuck. I used pass by reference. If you know what's wrong, please let me know.
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#define size 5
char name[size][20];
float rate[size];
float hours[size];
int i = 0;
void input();
float getbasepay(float *rt, float *hr);
float getovtime(float *rt, float *hr);
float getgross(float *base, float *ovt);
float gettax(float *gross);
float getnetpay(float *gross, float *tax);
void printout(char name, float rate, float hours, float base, float ovt, float gross, float tax, float net);
void input()
{
for (i = 0; i < size; i++)
{
puts("\ntype name: (type -1 to quit) \n");
scanf_s("%19s", &name[i], 20);
if (strcmp(name[i], "-1") == 0) { break; }
puts("\ntype hourly rate: (type -1 to quit) \n");
scanf_s("%f", &rate[i]);
if (rate[i] == -1) { break; }
puts("\ntype hours worked: (type -1 to quit) \n");
scanf_s("%f", &hours[i]);
if (hours[i] == -1) { break; }
}
return ;
}
float getbasepay(float *rt, float *hr)
{ float base;
float baseo;
float excessive = (*hr - 40);
if (*hr <= 40)
{
base = (*rt) * (*hr);
return base;
}
if (*hr >= 40)
{
baseo = (*hr - excessive) * (*rt);
return baseo;
}
}
float getovtime(float *rt,float *hr)
{
float time;
float ovt;
if (*hr >= 40)
{
time = (*hr - 40);
ovt = time * (*rt) *1.5;
return ovt;
}
else
{
return 0;
}
}
float getgross(float *base, float *ovt)
{
float gross = *base + *ovt;
return gross;
}
float gettax(float *gross)
{
float tax = *gross * 0.20;
return tax;
}
float getnetpay(float *gross, float *tax)
{
float net;
net = *gross - *tax;
return net;
}
void printout(char *name, float *rate, float *hours, float *base, float * ovt, float *gross, float *tax, float *net)
{
printf("\nPay to: %s \n", *name);
printf("Hourly rate: %f \n", *rate);
printf("Hours: %f \n", *hours);
printf("Base pay: $%f \n", *base);
printf("Overtime: $%f \n", *ovt);
printf("Gross Pay: $%f \n", *gross);
printf("Tax: $%f \n", *tax);
printf("Net Pay: $%f \n", *net);
}
int main(void)
{
int o, z;
float overtime[size] = { 0,0,0,0,0, };
float basepay[size] = { 0,0,0,0,0, };
float tax[size] = { 0,0,0,0,0, };
float grosspay[size] = { 0,0,0,0,0, };
float netpay[size] = { 0,0,0,0,0, };
float sum;
input();
for (o = 0; o < i; o++)
{
basepay[o] = getbasepay(&rate[o], &hours[o]);
overtime[o] = getovtime(&rate[o], &hours[o]);
grosspay[o] = getgross(&basepay[o], &overtime[o]);
tax[o] = gettax(&grosspay[o]);
netpay[o] = getnetpay(&grosspay[o], &tax[o]);
}
for (z = 0; z > i; z++)
{
printout(&name[z], &rate[z], &hours[z], &basepay[z], &overtime[z], &grosspay[z], &tax[z], &netpay[z]);
}
}

Your code doesn't compile:
gcc -o main main.c
Answers:
error: conflicting types for ‘printout’
And indeed, the signature in the declaration is different than the one you
wrote in the implementation (for example char *name instead of char name etc.)

Related

Need help solving a issue involving 'function definition is not allowed here' in c language

Tried running this in C but it keeps saying "function definition is not allowed here" when referring to '{'. This is the full code that im trying to run, its called a "greedy algorithm" so im still learning how to make it. The program is supposed to give you your exact change back then let you know how many coins of which type will be needed.
#include <cs50.h>
#include <math.h>
int main()
{
int len = 4;
double cost;
double pay;
{
//promt user for cost
printf("Enter cost: ");
scanf("%lf", &cost);
//prompt user for pay amount
printf("Enter pay: ");
scanf("%lf", &pay);
double change = pay - cost;
if(pay > cost)
printf("Trasncation complete\nHeres your change: %f\n", change);
else if(pay == cost)
printf("Transaction complete.\n");
else if(pay < cost)
printf("insufficient funds\nStill need: %f", cost - pay);
//greddy algorithm
}
void greedy(double change)
{
int i=o;
double number;
double coin[len] = [0.01, 0.05, 0.10, 0.25];
while(i>len)
{
if(coin[i] <= change)
{
number = change / coin[i];
printf("%f of %f is needed", number, change);
change = change * coin[i];
}
i++
}
}
}
You have to define your greedy() function outside of the main function like so:
#include <cs50.h>
#include <math.h>
int main()
{
int len = 4;
double cost;
double pay;
{
//promt user for cost
printf("Enter cost: ");
scanf("%lf", &cost);
//prompt user for pay amount
printf("Enter pay: ");
scanf("%lf", &pay);
double change = pay - cost;
if(pay > cost)
printf("Trasncation complete\nHeres your change: %f\n", change);
else if(pay == cost)
printf("Transaction complete.\n");
else if(pay < cost)
printf("insufficient funds\nStill need: %f", cost - pay);
}
}
// greddy algorithm
void greedy(double change)
{
int i=o;
double number;
double coin[len] = [0.01, 0.05, 0.10, 0.25];
while(i>len)
{
if(coin[i] <= change)
{
number = change / coin[i];
printf("%f of %f is needed", number, change);
change = change * coin[i];
}
i++
}
}

Program Variance and Standard Deviation Using Pointer Function

So i'm trying to make a standard deviation and variance function and I can't really figure out why it doesn't work. I'm suppose to call variance in case 3 and SD in case 4. everything else works in the program. If you see anything that doesn't look right let me know.
#include <stdio.h>
#include <math.h>
#define Max_Nums 20
void sortNums(float nums[], int size);
float meanValue(float nums[],int size);
float medianValue(float nums[], int size);
void var_stdDev(float nums[],int size,float *var,float *stdDev);
float sqrtf(float);
int main (void)
{
int NumValue = 0;
float array[Max_Nums];
int i=0;
int choice=0;
float avg=0;
float median=0;
printf("How many numbers do you wish to enter (Max of 20): ");
scanf("%d",&NumValue);
while (NumValue<1 || NumValue>Max_Nums)
{
printf("Invalid response. You must enter a value between 1 and 20.\n");
scanf("%d",&NumValue);
}
printf("Enter %d real numbers: ",NumValue);
for (i=0;i<NumValue;i++)
{
scanf("%f", &array[i]);
}
do
{
sortNums(array,NumValue);
printf("-----Menu-----\n\a");
printf("Enter 1 for mean value\n");
printf("Enter 2 for median value\n");
printf("Enter 3 for variance\n");
printf("Enter 4 for standard deviation\n");
printf("Enter 5 to exit the program\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
avg=meanValue(array,NumValue);
printf("The mean is:%.2f\n",avg);
break;
case 2:
median=medianValue(array,NumValue);
printf("The median is:%.2f\n",median);
break;
case 3:
//printf("The variance is:%.2f",variance);
//break;
case 4:
//printf("The standard deviation is:%.2f\n");
//break;
case 5:
printf("Exiting the program\n");
break;
default:
printf("\nInvalid, try again");
break;
}
}while (choice!=5);
return 0;
}
void sortNums(float nums[], int size)
{
int x;
int y;
float z;
for(x=0;x<(size-1);x++)
{
for(y=0;y<size-x-1;y++)
{
if(nums[y]>nums[y+1])
{
z=nums[y];
nums[y]=nums[y+1];
nums[y+1]=z;
}
}
}
}
float meanValue(float nums[],int size)
{
int i;
float avg;
float sum;
for(i=0;i<size;i++)
{
sum+=nums[i];
}
avg = (sum/size);
return avg;
}
float medianValue(float nums[], int size)
{
float EvenMed;
float Med;
void sortNums(float nums[], int size);
if (size%2==0)
{
EvenMed=(nums[size/2]+nums[size/2-1])/2;
return EvenMed;
}
else
{
Med=nums[size/2];
return Med;
}
}
void var_stdDev(float nums[],int size,float *var,float *stdDev)
{
int i;
float sum;
float meanValue(float nums[],int size);
for(i=0;i<size;i++)
{
sum+=pow((nums[i]-meanValue,2);
}
*var=sum/(float)size;
*stdDev=sqrt(*var);
}
This line is wrong:
sum+=pow((nums[i]-meanValue,2);
This is trying to subtract a function pointer from a number, which makes no sense. You need to call the meanValue function to get the mean, and then subtract that.
Also, you didn't initialize sum before adding to it.
void var_stdDev(float nums[],int size,float *var,float *stdDev)
{
int i;
float sum = 0;
float mean = meanValue(nums, size);
for(i=0;i<size;i++)
{
sum+=pow((nums[i]-mean,2);
}
*var=sum/(float)size;
*stdDev=sqrt(*var);
}
There's no need to have a declaration of meanValue inside var_stdDev, the declaration at the top of the file serves that purpose throughout.
In medianValue(), you have a declaration of sortNums(), but you never call it, so the numbers aren't sorted (it seems like you don't understand the difference between a prototype and a call).
float medianValue(float nums[], int size)
{
float EvenMed;
float Med;
sortNums(nums, size);
if (size%2==0)
{
EvenMed=(nums[size/2]+nums[size/2-1])/2;
return EvenMed;
}
else
{
Med=nums[size/2];
return Med;
}
}
As well explained by #Barmar , OP's code has a number of problems:
void var_stdDev(float nums[],int size,float *var,float *stdDev) {
int i;
// sum not initialize
float sum;
// unneeded function declaration
float meanValue(float nums[],int size);
// missing code to find the mean
for(i=0;i<size;i++) {
// improper call to accumulate the average derivation from the mean
sum+=pow((nums[i]-meanValue,2);
}
...
Recommend a new approach to standard deviation calculation
from here
std = sqrt(n*sum_of_squares - sum_of_x*sum_of_x)/n
Suggested improvements:
Use double for intermediate calculation. float is fine to reduce storage and sometimes for speed. Yet statistics often subtract values leading to significant lost of precision. Use double.
Due to rounding, select data sets could result in a tiny negative number - even if mathematically the result should be >= 0.0. So good to check sign before sqrt().
Handle case when size == 0 and do not perform a run-time division by 0.
void var_stdDev2(const float x[], size_t size, float *var, float *stdDev) {
double sumx = 0.0;
double sumxx = 0.0;
double std = 0.0; // Used when size == 0.0 - or set to NaN
if (size > 0) {
for (size_t i = 0; i < size; i++) {
sumx += x[i];
sumxx += 1.0 * x[i] * x[i];
}
double std = sumxx * size - sumx * sumx;
std = std >= 0.0 ? sqrt(std) : 0.0;
std /= size;
}
if (stdDev) *stdDev = (float) std;
if (var) *var = (float) sqrt(std);
}
Minor bits:
Use a const in the signature as function does not modify nums[].
Array are best indexed with size_t rather than int.

C. Check input of a float for a certain number of decimal places

Good morning. The code I have written is made to calculate the amount of change given in a transaction, the change portion only, the paper change is ignored. I would like to do an error check to make sure that the user entered # does not exceed 2 decimal places. This is my code.
#include <stdio.h>
#include <stdlib.h>
void intro();
void instructions();
void getvalues(float *owe, float *paid);
float totalchange(float *owe, float *paid);
void quarters (float *change);
void dimes (float *change);
void nickels (float *change);
void pennies (float *change);
int main()
{
float owe = 0.0, paid = 0.0, change;
int a = 2;
intro();
instructions();
printf("Would you like to continue?\n1: Continue\n0: Exit\n");
scanf("%i", &a);
if (a== 0)
exit(0);
while (a == 1){
getvalues(&owe, &paid);
while (owe > paid)
getvalues(&owe, &paid);
change = totalchange(&owe, &paid);
quarters (&change);
dimes (&change);
nickels (&change);
pennies (&change);
printf("Would you like to make another calculation?\n1: Continue\n0: Exit\n");
scanf("%i", &a);
}
return 0;
}
void intro(){
printf("Program: Homework 1 Part 1 :: Change Calculator\nAuthor: Jason Golightly\nDate:5-13-15\nVersion 1.0\n\n");
}
void instructions(){
printf("This program is designed to calculate the coin\nportion of the change given after a purchase.\n");
printf("When prompted, please enter the purchase amount and the amount paid.\nThe amount paid must exceed the purchase amount.\n");
}
void getvalues(float *owe, float *paid){
printf("Please enter the amounts in a dollars.cents fashion\n\nPurchase amount?\n");
scanf("%f", owe);
printf("\nAmount paid?\n");
scanf("%f", paid);
printf("\n");
if (*owe > *paid)
printf("ERROR. Please enter valid amounts.\n");
if (*owe == *paid)
printf("You have given exact change.\n")
}
float totalchange(float *owe, float *paid){
int a;
a = (*paid - *owe)*100;
a = a % 100;
printf("total change = %i\n",a);
return a;
}
void quarters (float *change){
int q;
q = *change / 25;
printf("Quarters = %i\n", q);
*change = *change - 25*q;
}
void dimes (float *change){
int d;
d = *change / 10;
printf("Dimes = %i\n", d);
*change = *change - 10*d;
}
void nickels (float *change){
int n;
n = *change / 5;
printf("Nickels = %i\n", n);
*change = *change - 5*n;
}
void pennies (float *change){
int p;
p = *change / 1;
printf("Pennies = %i\n\n", p);
*change = *change - 1*p;
}
Also, in case you have not noticed, I am fairly new to programming. If you see anything else I could be doing better, please feel free to point it out.
Thanks, Jason
On simple approach to avoid the in-exact floating point calculation is to read the input as a string, parse it into a int, and display it as *.##
i.e.
char number[11];
scanf("%10s", number);
int actual_number = parse(number); // parse the string here.
This actual number is basically dollar*100 + cents - which is an int.
Now perform all the calculations and display like this:
float f = (float)actual_number/100.f ;
printf("%.2f", f);
In the parse routine, you only consider the first two digits after encountering a .
Here is an example of the parse routine in C.

Output shows all 0's. The gross and OT is not calculating

Output shows all 0's. The gross and OT is not calculating..
#include <stdio.h>
#define STD_HOURS 40.0
#define OT_RATE 1.5
#define SIZE 5
void read_hours(float worked_hours[], long int clockNumber[]);
void calculate_gross(float wage[], float worked_hours[], float gross);
void calculate_gross_ot(float wage[], float worked_hours[]);
void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[], float gross);
int i;
int main()
{
long int clockNumber [SIZE] = {98401, 526488, 765349, 34645, 127615};/* employee ID */
float gross = 0.0; /* gross */
float OT [SIZE] = {}; /* overtime */
float wage [SIZE] = {10.6, 9.75, 10.5, 12.25, 8.35}; /* hourly wage */
float worked_hours [SIZE] = {}; // Hours worked
read_hours(worked_hours, clockNumber);
if (worked_hours[i]>STD_HOURS)
{
calculate_gross_ot(wage, worked_hours);
}
else
{
calculate_gross(wage,worked_hours, gross);
}
printFunction(clockNumber, wage, worked_hours, OT, gross);
return(0);
}
void read_hours(float worked_hours[], long int clockNumber[])
{
for (i=0; i<SIZE; i++)
{
printf("\n Enter Hours for Emlployee ID: %ld\n", clockNumber[i]);
scanf ("%f", &worked_hours[i]);
}
}
void calculate_gross(float wage[], float worked_hours[], float gross)
{
for(i=0; i<SIZE; i++)
gross=(wage[i]*worked_hours[i]);
}
void calculate_gross_ot(float wage[], float worked_hours[])
{
float gross;
float OT[SIZE];
for (i=0; i<SIZE; i++)
{
/* calculating overtime hours */
OT[i]=worked_hours[i]-STD_HOURS;
/* calculating gross pay with overtime */
gross = (STD_HOURS*wage[i]) + (OT[i]*OT_RATE*wage[i]);
//}
}
}
void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[], float gross)
{
/* creating a table for the output */
printf("------------------------------------------------\n");
printf("%7s","Clock#");
printf("%7s","Wages");
printf("%7s","Hours");
printf("%7s","OT");
printf("%7s\n","Gross");
printf("------------------------------------------------\n");
for (i=0; i<SIZE; i++)
{
/* printing the results */
printf("%6ld", clockNumber[i]);
printf("%10.2f",wage[i]);
printf("%6.1f", worked_hours[i]);
printf("%6.1f", OT[i]);
printf("%10.2f",gross);
printf("\n");
}
}
The program is used to calculate gross with and without OT hours. The output is showing all 0's for the gross and OT. Please help to figure out where is the mistake.
In the first case, you are passing gross by value.
In the second case, you are not passing it at all (the function has a local function called gross).
In both cases, whenever the respective function changes gross, this change does not propagate to the caller.
You need to either pass gross by pointer, or return the value from the function using the return statement (and changing the return type appropriately).
Pass OT to your calculate_gross_ot() function from main().
...
calculate_gross_ot(wage, worked_hours, OT);
...
void calculate_gross_ot(float wage[], float worked_hours[], float OT[])

Why am I receiving a segmentation fault in my code and can you explain pass by reference, pass by value?

I am writing a program in C. I am trying to learn the difference between pass by reference and pass by value. I'm not sure if I got those right.
I am getting a segmentation fault / core dump error after executing the do while loop for the second time. What am I missing with the loop?
Here is my main:
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <iostream>
extern void CalculateTaxes(float gross,float defr, float *ft,float *st,float *ssit);
extern float calcGross(float hours, float payrate);
extern void InputEmplData(char *lastname, char *firstname, float *hours, float *payrate, float *defr);
extern void InitAcc(float *totreg, float *totovt, float *totrate, float *totgross, float *totfed, float *totstate, float *totssi, float *totdef, float *totnet);
extern void AddDetail2Acc(float reghrs, float *totreg, float ovthrs, float *totovt, float pr, float *totpayrate, float theGross, float *totgross,float ft, float *totfed, float st, float *totstate, float ssit, float *totssi, float defr, float *totdef, float net, float *totnet);
int main(void)
{
float fedtax,statetax,ssitax;
float theGross;
//int counter=0;
char answer;
char lastname, firstname;
float hours, payrate, defr, reghrs, ovthrs, net, numemps;
float totgross, totpayrate, totreg, totovt, totrate, totfed, totstate, totssi, totdef, totnet;
/*
FILE * reportFile; // 1) declare a FILE * variable
reportFile = fopen("./report.txt","wt");
// 2) open a report file with access mode "write-text"
if (reportFile == NULL)
{
printf(" Report file open failed ...\n");
fflush(stdin);
getchar();
exit(-10); // terminate w/ failure code -10; reqs <stdlib>
}
*/
printf(" Welcome to the Employee Tax Program\n");
printf(" Please enter the number of employees taxes will be calculated:");
scanf (" %f", numemps);
InitAcc(&totreg, &totovt, &totrate, &totgross, &totfed, &totstate, &totssi, &totdef, &totnet);
do
{
InputEmplData(&lastname, &firstname, &hours, &payrate, &defr);
theGross = calcGross(hours, payrate); // call 3.4
CalculateTaxes(theGross,defr,&fedtax,&statetax,&ssitax); // 3.5
net=(theGross-defr)-fedtax-statetax-ssitax;
AddDetail2Acc(reghrs, &totreg, ovthrs, &totovt, payrate, &totpayrate, theGross, &totgross,fedtax, &totfed, statetax, &totstate, ssitax, &totssi, defr, &totdef, net, &totnet);
printf(" Fedtax = %8.2f",fedtax);
//fprintf(reportFile," Fedtax = %8.2f",fedtax);
//fprintf(stdout," Fedtax = %8.2f",fedtax);
printf(" Statetax = %8.2f",statetax);
//fprintf(reportFile," Statetax = %8.2f",statetax);
//fprintf(stdout," Statetax = %8.2f",statetax);
printf(" SSItax = %8.2f\n",ssitax);
//fprintf(reportFile," SSItax = %8.2f\n",ssitax);
//fprintf(stdout," SSItax = %8.2f\n",ssitax);
//printf(" %f %f %f %f %f %f %f %f", totreg, totovt, totrate, totfed, totstate, totssi, totdef, totnet);
printf(" Do you have another employee(YES = 1 / No = 2) ==> ");
fflush(stdin);
scanf (" %c", answer);
answer = getchar();
getchar(); // removes '\n' from response
} while (answer == 'y' || answer == 'y');
//fclose(reportFile); // 4) close the file
printf(" Press any key ... ");
fflush(stdin);
getchar();
return 0;
}
Here are my functions:
#include <stdio.h>
void InputEmplData (char *lastname, char *firstname, float *hours, float *payrate, float *defr);
void InputEmplData (char *lastname, char *firstname, float *hours, float *payrate, float *defr)
{
printf(" Please enter your lastname: ");
scanf (" %s", lastname);
fflush(stdin);
printf(" Please enter your firstname: ");
scanf (" %s", firstname);
fflush(stdin);
printf(" Please enter the hours you have worked including overtime: ");
scanf (" %f", hours);
while (hours < 0)
{
printf(" Please enter a valid number. Must be between 0.");
scanf (" %f", hours);
}
printf(" Please enter your payrate(15.00): ");
scanf (" %f", payrate);
while (payrate <= 0)
{
printf(" Please enter a valid number. Must be above 0.");
scanf (" %f", payrate);
}
printf(" Please enter the amount exempt from taxes: ");
scanf (" %f", defr);
while (defr <= 0)
{
printf(" Please enter a valid number. Must be above 0.");
scanf (" %f", defr);
}
}
CalculateTaxes function:
#include "./taxrates.h" //user defined header for tax rates
void CalculateTaxes(float gross,float defr, float *ft,float *st,float *ssit);
float calcFed(float gross, float defr);
float calcState(float ft);
float calcSSI(float gross,float defr);
void CalculateTaxes(float gross,float defr, float *ft,float *st,float *ssit)
{
*ft = calcFed(gross,defr);
*st = calcState(*ft);
*ssit = calcSSI(gross,defr);
}
float calcFed(float gross, float defr)
{
return (gross-defr)*FEDTAXRATE;
}
float calcState(float ft)
{
return ft * STATETAXRATE;
}
float calcSSI(float gross,float defr)
{
return (gross-defr)*SSITAXRATE;
}
float calcGross(float h, float pr);
float calcGross(float h, float pr)
{
return h <= 40? h * pr : 40 *h + (h-40)*1.5*pr;
}
Function to initialize counters:
void InitAcc(float *totreg, float *totovt, float *totrate, float *totgross, float *totfed, float *totstate, float *totssi, float *totdef, float *totnet);
void InitAcc(float *totreg, float *totovt, float *totrate, float *totgross, float *totfed, float *totstate, float *totssi, float *totdef, float *totnet)
{
*totreg = 0;
*totovt = 0;
*totrate = 0;
*totgross = 0;
*totfed = 0;
*totstate = 0;
*totssi = 0;
*totdef = 0;
*totnet = 0;
}
Function to use counters (unfinished):
#include <stdio.h>
void AddDetail2Acc(float reghrs, float *totreg, float ovthrs, float *totovt,
float pr, float *totpayrate, float theGross, float *totgross,
float ft, float *totfed, float st, float *totstate, float ssit, float *totssi,
float defr, float *totdef, float net, float *totnet);
void AddDetail2Acc(float reghrs, float *totreg, float ovthrs, float *totovt,
float pr, float *totpayrate, float theGross, float *totgross,
float ft, float *totfed, float st, float *totstate, float ssit, float *totssi,
float defr, float *totdef, float net, float *totnet)
{
*totgross+= theGross;
*totreg += reghrs;
*totovt += ovthrs;
*totpayrate += pr;
*totfed += ft;
*totstate += st;
*totssi += ssit;
*totdef += defr;
*totnet += net;
}
Makefile:
calc.obj: calctaxes.cpp taxrates.h
g++ -c calctaxes.cpp -o calc.obj
main2.exe: main2.cpp calc.obj cGross.obj InputEmplData.obj InitAcc.obj
g++ main2.cpp calc.obj cGross.obj InputEmplData.obj InitAcc.obj AddDetail2Acc.obj -o main2.exe
cGross.obj: calcGross.cpp InputEmplData.obj
g++ -c calcGross.cpp -o cGross.obj
InputEmplData.obj: InputEmplData.cpp
g++ -c InputEmplData.cpp -o InputEmplData.obj
InitAcc.obj: InitAcc.cpp InputEmplData.obj
g++ -c InitAcc.cpp InputEmplData.obj -o InitAcc.obj
AddDetail2Acc.obj: AddDetail2Acc.cpp
g++ -c AddDetail2Acc.cpp InitAcc.obj -o AddDetail2Acc.obj
There is really a lot of code here, but here is the first error I see:
//...
extern void InputEmplData(char *lastname, char *firstname, float *hours, float *payrate, float *defr);
//...
char lastname, firstname;
//...
InputEmplData(&lastname, &firstname, &hours, &payrate, &defr);
//...
void InputEmplData (char *lastname, char *firstname, float *hours, float *payrate, float *defr)
{
printf(" Please enter your lastname: ");
scanf (" %s", lastname);
fflush(stdin);
printf(" Please enter your firstname: ");
scanf (" %s", firstname);
fflush(stdin);
Unless you aren't assuming that lastname and firstname lengths is 1, then here is your first error. You are receiving segmentation fault because you are writing a string with lentgh of more then 1 into char.
Possible correction is to define:
char lastname[NAME_LENGTH], firstname[NAME_LENGTH];
/...
InputEmplData(lastname, firstname, &hours, &payrate, &defr);
With this you will achieve desired effect. I used arrays of char for lastname and firstname, but you can allocate memory for lastname and etc dynamically with malloc, but I wouldn't recommend you to do this without clear understanding of what you are doing.
Also, I have just several small advices, not considering segmentation fault:
1.Try to create some structures, that would represent different groups of variables. Then you can write, for example, instead of
void AddDetail2Acc(float reghrs, float *totreg, float ovthrs, float *totovt,
float pr, float *totpayrate, float theGross, float *totgross,
float ft, float *totfed, float st, float *totstate, float ssit, float *totssi,
float defr, float *totdef, float net, float *totnet);
something like this:
void AddDetail2Acc(AccStruct *in_out_sourceAcc, AddDataStruct in_addData);
This is more readable and less error prone.
2.As I understand from variables name you are performing some operations with money values. Try not to use float variables to represent money value. Floating point arithmetic one day can give you a small suprise. Try to use integer types.

Resources