Karl this is albeit your program, in the program that you wrote for me
I learned quite alot, i didn't realise I could return NULL for
instance or even use functions without first declaring them, but this
is where I'm encountering resistance, as all the bool functions are
compiling with the error: local function definitions are illegal. I've
never used using namespace std before and was wondering if you had
created a header file in conjunction with the program you wrote. Your
program executed perfectly.
Any one else who has spotted something amiss please post back.
Thank you for your kind attention in this matter.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct crecord {
long customercode;
char customername[21];
char customeraddress[61];
double customerbalance ;
double creditlimit;
};
struct irrecord {
long customercode;
long partnum;
long issue_rec;
};
struct drecord {
long customercode;
};
////////////////////////////////////////////////////////////
bool IRRecordvalidle ngth(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 CRecordvalidlen gth(ofstream& prnfile, char* cstring)
{
int len;
switch(cstring[0])
{
case'c':
case'C':
len = strlen(cstring) ;
if(len<103){
prnfile<< "Invalid:";
prnfile<< cstring << endl;
return false;
};
return true;
};
////////////////////////////////////////////////////////
bool Drecordvalidlen gth(ofstream& prnfile, char* dstring)
{
int len;
switch(dstring)
{
case'd':
case'D':
len = strlen(dstring) ;
if(len<7){
prnfile<< "Invalid:";
prnfile<< dstring <<endl;
};
return false;
};
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 CheckFloatNumbe r( const char* string )
{
int Len = strlen( string );
for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) &&
string[i] != '.' )
return false;
return true;
}
////////////////////////////////////////////////////////////////
bool Processcrecord( ofstream& prnfile, char* Record )
{
char code[6];
char balance[10];
char limit[10];
crecord Newcrecord;
//
// process the customer code
//
strncpy( code, &record[2], 5 );
code[5] = '\0';
if( !CheckAllNumeri c( customercode ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omercode = atol( customercode );
//
// process the customer name
//
strncpy( Newcrecord.cust omername, &record[7], 20 );
Newcrecord.cust omername[20] = '\0';
//
// process the customer address
//
strncpy( Newcrecord.addr ess, &record[27], 60 );
Newcrecord.addr ess[60] = '\0';
//
// process the customer balance
//
strncpy( balance, &record[87], 9 );
balance[9] = '\0';
if( !CheckFloatNumb er( balance ) ) {
prnfile << "Invalid: balance field is not a valid number [position
88 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omerbalance = atof( balance );
//
// process the customer limit
//
strncpy( limit, &record[97], 9 );
limit[9] = '\0';
if( !CheckFloatNumb er( limit ) ) {
prnfile << "Invalid: limit field is not a valid number [position
98 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cred itlimit = atof( limit );
return true;
};
///////////////////////////////////////////////////////
bool processdrecord( ofstream& prnfile, char* dstring)
{
char code[6];
strncpy( code, &dstring[2], 5);
code[6] = '\0';
if(!CheckAllNum eric(customerco de)){
prnfile<< "Invalid: D record customer code does not contain
numerical format"<<endl;
prnfile<< dstring <<endl;
return false;
};
NewDrecord.cust omercode = atol( code );
return true;
};
bool ProcessIRecord( ofstream& prnfile, char* record )
{
char customer[6];
char issue_rec[5];
char code[4];
char part[7];
long customer_code;
long part_number;
long issuerec;
//
// process the customer code
//
strncpy( customer, &record[2], 5 );
customer[5] = '\0';
if( !CheckAllNumeri c( 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( !CheckAllNumeri c( part ) ) {
prnfile << "Invalid: part number needs to be all numeric [position
9 - 6 characters]:\n";
prnfile << record << endl;
return false;
}
part_number = atol( part );
//
// process the third number
//
strncpy( issuerec, &Record[13], 4 );
code[4] = '\0';
if( !CheckAllNumeri c( 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 CheckDigit(long part_number[6], char* record)
{
char partn;
int weightingfactor ;
int remainder;
int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;
int product;
int Mod11 = 11;
strncpy(partn, &record[7], 6);
partn = '\0';
part_number = atol( partn );
weightingfactor = 6;
weightitem1 = (part_number[1] * weightingfactor );
weightingfactor = 5;
weightitem2 = (part_number[2] * weightingfactor );
weightingfactor = 4;
weightitem3 = (part_number[3] * weightingfactor );
weightingfactor = 3;
weightitem4 = (part_number[4] * weightingfactor );
weightingfactor = 2;
weightitem5 = (part_number[5] * weightingfactor );
product = (weightitem1 + weightitem2 + weightitem3 + weightitem4 +
weightitem5);
remainder = (product % Mod11);
checkdigit = (Mod11 - remainder);// may need an if statement
return checkdigit;
};
bool ProcessPartNumb er(ofstream& prnfile, char* record)
{
char processpartnum;
long partnumber1[6];
strncpy(process partnum, &record[7], 6);
processpartnum = '\0';
if( !CheckDigit( processpartnum) ){
prnfile<< "Invalid: Part number does not contain correct check
digit";
prnfile << record << endl;
return false;
};
partnumber1 = atol( processpartnum );
return true;
};
int CheckDigitForCu stomerCode(long customer_code[5], char* record)
{
int weightfactor;
int counter1, counter2, counter3, counter4;
int remainder;
int prod;
char custcode;
int Mod11 = 11;
int check_digit;
strncpy(custcod e, &record[2], 5);
custcode = '\0';
customer_code[5] = atol( custcode );
weightfactor = 5;
counter1 = (customer_code[1] * weightfactor);
weightfactor = 4;
counter2 = (customer_code[2] * weightfactor);
weightfactor = 3;
counter3 = (customer_code[3] * weightfactor);
weightfactor = 2;
counter4 = (customer_code[4] * weightfactor);
prod = (counter1 + counter2 + counter3 + counter4);
remainder = (prod % Mod11);
check_digit = (Mod11 - remainder);
return check_digit
};
bool ProcessCCustome rCode(ofstream& prnfile, char* record)
{
char check_code;
long checkcode;
strncpy(check_c ode, &record[2], 5);
check_code = '\0';
if( !CheckDigitForC ustomerCode( check_code ){
prnfile<< "Invalid: Incorrect c record customer code, invalid check
digit";
prnfile<< record <<endl;
return false;
};
checkcode = atol (check_code);
return true;
};
bool ProcessDCustome rCode(ofstream& prnfile, char* dstring)
{
char dcustomercode;
long d_code;
strncpy(dcustom ercode, &dstring[2], 5);
dcustomercode = '\0';
if( !CheckDigitForC ustomerCode ( dcode ) {
prnfile<< "Invalid: Incorrect d record customer code, invalid check
digit";
prnfile<< dstring <<endl;
return false;
};
d_code = atol( dcustomercode );
return true;
};
bool ProcessIRCustom erCode(ofstream & prnfile, char* record)
{
char ircode;
irrecord Newirrecord;
strncpy(ircode, &record[2], 5);
ircode = '\0';
if( !CheckDigitForC ustomerCode( ircode ) {
prnfile<< "Invalid: incorrect ir record customer code, invalid check
digit";
prnfile<< record << endl;
return false;
};
Newirrecord.cus tomercode = atol ( ircode );
return true;
};
int main()
{
const char infile[] = "A:\\514650TD.t xt";
const char outfile[] = "A:\\514650VD.D AT";
const char printerfile[] = "A:\\514650TD.T XT";
char temp1[104];
ifstream testdata;
ofstream validdata;
ofstream prnfile;
testdata.open(t estdata, ios::in);
if(!testdata.fa il())
{
cout<<"The file does not already exist";
return EXIT_FAILURE;
}
else
{
cout<<"514650TD is open";
};
validdata.open( validdata, ios::out || ios::binary);
if(!validdata.i s_open())
{
cout<<" The file could not be opened "<<endl;;
return EXIT_FAILURE;
}
else
{
cout<<" 514650VD is open "<<endl;
};
prnfile.open(pr interfile, ios::out || ios::binary);
if(!prnfile.is_ open())
{
cout<< "File could not be opened";
return EXIT_FAILURE;
};
prnfile << "C_RECORD, I-R RECORD, D_RECORD ERRORS" << endl;
while( testdata.getlin e( temp1, sizeof(temp1)))
{
if(!CRecordvali dlength( temp1 )){
prnfile<<"Inval id: record does not contain enough characters:\n";
prnfile<< temp1 <<endl;
continue;
};
switch( temp1[0] )
{
case 'c':
case 'C':
Processcrecord( prnfile, temp1 );
break;
case 'i':
case 'I':
case 'r':
case 'R':
ProcessIRecord( prnfile, temp1 );
break;
case 'd':
case 'D':
processdrecord( prnfile, temp1 );
};
testdata.close( );
validdata.close ();
prnfile.close() ;
};
return EXIT_SUCCESS;
};
I learned quite alot, i didn't realise I could return NULL for
instance or even use functions without first declaring them, but this
is where I'm encountering resistance, as all the bool functions are
compiling with the error: local function definitions are illegal. I've
never used using namespace std before and was wondering if you had
created a header file in conjunction with the program you wrote. Your
program executed perfectly.
Any one else who has spotted something amiss please post back.
Thank you for your kind attention in this matter.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct crecord {
long customercode;
char customername[21];
char customeraddress[61];
double customerbalance ;
double creditlimit;
};
struct irrecord {
long customercode;
long partnum;
long issue_rec;
};
struct drecord {
long customercode;
};
////////////////////////////////////////////////////////////
bool IRRecordvalidle ngth(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 CRecordvalidlen gth(ofstream& prnfile, char* cstring)
{
int len;
switch(cstring[0])
{
case'c':
case'C':
len = strlen(cstring) ;
if(len<103){
prnfile<< "Invalid:";
prnfile<< cstring << endl;
return false;
};
return true;
};
////////////////////////////////////////////////////////
bool Drecordvalidlen gth(ofstream& prnfile, char* dstring)
{
int len;
switch(dstring)
{
case'd':
case'D':
len = strlen(dstring) ;
if(len<7){
prnfile<< "Invalid:";
prnfile<< dstring <<endl;
};
return false;
};
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 CheckFloatNumbe r( const char* string )
{
int Len = strlen( string );
for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) &&
string[i] != '.' )
return false;
return true;
}
////////////////////////////////////////////////////////////////
bool Processcrecord( ofstream& prnfile, char* Record )
{
char code[6];
char balance[10];
char limit[10];
crecord Newcrecord;
//
// process the customer code
//
strncpy( code, &record[2], 5 );
code[5] = '\0';
if( !CheckAllNumeri c( customercode ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omercode = atol( customercode );
//
// process the customer name
//
strncpy( Newcrecord.cust omername, &record[7], 20 );
Newcrecord.cust omername[20] = '\0';
//
// process the customer address
//
strncpy( Newcrecord.addr ess, &record[27], 60 );
Newcrecord.addr ess[60] = '\0';
//
// process the customer balance
//
strncpy( balance, &record[87], 9 );
balance[9] = '\0';
if( !CheckFloatNumb er( balance ) ) {
prnfile << "Invalid: balance field is not a valid number [position
88 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omerbalance = atof( balance );
//
// process the customer limit
//
strncpy( limit, &record[97], 9 );
limit[9] = '\0';
if( !CheckFloatNumb er( limit ) ) {
prnfile << "Invalid: limit field is not a valid number [position
98 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cred itlimit = atof( limit );
return true;
};
///////////////////////////////////////////////////////
bool processdrecord( ofstream& prnfile, char* dstring)
{
char code[6];
strncpy( code, &dstring[2], 5);
code[6] = '\0';
if(!CheckAllNum eric(customerco de)){
prnfile<< "Invalid: D record customer code does not contain
numerical format"<<endl;
prnfile<< dstring <<endl;
return false;
};
NewDrecord.cust omercode = atol( code );
return true;
};
bool ProcessIRecord( ofstream& prnfile, char* record )
{
char customer[6];
char issue_rec[5];
char code[4];
char part[7];
long customer_code;
long part_number;
long issuerec;
//
// process the customer code
//
strncpy( customer, &record[2], 5 );
customer[5] = '\0';
if( !CheckAllNumeri c( 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( !CheckAllNumeri c( part ) ) {
prnfile << "Invalid: part number needs to be all numeric [position
9 - 6 characters]:\n";
prnfile << record << endl;
return false;
}
part_number = atol( part );
//
// process the third number
//
strncpy( issuerec, &Record[13], 4 );
code[4] = '\0';
if( !CheckAllNumeri c( 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 CheckDigit(long part_number[6], char* record)
{
char partn;
int weightingfactor ;
int remainder;
int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;
int product;
int Mod11 = 11;
strncpy(partn, &record[7], 6);
partn = '\0';
part_number = atol( partn );
weightingfactor = 6;
weightitem1 = (part_number[1] * weightingfactor );
weightingfactor = 5;
weightitem2 = (part_number[2] * weightingfactor );
weightingfactor = 4;
weightitem3 = (part_number[3] * weightingfactor );
weightingfactor = 3;
weightitem4 = (part_number[4] * weightingfactor );
weightingfactor = 2;
weightitem5 = (part_number[5] * weightingfactor );
product = (weightitem1 + weightitem2 + weightitem3 + weightitem4 +
weightitem5);
remainder = (product % Mod11);
checkdigit = (Mod11 - remainder);// may need an if statement
return checkdigit;
};
bool ProcessPartNumb er(ofstream& prnfile, char* record)
{
char processpartnum;
long partnumber1[6];
strncpy(process partnum, &record[7], 6);
processpartnum = '\0';
if( !CheckDigit( processpartnum) ){
prnfile<< "Invalid: Part number does not contain correct check
digit";
prnfile << record << endl;
return false;
};
partnumber1 = atol( processpartnum );
return true;
};
int CheckDigitForCu stomerCode(long customer_code[5], char* record)
{
int weightfactor;
int counter1, counter2, counter3, counter4;
int remainder;
int prod;
char custcode;
int Mod11 = 11;
int check_digit;
strncpy(custcod e, &record[2], 5);
custcode = '\0';
customer_code[5] = atol( custcode );
weightfactor = 5;
counter1 = (customer_code[1] * weightfactor);
weightfactor = 4;
counter2 = (customer_code[2] * weightfactor);
weightfactor = 3;
counter3 = (customer_code[3] * weightfactor);
weightfactor = 2;
counter4 = (customer_code[4] * weightfactor);
prod = (counter1 + counter2 + counter3 + counter4);
remainder = (prod % Mod11);
check_digit = (Mod11 - remainder);
return check_digit
};
bool ProcessCCustome rCode(ofstream& prnfile, char* record)
{
char check_code;
long checkcode;
strncpy(check_c ode, &record[2], 5);
check_code = '\0';
if( !CheckDigitForC ustomerCode( check_code ){
prnfile<< "Invalid: Incorrect c record customer code, invalid check
digit";
prnfile<< record <<endl;
return false;
};
checkcode = atol (check_code);
return true;
};
bool ProcessDCustome rCode(ofstream& prnfile, char* dstring)
{
char dcustomercode;
long d_code;
strncpy(dcustom ercode, &dstring[2], 5);
dcustomercode = '\0';
if( !CheckDigitForC ustomerCode ( dcode ) {
prnfile<< "Invalid: Incorrect d record customer code, invalid check
digit";
prnfile<< dstring <<endl;
return false;
};
d_code = atol( dcustomercode );
return true;
};
bool ProcessIRCustom erCode(ofstream & prnfile, char* record)
{
char ircode;
irrecord Newirrecord;
strncpy(ircode, &record[2], 5);
ircode = '\0';
if( !CheckDigitForC ustomerCode( ircode ) {
prnfile<< "Invalid: incorrect ir record customer code, invalid check
digit";
prnfile<< record << endl;
return false;
};
Newirrecord.cus tomercode = atol ( ircode );
return true;
};
int main()
{
const char infile[] = "A:\\514650TD.t xt";
const char outfile[] = "A:\\514650VD.D AT";
const char printerfile[] = "A:\\514650TD.T XT";
char temp1[104];
ifstream testdata;
ofstream validdata;
ofstream prnfile;
testdata.open(t estdata, ios::in);
if(!testdata.fa il())
{
cout<<"The file does not already exist";
return EXIT_FAILURE;
}
else
{
cout<<"514650TD is open";
};
validdata.open( validdata, ios::out || ios::binary);
if(!validdata.i s_open())
{
cout<<" The file could not be opened "<<endl;;
return EXIT_FAILURE;
}
else
{
cout<<" 514650VD is open "<<endl;
};
prnfile.open(pr interfile, ios::out || ios::binary);
if(!prnfile.is_ open())
{
cout<< "File could not be opened";
return EXIT_FAILURE;
};
prnfile << "C_RECORD, I-R RECORD, D_RECORD ERRORS" << endl;
while( testdata.getlin e( temp1, sizeof(temp1)))
{
if(!CRecordvali dlength( temp1 )){
prnfile<<"Inval id: record does not contain enough characters:\n";
prnfile<< temp1 <<endl;
continue;
};
switch( temp1[0] )
{
case 'c':
case 'C':
Processcrecord( prnfile, temp1 );
break;
case 'i':
case 'I':
case 'r':
case 'R':
ProcessIRecord( prnfile, temp1 );
break;
case 'd':
case 'D':
processdrecord( prnfile, temp1 );
};
testdata.close( );
validdata.close ();
prnfile.close() ;
};
return EXIT_SUCCESS;
};
Comment