473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

function error

In the following function there is an access violation error, some
memory can't be read. A week ago this code did compile.
Can anyone possibly tell me why my compiler is unable to read part of
this function.

bool processdrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
drecord Newdrecord;
char customercode[5];

strncpy( customercode, &record[1], 5);
customercode[5] = '\0';
if(!CheckAllNumeric( customercode )){
prnfile<< "Invalid: D record customer code does not contain
numerical format"<<endl;
prnfile<< record <<endl;
return false;
}
Newdrecord.customercode[5] = atol( customercode );

validdata.write((char*) record, Newdrecord.customercode[5]);

return true;
}

the program in full.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;

struct crecord {
long customercode[5];
char customername[21];
char customeraddress[61];
double customerbalance;
double creditlimit;

};
crecord Newcrecord;

struct irrecord {
long customercode[5];
long partnum[5];
long issue_rec;

};
irrecord Newirrecord;

struct drecord {
long customercode[5];
};
drecord Newdrecord;


////////////////////////////////////////////////////////////
bool IRRecordvalidlength(char* record)
{
int len;

switch(record[0])
{
case'i':
case'I':
case'r':
case'R':

len= strlen(record);
if(len<17)
return false;
break;
}

return true;
};
////////////////////////////////////////////////////////////
bool CRecordvalidlength(char* record)
{
int len;

switch(record[0])
{
case'c':
case'C':

len = strlen(record);
if(len<103)
return false;
break;

}
return true;

}
////////////////////////////////////////////////////////
bool Drecordvalidlength( char* record )
{
int len;

switch(record[0])
{
case'd':
case'D':

len = strlen(record);
if(len<7)
return false;
break;

}
return true;
}
////////////////////////////////////////////////////////////////////////////
bool CheckAllNumeric( const char * string )
{
int Len = strlen( string );

for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) )
return false;

return true;
}

////////////////////////////////////////////////////////////////////////////
bool CheckFloatNumber( const char* string )
{
int Len = strlen( string );

for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) &&
string[i] != '.' )
return false;

return true;
}

///////////////////////////////////////////////////////////////////////////
irrecord* CheckDigit(ofstream& prnfile, ofstream& validdata, char*
record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 6);
code[5] = '\0';
Newirrecord.customercode[5] = atol( code );
for(int i =0; i < 6; ++i)
check1 += Newirrecord.customercode[i] * weightingfactor;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(! Newirrecord.customercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

if(Newirrecord.customercode[5] == checkdigit){

validdata.write((char*) record, Newirrecord.customercode[5]);
}

}
/////////////////////////////////////////////////////////////////////////////////////

drecord* checkdigit(ofstream& prnfile, ofstream& validdata, char*
record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 5);
code[5] = '\0';
Newdrecord.customercode[5] = atol( code );
for(int i =0; i < 6; ++i)
check1 += Newdrecord.customercode[i] * weightingfactor;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(! Newdrecord.customercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

validdata.write((char*) record, Newdrecord.customercode[5]);
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
crecord* checkdigitforcustomercode( ofstream& prnfile, ofstream&
validdata, char* record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 5);
code[5] = '\0';
Newcrecord.customercode[5] = atol( code );
for(int i =0; i < 6; ++i)
check1 += Newcrecord.customercode[i] * weightingfactor;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(checkdigit == 11){
checkdigit = 'X';
}
else
if(checkdigit = checkdigit = 10){
checkdigit = '0';
}
if(! Newcrecord.customercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

validdata.write((char*) record, Newcrecord.customercode[5]);

}
////////////////////////////////////////////////////////////////////////////////////
irrecord* checkdigitforpartnum(ofstream& prnfile, ofstream& validdata,
char* record)
{
int weightingfactor2;
char part_num[6];
int partnumcheck;
int produce;
int holdall;

strncpy(part_num, &record[7], 6);
part_num[6] = '\0';
Newirrecord.partnum[6] = atol( part_num );

weightingfactor2 = 6;

for(int i=0; i < 6; ++i)
holdall += Newirrecord.partnum[i] * weightingfactor2;
weightingfactor2 --;

produce = holdall % 11;

partnumcheck = 11 - produce;
if(partnumcheck == 11){
partnumcheck = 'X';
}
else
if(partnumcheck = 10){
partnumcheck = '0';
}

if(! Newirrecord.partnum[5] == partnumcheck){
prnfile<< "Invalid: Incorrect part number, check digit invalid";
prnfile<< record << endl;
}
return false;

validdata.write((char*) record, Newirrecord.partnum[5]);

}

////////////////////////////////////////////////////////////////////////////////////
bool CheckAddress( const char* alpha )
{

int max_alpha = 60;

for( int i = 0; i < max_alpha; ++i )
if( !isalpha( alpha[i] ) && ( alpha[i] != ';;;;' ) && ( alpha[i] !=
' ' ))
if(Newcrecord.customeraddress[61] != (alpha[i]))
return false;

return true;
}
///////////////////////////////////////////////////////////////////////////////
bool Processcrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
char customercode[6];
char balance[10];
char limit[10];
crecord Newcrecord;

//
// process the customer code
//
strncpy( customercode, &record[1], 5 );
customercode[5] = '\0';
if( !CheckAllNumeric( customercode ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.customercode[5] = atol( customercode );

//
// process the customer name
//
strncpy( Newcrecord.customername, &record[7], 20 );
Newcrecord.customername[20] = '\0';

//
// process the customer address
//
strncpy( Newcrecord.customeraddress, &record[27], 61 );
Newcrecord.customeraddress[61] = '\0';


// process the customer balance
//
strncpy( balance, &record[87], 9 );
balance[9] = '\0';
if( !CheckFloatNumber( balance ) ) {
prnfile << "Invalid: balance field is not a valid number [position
88 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.customerbalance = atof( balance );

cout << setiosflags(ios::fixed)
<< setprecision(2);

validdata.write((char*) record, Newcrecord.customerbalance );

//
// process the customer limit
//
strncpy( limit, &record[97], 9 );
limit[9] = '\0';
if( !CheckFloatNumber( limit ) ) {
prnfile << "Invalid: limit field is not a valid number [position
98 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.creditlimit = atof( limit );

validdata.write((char*) record, Newcrecord.creditlimit );

return true;

}

///////////////////////////////////////////////////////

bool processdrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
drecord Newdrecord;
char customercode[5];

strncpy( customercode, &record[1], 5);
customercode[5] = '\0';
if(!CheckAllNumeric( customercode )){
prnfile<< "Invalid: D record customer code does not contain
numerical format"<<endl;
prnfile<< record <<endl;
return false;
}
Newdrecord.customercode[5] = atol( customercode );

validdata.write((char*) record, Newdrecord.customercode[5]);

return true;
}

/////////////////////////////////////////////////////////////////////////
bool ProcessIRecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
char customer[6];
char issue_rec[5];
char code[4];
char part[6];

long customer_code;
long issuerec;
long partnum[6];

//
// process the customer code
//
strncpy( customer, &record[2], 5 );
customer[6] = '\0';
if( !CheckAllNumeric( customer ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
customer_code = atol( customer );

//
// process the part number
//
strncpy( part, &record[7], 6 );
part[6] = '\0';
if( !CheckAllNumeric( part ) ) {
prnfile << "Invalid: part number needs to be all numeric [position
9 - 6 characters]:\n";
prnfile << record << endl;
return false;
}
partnum[6] = atol( part );

validdata.write((char*) record, partnum[6]);

return true;

//
// process the third number
//
strncpy( issue_rec, &record[13], 4 );
code[4] = '\0';
if( !CheckAllNumeric( issue_rec ) ) {
prnfile << "Invalid: the code needs to be all numeric [position 16
- 4 characters]:\n";
prnfile << record << endl;
return false;
}
issuerec = atol( issue_rec );
return true;
}

/////////////////////////////////////////////////////////////////////////
int main()
{
const char infile[] = "A:\\514650TD.txt";
const char outfile[] = "A:\\514650VD.DAT";
const char printerfile[] = "A:\\514650.TXT";

int max = 256;

char temp1[256];

ifstream testdata;
ofstream validdata;
ofstream prnfile;

testdata.open("A:\\514650TD.txt", ios::in);
if(!testdata)
{
cout<<"The file does not already exist" << infile << endl;
return EXIT_FAILURE;
}

validdata.open("A:\\514650.DAT", ios::out | ios::binary);
if(!validdata.is_open())
{
cout<<" The file could not be opened " << outfile <<endl;
return EXIT_FAILURE;
}

prnfile.open("A:\\514650.TXT", ios::out);
if(!prnfile)
{
cout<< "File could not be opened" << prnfile << endl;
return EXIT_FAILURE;
};
prnfile << "C_RECORD, I-R RECORD, D_RECORD ERRORS" << endl;

while( testdata.getline( temp1, sizeof(temp1)) ){
if(!CRecordvalidlength( temp1 )){
prnfile<<"Invalid: record does not contain enough characters:\n";
prnfile<< temp1 <<endl;
continue;
}
if(!IRRecordvalidlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
}
if(!Drecordvalidlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
};

switch( temp1[0] )
{
case 'c':
case 'C':
Processcrecord( prnfile, validdata, temp1 );
checkdigitforcustomercode( prnfile, validdata, temp1);
//must be able to reference
//these two, but only by creating variables within the function
they came from.
//it could be possible that these variables will have to be
pointers.
//there is a page about it in the newsgroups.

break;

case 'i':
case 'I':
case 'r':
case 'R':
ProcessIRecord( prnfile, validdata, temp1 );
CheckDigit( prnfile, validdata, temp1 );
checkdigitforpartnum( prnfile, validdata, temp1 );
break;

case 'd':
case 'D':
processdrecord( prnfile, validdata, temp1 );
checkdigit( prnfile, validdata, temp1 );
break;
default: prnfile<< "Unknown record";
prnfile<< temp1 << endl;
};

}
testdata.close();
validdata.close();
prnfile.close();
return EXIT_SUCCESS;

};
Jul 19 '05 #1
6 1870
"muser" <ch**********@hotmail.com> wrote in message
news:f9**************************@posting.google.c om...
In the following function there is an access violation error, some
memory can't be read. A week ago this code did compile.
Can anyone possibly tell me why my compiler is unable to read part of
this function.

bool processdrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
drecord Newdrecord;
char customercode[5];

strncpy( customercode, &record[1], 5);
You're trying to write past the end of the array
'customercode', whose valid indices are 0 - 4.
Undefined behavior.
customercode[5] = '\0';


You're trying to write past the end of the array
'customercode', whose valid indices are 0 - 4.
Undefined behavior.

I didn't look any further.

-Mike
Jul 19 '05 #2

"muser" <ch**********@hotmail.com> wrote in message news:f9**************************@posting.google.c om...
customercode[5] = '\0';
You are writing the sixth position in a five character array.
Newdrecord.customercode[5] = atol( customercode );
Same problem here. What do you think you're doing?
validdata.write((char*) record, Newdrecord.customercode[5]);


Why do you think the number of characters to be written should be some goofy undefined
value?

Jul 19 '05 #3

"muser" <ch**********@hotmail.com> wrote in message
news:f9**************************@posting.google.c om...
In the following function there is an access violation error, some
memory can't be read. A week ago this code did compile.
Can anyone possibly tell me why my compiler is unable to read part of
this function.


I'm confused. You talk about an access violation, which is a run-time
error, then you say that it used to compile, and ask why the compiler is
"unable to read part of this function". Is it failing to compile, or is it
crashing when running? And what do you mean when you say the compiler is
"unable to read part of this function"? Is the compiler giving you an
error? What's the error message?

I just noticed the answers regarding your writing past the end of your local
arrays. That's a very likely cause of an access violation, and also a
reason why it might have worked before. When you do something that invokes
undefined behavior like that, you never know what's going to happen. It
might work fine for you, but crash for someone else. Fix those problems.
Then let us know if there is a compile problem, too, and what the compiler
error message is, and what line of code it refers to. (And make sure that
you know the difference between compile-time and run-time errors!)

-Howard
Jul 19 '05 #4
"Howard" <al*****@hotmail.com> wrote in message news:<bn********@dispatch.concentric.net>...
"muser" <ch**********@hotmail.com> wrote in message
news:f9**************************@posting.google.c om...
In the following function there is an access violation error, some
memory can't be read. A week ago this code did compile.
Can anyone possibly tell me why my compiler is unable to read part of
this function.


I'm confused. You talk about an access violation, which is a run-time
error, then you say that it used to compile, and ask why the compiler is
"unable to read part of this function". Is it failing to compile, or is it
crashing when running? And what do you mean when you say the compiler is
"unable to read part of this function"? Is the compiler giving you an
error? What's the error message?

I just noticed the answers regarding your writing past the end of your local
arrays. That's a very likely cause of an access violation, and also a
reason why it might have worked before. When you do something that invokes
undefined behavior like that, you never know what's going to happen. It
might work fine for you, but crash for someone else. Fix those problems.
Then let us know if there is a compile problem, too, and what the compiler
error message is, and what line of code it refers to. (And make sure that
you know the difference between compile-time and run-time errors!)

-Howard


completely different problem, but still in the area of functions.
here is the function.

bool checkdigitinaddress( const char* string )
{
for( int i =0; i <4; i++)
if(string[i] == isalpha(string[i]))//find first occurance of a
character//
for(i = string[i]; i <4; i++)//increment to the next set of
characters
if(string[i] != isalpha(string[i]))//if the next element, after a
character is found
//isn't another character then return false.//
return false;
if(string[i] == isalpha(string[i]))//find first occurance of a
character//
for(i = string[i]; i <4; --i)//decrement to the previous set of
characters//
if(string[i] != isdigit(string[i]))//if the previous element,
before a character is found
//isn't a digit then return false.//
return false;

return true;

return true;

}
the line this function is suppose to evaluate is an address.
a1a2kewgardens etc etc.
it should return false and this should be written to the approriate
error file like so:

strncpy( Newcrecord.customeraddress, &record[27], 61 );
Newcrecord.customeraddress[61] = '\0';
if(!checkdigitinaddress( Newcrecord.customeraddress )){
prnfile<< "Invalid customer address, incorrect format";
prnfile<< record << endl;
return false;
}
return true;

can you please tell me why it isn't picking up the error in the
address.
Jul 19 '05 #5
muser escribió:
if(string[i] == isalpha(string[i]))//find first occurance of a


isalpha returns 0 or 1, not a char value . Use simply if (isalpha
(string [i] ) )

Regards.
Jul 19 '05 #6
In article <3F***************@terra.es>, JU********@terra.es says...

[ ... ]
isalpha returns 0 or 1, not a char value .


More accurately, it returns 0 or non-zero -- it can (and in many
implementations does) return a value other than 1 to indicate true.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Hansen | last post by:
I'm sure I saw this somewhere but can't remember where and can't find it now... Is there a PHP function or global variable that will return name of the calling function? I want to do this for...
9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
11
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
3
by: Piotre Ugrumov | last post by:
I have declared some function virtual, but when I call this function I have errors. In a class Simulator I have defined this: Nave *navi; The virtual function is the function modifica(). I call...
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
6
by: jchao123 | last post by:
Dear All, I have an MDB file (Access 2000/XP) which contains generic routines I use in various apps (eg, API calls, File access classes etc). I have compiled into an MDE file which I reference...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
10
by: lasmith329 | last post by:
I've never posted a question on any site before, but after racking my head over this hurdle for days, I've caved. I'm working on a program that creates a kml file and exports it to Google Earth. In...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.