Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there guys and good morning!

I'm quite new to programming and forgive me if I'm quite slow with it. Takes me ages to code alone and this is the only part (finally) I have left to do. The teacher gave us an algorithm for the program so we would have to change that into a workable code for our final program.

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

typedef short BOOLEAN;
typedef char STR15[15 + 1];
typedef char STR10[10 + 1];
typedef struct PayRecord
{
	STR15 LastName;
	STR10 FirstName;
	float hours, payrate, deferred, gross, fedtax, statetax, ssitax, net;
	float reghrs, ovthrs;
	float totreg, totovt, totrate, totgross, totfed, totstate, totssi, totdefr, totnet;
} PayRecord;

#define BLANK	"*							   *\n"
#define BORDER  "************************************************************\n"
#define HEADER  "*				Check No %3d		   *\n"
#define HEADER2 "*   Sabre Corporation					   *\n"
#define HEADER3 "*   15790 West Henness lane				   *\n"
#define HEADER4 "*   New Corio, New Mexico 65790				   *\n"
#define HEADER5 "*   Pay to the Order of					   *\n"
#define HEADER6 "*   %-16s					   *\n"
#define HEADER7 "*   %-54s *\n"
#define HEADER8 "*   Reference: %-16s				   *\n"

void convertNetPay(float net, char *netPayString)
{
	int numHuns, numTens, numOnes;
	char OnesTable[9][8] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
	char TensTable[9][8] = { "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
	char TeensTable[9][11] = { "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
	float cents;

	cents = net - int(net);

	strcpy(netPayString, "The Sum of ");

	numHuns = int(net) / 100;
	if (numHuns > 0)
	{
		strcat(netPayString, OnesTable[numHuns - 1]);
		strcat(netPayString, " Hundred ");
	}

	int remainder = int(net) % 100;
	if ((remainder >= 11) || (remainder <= 19))
	{
		strcat(netPayString, TeensTable[remainder - 11]);
	}
	else
	{
		numTens = int(net) % 100 / 10;
		numOnes = int(net) % 100 % 10;
		if (numTens > 0)
		{
			strcat(netPayString, TensTable[numTens - 1]);
			strcat(netPayString, "-");
		}
		if (numOnes > 0)
		{
			strcat(netPayString, OnesTable[numOnes - 1]);
		}
	}
	strcat(netPayString, " Dollars and ");

	cents = cents + 0.005;
	cents = cents * 100;
	cents = int(cents);

	char ResultString[10 + 1];
	sprintf(ResultString, "%s", int(cents));
	strcat(netPayString, ResultString);
}
//====================

void printCheckHeader(PayRecord EmployeeData[], char *CheckName, char *netPayString, int chkNo, char *refCode, int count)
{
	printf(BORDER);
	printf(HEADER, chkNo);
	printf(BLANK);
	printf(HEADER2);
	printf(HEADER3);
	printf(HEADER4);
	printf(BLANK);
	printf(BLANK);
	printf(HEADER5);
	printf(BLANK);
	printf(HEADER6, CheckName);
	printf(BLANK);
	printf(HEADER7, netPayString);
	printf(BLANK);
	printf(BLANK);
	printf(HEADER8, refCode);
	printf(BLANK);
	printf(BLANK);
	printf(BORDER);
}

int main(void)
{
	PayRecord EmployeeData[MAXEMPLOYEES];
	char FullName[15 + 10 + 1];
	char CheckName[15 + 10 + 1];
	char *netPayString;
	char *refCode;
	int count;
	int chkNo = 100;
	FILE *reportFile; // Read as "reportFile is a pointer to a file"

	reportFile = fopen("./report.txt", "wt");// open the file for "write-mode" access

	if (reportFile == NULL)// Test if file DID NOT open, exit.
	{
		printf(" report file open failed ...\n");
		fflush(stdin);
		exit(-30); //reqs <stdlib.h>
	}
//.....
	for (count = 0; count < MAXEMPLOYEES; count++)
	{
		refCode = generateRefCode(EmployeeData, chkNo, count);
		strcpy(CheckName, EmployeeData[count].FirstName);
		strcat(CheckName, " ");
		strcat(CheckName, EmployeeData[count].LastName);
		printCheckHeader(EmployeeData, CheckName, netPayString, chkNo, refCode, count);
		printCheckStub(EmployeeData, CheckName, chkNo, count);
		chkNo++;
	}


This occurs at printCheckHeader() in int main(). Help please!
Posted
Updated 18-May-14 10:43am
v8
Comments
[no name] 18-May-14 15:40pm    
It would happen because you did not include "stdafx.h".
Vampyrai 18-May-14 16:34pm    
Figured out that I accidentally used a pre-commpiled header when I didn't mean to, initial problem is fixed now.
[no name] 18-May-14 17:25pm    
Okay so are you just going to keep updating your "question" until we all do your homework for you? Shouldn't you be doing some of the work yourself? What is it that you think "uninitialized local variable" means? *Hint* it means exactly what it says. So what have you done to try and fix it?

It's obvious! Yo have declared netPayString as a pointer to char but never pointed it to nowhere...(You did it also with refCode)
It's a very good start to mess up all your memory!
As I see in the other methods you are adding strings (strcat) to netPayString, you probably have to allocate some memory for it first...
 
Share this answer
 
You are using the variable netpaystring as an input, but didn't initialize it to some value. Note that the argument that you pass to printCheckHeader is a pointer, not a string! and that pointer has not been assigned to anything, therefore it points to a random address in memory. Accessing it in any way inside the function will lead to an access violation!

When you want to use strings in C, you first need to allocate a sufficiently large buffer, or define a sufficiently large buffer on the stack. You can then pass a pointer to that buffer to any function that needs to work on it.

That said, when you pass a string buffer to another function, you should also always pass the buffer size: if you don't, the function will not know how large the buffer is, and may accidentally access memory past the end of the buffer! These errors are called buffer overruns, and they are the source of most viruses and malware! If that is not sufficient incentive to always make sure that you check your buffer size, then I don't know what is!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900