0

This program requires the user to input data, after which it calculates. After calculating all the users it puts it prints it onto a document and screen. After which it gets sorted by name, the data gets outputted to another screen displaying a fake check that includes that is grabbed off a txt doc.

The problem is that after the sort (as seen in the code) the code is suppose to print the data, it stops however getting this:

[IMG]http://img198.imageshack.us/img198/6350/blahvtm.jpg[/IMG]

and I get this:
First-chance exception at 0xcccccccc in Assignmento3o.exe: 0xC0000005: Access violation reading location 0xcccccccc.
First-chance exception at 0xcccccccc in Assignmento3o.exe: 0xC0000005: Access violation reading location 0xcccccccc.
First-chance exception at 0xcccccccc in Assignmento3o.exe: 0xC0000005: Access violation reading location 0xcccccccc.
First-chance exception at 0xcccccccc in Assignmento3o.exe: 0xC0000005: Access violation reading location 0xcccccccc.
First-chance exception at 0xcccccccc in Assignmento3o.exe: 0xC0000005: Access violation reading location 0xcccccccc.
First-chance exception at 0xcccccccc in Assignmento3o.exe: 0xC0000005: Access violation reading location 0xcccccccc.
First-chance exception at 0x7c92a978 in Assignmento3o.exe: 0xC0000005: Access violation writing location 0x00030fdc.


The function containing the problem:

#include <stdio.h>
	#include <stdlib.h>
	#include <string.h>
	#include <iostream>
	#include <fstream>
	#include "myheader.h"
    #include <stdlib.h>
    #include <ctype.h>

#define TRUE 1
#define FALSE 0
typedef short BOOLEAN;

void PrintCheck(EmployeeRecord PrintS[], FILE *company, int numc);
char * generateRefCode(char * lastname,int chkNo);
BOOLEAN isVowel(char aChar);
void NumberToText(char *, char *); 
int ValidateNumber(char *, char *);


void PrintCheck(EmployeeRecord PrintS[], FILE *company, int numc)
 {
	  int i;
	  int checknum=100;
	  char number[100];
	  char s[MAXNAMELENGTH];
	  char only_number[100];// only number no extra characters
      char outputString[2048]; // text holder
      int fred;
	  printf("Block");
	  char cn1[MAXNAMELENGTH], cn2[MAXNAMELENGTH], a1[MAXNAMELENGTH],
		   a2[MAXNAMELENGTH], a3[MAXNAMELENGTH], a4[MAXNAMELENGTH],
	    cs1[MAXNAMELENGTH], cs2[MAXNAMELENGTH],cs3[MAXNAMELENGTH],
		cs4[MAXNAMELENGTH], zip[MAXNAMELENGTH],companyname[MAXNAMELENGTH],
		address[MAXNAMELENGTH],citystate[MAXNAMELENGTH];
	 
     fred=fscanf(company,"%s%s%s%s%s%s%s%s%s%s%s",
		  cn1, cn2, a1, a2, a3, a4, cs1, cs2, cs3, cs4, zip);
	 printf("Block %d\n", fred); // It stops here
     strcpy(companyname, cn1);
     strcat(companyname, " ");
     strcat(companyname, cn2);
	 printf("Block");
     strcpy(address, a1);
     strcat(address, " ");
     strcat(address, a2);
     strcat(address, " ");
     strcat(address, a3);
     strcat(address, " ");
     strcat(address, a4);
	 printf("Block");
     strcpy(citystate, cs1);
     strcat(citystate, " ");
     strcat(citystate, cs2);
     strcat(citystate, " ");
     strcat(citystate, cs3);
     strcat(citystate, " ");
     strcat(citystate, cs4);
	 printf("Block");

     for (i=0; i< numc; i++)
	 {
		 sprintf(number,"%.4g",(PrintS[i].netpay));
		 if(strlen(number) > 66 || strlen(number) == 0)
			printf("Invalid length of numbers!\n");
		else 
		{
			if(ValidateNumber(number, only_number)) 
			{
				NumberToText(only_number, outputString);
				printf("Number in Text : %s\n",outputString);
			}
			else
				printf("\n\nInvalid Number !!!\n");
		}
	 printf("********************************************************\n");
	 printf("* 				        	       Check No. %d         *\n",checknum);
	 printf("*                                                      *\n");
	 printf("*    %s                                                *\n",companyname);
	 printf("*    %s                                                *\n",address);
	 printf("*    %s , %s                                           *\n",citystate,zip);
	 printf("*                                                      *\n");
	 printf("*                                                      *\n");
	 printf("*    Pay to the Order of:                              *\n");
	 printf("*    %s , %s                                           *\n",(PrintS[i].firstname),(PrintS[i].lastname));
	 printf("*    The Sum of %s                                     *\n",outputString);
	 printf("*                                                      *\n");
	 printf("*                                                      *\n");
	 printf("*    Reference: %s                                     *\n",generateRefCode((PrintS[i].lastname),checknum));
	 printf("*                                                      *\n");
	 printf("*                                                      *\n");
	 printf("********************************************************\n");
	 printf("* 				                   Check No. %d         *\n",checknum);
	 printf("*                                                      *\n");
	 printf("*      %s,%s            %f               %f            *\n",(PrintS[i].lastname),(PrintS[i].firstname),
		                                                                          (PrintS[i].reghrs),(PrintS[i].federaltax));
     printf("*      %s               %f               %f            *\n",address,(PrintS[i].ovthrs),(PrintS[i].statetax));
	 printf("*      %s               %f               %f            *\n",citystate,(PrintS[i].gross),(PrintS[i].ssitax));
	 printf("*      %s               %f               %f            *\n",zip,(PrintS[i].netpay),(PrintS[i].taxtotal));
	 printf("*                                                      *\n");
	 printf("********************************************************\n");
	 checknum=checknum+1;
	 }
	 fclose(company);
  }

char * generateRefCode(char * lastname,int chkNo)
{
	static char tempStr[30+1];
	int i,j = 1;

	tempStr[0] = lastname[0];
	for (i=1; i < strlen(lastname);i++)
	  if (!isVowel(lastname[i])) tempStr[j++] = lastname[i];
	tempStr[j] = 0; // add string terminator
	return tempStr;
}

BOOLEAN isVowel(char aChar)
{
	switch (toupper(aChar)) //<ctype.h>
	{
	  case 'A':
	  case 'E':
	  case 'I':
	  case 'O':
	  case 'U' : return TRUE;break;
	  default: return FALSE;
	}
}

void NumberToText(char *only_number, char *outputString)
{
    char one_array[10][11] = {
        " ","one ", "two ", "three ", "four ", "five ","six ", "seven ", "eight ", "nine "
	};
    char ten_array[10][11] = {
		"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", 
		"seventeen ", "eighteen ", "nineteen "
	};
    char twenty_array[10][11] = {
        " "," ","twenty ", "thirty ", "fourty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "
	};
	char big_unit_array[22][20] = {"trillion ",
								   "billion ", "million ", "thounsand ", "hundred "};
	const int MaxUnit = 21;
	char tempString[150][20];
	//E.g. 1,234,567,890,123
	int i; //for only_number
	int j; //for tempString
	int rem;
	int sizeofNumber = strlen(only_number);
	int tflag = 0;
	for(i=0,j=0; i<sizeofNumber; i++) 
	{
		rem = (sizeofNumber-i)%3;
		if(!rem)
			tflag = 0;
		if(only_number[i] != '0') 
		{
			if(rem == 1) //in oneth position
			{
                strcpy(tempString[j++],one_array[only_number[i]-48]);
				tflag = 1;
			}
			else if(rem == 2) 
			{ //in tenth position
                if(only_number[i] == '1')
					strcpy(tempString[j++],ten_array[only_number[++i]-48]);
                else
					strcpy(tempString[j++],twenty_array[only_number[i]-48]);
				tflag = 1;
            }
            else if(rem == 0) 
			{// in hundredth position
				strcpy(tempString[j++],one_array[only_number[i]-48]);
				strcpy(tempString[j++],big_unit_array[MaxUnit]);
				tflag = 1;
			}
		}
		if(tflag)
		{
			switch(sizeofNumber-i) 
			{
				//select which big_unit_array to be placed behind the number
			case 64: strcpy(tempString[j++],big_unit_array[MaxUnit-21]); break;
			case 61: strcpy(tempString[j++],big_unit_array[MaxUnit-20]); break;
			case 58: strcpy(tempString[j++],big_unit_array[MaxUnit-19]); break;
			case 55: strcpy(tempString[j++],big_unit_array[MaxUnit-18]); break;
			case 52: strcpy(tempString[j++],big_unit_array[MaxUnit-17]); break;
			case 49: strcpy(tempString[j++],big_unit_array[MaxUnit-16]); break;
			case 46: strcpy(tempString[j++],big_unit_array[MaxUnit-15]); break;
			case 43: strcpy(tempString[j++],big_unit_array[MaxUnit-14]); break;
			case 40: strcpy(tempString[j++],big_unit_array[MaxUnit-13]); break;
			case 37: strcpy(tempString[j++],big_unit_array[MaxUnit-12]); break;
			case 34: strcpy(tempString[j++],big_unit_array[MaxUnit-11]); break;
			case 31: strcpy(tempString[j++],big_unit_array[MaxUnit-10]); break;
			case 28: strcpy(tempString[j++],big_unit_array[MaxUnit-9]); break;
			case 25: strcpy(tempString[j++],big_unit_array[MaxUnit-8]); break;
			case 22: strcpy(tempString[j++],big_unit_array[MaxUnit-7]); break;
			case 19: strcpy(tempString[j++],big_unit_array[MaxUnit-6]); break;
			case 16: strcpy(tempString[j++],big_unit_array[MaxUnit-5]); break;
			case 13: strcpy(tempString[j++],big_unit_array[MaxUnit-4]); break;
			case 10: strcpy(tempString[j++],big_unit_array[MaxUnit-3]); break;
			case 7 : strcpy(tempString[j++],big_unit_array[MaxUnit-2]); break;
			case 4 : strcpy(tempString[j++],big_unit_array[MaxUnit-1]); break;
			}
		}
	}
	j--; 
	//to minus an extra increment of j
	//paste text from tempString to outputString
	strcpy(outputString,tempString[0]);
	for(i=1; i<=j; i++)
		strcat(outputString, tempString[i]);
}
/* End of NumberToText() */

int ValidateNumber(char *number, char *only_number)
{
    char tempString[100]={'\0'};
    int i,j;
    int len = strlen(number);
    for(i=0, j=0; i<len; i++)
	{
		if(isdigit(number[i]))
			tempString[j++] = number[i];
		else
			break;
	}
	strcpy(only_number,tempString);
	return j;
}
/* End of ValidateNumber() */

Thanks

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Salem
0

1. Learn how to format code. Do NOT post code which is indented with a mixture of space and tabs. Forums will make a mess of your code.
Compare with this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include "myheader.h"
#include <stdlib.h>
#include <ctype.h>

#define TRUE 1
#define FALSE 0
typedef short BOOLEAN;

void PrintCheck(EmployeeRecord PrintS[], FILE *company, int numc);
char * generateRefCode(char * lastname,int chkNo);
BOOLEAN isVowel(char aChar);
void NumberToText(char *, char *); 
int ValidateNumber(char *, char *);


void PrintCheck(EmployeeRecord PrintS[], FILE *company, int numc)
{
  int i;
  int checknum=100;
  char number[100];
  char s[MAXNAMELENGTH];
  char only_number[100];// only number no extra characters
  char outputString[2048]; // text holder
  int fred;
  printf("Block");
  char cn1[MAXNAMELENGTH], cn2[MAXNAMELENGTH], a1[MAXNAMELENGTH],
  a2[MAXNAMELENGTH], a3[MAXNAMELENGTH], a4[MAXNAMELENGTH],
  cs1[MAXNAMELENGTH], cs2[MAXNAMELENGTH],cs3[MAXNAMELENGTH],
  cs4[MAXNAMELENGTH], zip[MAXNAMELENGTH],companyname[MAXNAMELENGTH],
  address[MAXNAMELENGTH],citystate[MAXNAMELENGTH];
  
  fred=fscanf(company,"%s%s%s%s%s%s%s%s%s%s%s",
  cn1, cn2, a1, a2, a3, a4, cs1, cs2, cs3, cs4, zip);
  printf("Block %d\n", fred); // It stops here
  strcpy(companyname, cn1);
  strcat(companyname, " ");
  strcat(companyname, cn2);
  printf("Block");
  strcpy(address, a1);
  strcat(address, " ");
  strcat(address, a2);
  strcat(address, " ");
  strcat(address, a3);
  strcat(address, " ");
  strcat(address, a4);
  printf("Block");
  strcpy(citystate, cs1);
  strcat(citystate, " ");
  strcat(citystate, cs2);
  strcat(citystate, " ");
  strcat(citystate, cs3);
  strcat(citystate, " ");
  strcat(citystate, cs4);
  printf("Block");

  for (i=0; i< numc; i++)
  {
    sprintf(number,"%.4g",(PrintS[i].netpay));
    if(strlen(number) > 66 || strlen(number) == 0)
    printf("Invalid length of numbers!\n");
    else 
    {
      if(ValidateNumber(number, only_number)) 
      {
        NumberToText(only_number, outputString);
        printf("Number in Text : %s\n",outputString);
      }
      else
      printf("\n\nInvalid Number !!!\n");
    }
    printf("********************************************************\n");
    printf("*                                 Check No. %d         *\n",checknum);
    printf("*                                                      *\n");
    printf("*    %s                                                *\n",companyname);
    printf("*    %s                                                *\n",address);
    printf("*    %s , %s                                           *\n",citystate,zip);
    printf("*                                                      *\n");
    printf("*                                                      *\n");
    printf("*    Pay to the Order of:                              *\n");
    printf("*    %s , %s                                           *\n",(PrintS[i].firstname),(PrintS[i].lastname));
    printf("*    The Sum of %s                                     *\n",outputString);
    printf("*                                                      *\n");
    printf("*                                                      *\n");
    printf("*    Reference: %s                                     *\n",generateRefCode((PrintS[i].lastname),checknum));
    printf("*                                                      *\n");
    printf("*                                                      *\n");
    printf("********************************************************\n");
    printf("*                                 Check No. %d         *\n",checknum);
    printf("*                                                      *\n");
    printf("*      %s,%s            %f               %f            *\n",(PrintS[i].lastname),(PrintS[i].firstname),
    (PrintS[i].reghrs),(PrintS[i].federaltax));
    printf("*      %s               %f               %f            *\n",address,(PrintS[i].ovthrs),(PrintS[i].statetax));
    printf("*      %s               %f               %f            *\n",citystate,(PrintS[i].gross),(PrintS[i].ssitax));
    printf("*      %s               %f               %f            *\n",zip,(PrintS[i].netpay),(PrintS[i].taxtotal));
    printf("*                                                      *\n");
    printf("********************************************************\n");
    checknum=checknum+1;
  }
  fclose(company);
}

char * generateRefCode(char * lastname,int chkNo)
{
  static char tempStr[30+1];
  int i,j = 1;

  tempStr[0] = lastname[0];
  for (i=1; i < strlen(lastname);i++)
  if (!isVowel(lastname[i])) tempStr[j++] = lastname[i];
  tempStr[j] = 0; // add string terminator
  return tempStr;
}

BOOLEAN isVowel(char aChar)
{
  switch (toupper(aChar)) //<ctype.h>
  {
  case 'A':
  case 'E':
  case 'I':
  case 'O':
  case 'U' : return TRUE;break;
  default: return FALSE;
  }
}

void NumberToText(char *only_number, char *outputString)
{
  char one_array[10][11] = {
    " ","one ", "two ", "three ", "four ", "five ","six ", "seven ", "eight ", "nine "
  };
  char ten_array[10][11] = {
    "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", 
    "seventeen ", "eighteen ", "nineteen "
  };
  char twenty_array[10][11] = {
    " "," ","twenty ", "thirty ", "fourty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "
  };
  char big_unit_array[22][20] = {"trillion ",
    "billion ", "million ", "thounsand ", "hundred "};
  const int MaxUnit = 21;
  char tempString[150][20];
  //E.g. 1,234,567,890,123
  int i; //for only_number
  int j; //for tempString
  int rem;
  int sizeofNumber = strlen(only_number);
  int tflag = 0;
  for(i=0,j=0; i<sizeofNumber; i++) 
  {
    rem = (sizeofNumber-i)%3;
    if(!rem)
    tflag = 0;
    if(only_number[i] != '0') 
    {
      if(rem == 1) //in oneth position
      {
        strcpy(tempString[j++],one_array[only_number[i]-48]);
        tflag = 1;
      }
      else if(rem == 2) 
      { //in tenth position
        if(only_number[i] == '1')
        strcpy(tempString[j++],ten_array[only_number[++i]-48]);
        else
        strcpy(tempString[j++],twenty_array[only_number[i]-48]);
        tflag = 1;
      }
      else if(rem == 0) 
      {// in hundredth position
        strcpy(tempString[j++],one_array[only_number[i]-48]);
        strcpy(tempString[j++],big_unit_array[MaxUnit]);
        tflag = 1;
      }
    }
    if(tflag)
    {
      switch(sizeofNumber-i) 
      {
        //select which big_unit_array to be placed behind the number
      case 64: strcpy(tempString[j++],big_unit_array[MaxUnit-21]); break;
      case 61: strcpy(tempString[j++],big_unit_array[MaxUnit-20]); break;
      case 58: strcpy(tempString[j++],big_unit_array[MaxUnit-19]); break;
      case 55: strcpy(tempString[j++],big_unit_array[MaxUnit-18]); break;
      case 52: strcpy(tempString[j++],big_unit_array[MaxUnit-17]); break;
      case 49: strcpy(tempString[j++],big_unit_array[MaxUnit-16]); break;
      case 46: strcpy(tempString[j++],big_unit_array[MaxUnit-15]); break;
      case 43: strcpy(tempString[j++],big_unit_array[MaxUnit-14]); break;
      case 40: strcpy(tempString[j++],big_unit_array[MaxUnit-13]); break;
      case 37: strcpy(tempString[j++],big_unit_array[MaxUnit-12]); break;
      case 34: strcpy(tempString[j++],big_unit_array[MaxUnit-11]); break;
      case 31: strcpy(tempString[j++],big_unit_array[MaxUnit-10]); break;
      case 28: strcpy(tempString[j++],big_unit_array[MaxUnit-9]); break;
      case 25: strcpy(tempString[j++],big_unit_array[MaxUnit-8]); break;
      case 22: strcpy(tempString[j++],big_unit_array[MaxUnit-7]); break;
      case 19: strcpy(tempString[j++],big_unit_array[MaxUnit-6]); break;
      case 16: strcpy(tempString[j++],big_unit_array[MaxUnit-5]); break;
      case 13: strcpy(tempString[j++],big_unit_array[MaxUnit-4]); break;
      case 10: strcpy(tempString[j++],big_unit_array[MaxUnit-3]); break;
      case 7 : strcpy(tempString[j++],big_unit_array[MaxUnit-2]); break;
      case 4 : strcpy(tempString[j++],big_unit_array[MaxUnit-1]); break;
      }
    }
  }
  j--; 
  //to minus an extra increment of j
  //paste text from tempString to outputString
  strcpy(outputString,tempString[0]);
  for(i=1; i<=j; i++)
  strcat(outputString, tempString[i]);
}
/* End of NumberToText() */

int ValidateNumber(char *number, char *only_number)
{
  char tempString[100]={'\0'};
  int i,j;
  int len = strlen(number);
  for(i=0, j=0; i<len; i++)
  {
    if(isdigit(number[i]))
    tempString[j++] = number[i];
    else
    break;
  }
  strcpy(only_number,tempString);
  return j;
}
/* End of ValidateNumber() */

2. "Reading location 0xcccccccc" means you tried to use uninitialised data. Debug builds add code which pre-set your variables to special patterns like 0xCC so that they are instantly recognisable when you see them.

3. Use a debugger and it will stop the program at the exception, and point you at a line of code.
Examine that line carefully, and you'll discover the problem.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.