M
muser
I have a problem with initialising a char variable. I wrote about this
some weeks back, and someone mentioned that the problem could lie in a
POD structure, although wasn't sure this is the case.
How can I initialise temp2, and the union instance of unionarray?
These two are coming up as warnings as oppose to errors themselves.
Also while I have your attention, the logic in this program is to read
a binary into a character array, depending on the first letter in the
array (i.e. if 'c' then Newcrecord, 'i' or 'r' Newirrecord, 'd'
Newdrecord, which are all structures.) the appropiate structure member
(customercode) should be called then sorted in numerical order.
In my program I've already specified Newcrecord, when I don't that
will be the first structure that needs sorting.
if(sort_file.peek(temp2[0]) == 'c' | 'C')//is this synatically correct
{
for( int i=0; i <loop; i++)
str_ptr1.Newcrecord.Newcrecord.customercode, '\0';
etc etc;
Thank you for you help in both matters
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct crecord {
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;
};
struct irrecord {
char customercode[5];
char partnum[6];
char issue_rec[5];
};
struct drecord {
char customercode[5];
};
int loop = 200;
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;
};
Allrecords unionarray;
void sort_function(union Allrecords unionarray)
{
union Allrecords *str_ptr1, *str_ptr2, tempstr;
for(int i =0; i< loop; i++)
while(strcmp(str_ptr1.Newcrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.
for( i=0; i<loop; i++)
while(strcmp(str_ptr2.Newcrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1.Newirrecord.customercode,
str_ptr2.Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;
}
*str_ptr1++;//incremented, so that the same code isn't sorted
again
}
str_ptr2++;
}
}
//if strcmp(unionarray.dreccusto, unionaarry +1 ] crec.cuscode[
int main()
{
const char sorted_file[] = "A\\514650P2SD.txt";
union Allrecords unionarray;
char* temp2;
fstream sort_file;
sort_file.open(sorted_file, ios::in || ios::binary);
if(!sort_file)
{
cout<<"Cannot creat file"<< endl;
return EXIT_FAILURE;
}
while(sort_file.peek() != EOF)//read the whole file.
{
sort_file.read((char*) temp2, sizeof(temp2));
switch(temp2[1])
{
case 'i':
case 'I':
case 'r':
case 'R':
case 'd':
case 'D':
sort_function(unionarray);
default:;
}
}
return 0;
}
some weeks back, and someone mentioned that the problem could lie in a
POD structure, although wasn't sure this is the case.
How can I initialise temp2, and the union instance of unionarray?
These two are coming up as warnings as oppose to errors themselves.
Also while I have your attention, the logic in this program is to read
a binary into a character array, depending on the first letter in the
array (i.e. if 'c' then Newcrecord, 'i' or 'r' Newirrecord, 'd'
Newdrecord, which are all structures.) the appropiate structure member
(customercode) should be called then sorted in numerical order.
In my program I've already specified Newcrecord, when I don't that
will be the first structure that needs sorting.
if(sort_file.peek(temp2[0]) == 'c' | 'C')//is this synatically correct
{
for( int i=0; i <loop; i++)
str_ptr1.Newcrecord.Newcrecord.customercode, '\0';
etc etc;
Thank you for you help in both matters
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct crecord {
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;
};
struct irrecord {
char customercode[5];
char partnum[6];
char issue_rec[5];
};
struct drecord {
char customercode[5];
};
int loop = 200;
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;
};
Allrecords unionarray;
void sort_function(union Allrecords unionarray)
{
union Allrecords *str_ptr1, *str_ptr2, tempstr;
for(int i =0; i< loop; i++)
while(strcmp(str_ptr1.Newcrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.
for( i=0; i<loop; i++)
while(strcmp(str_ptr2.Newcrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1.Newirrecord.customercode,
str_ptr2.Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;
}
*str_ptr1++;//incremented, so that the same code isn't sorted
again
}
str_ptr2++;
}
}
//if strcmp(unionarray.dreccusto, unionaarry +1 ] crec.cuscode[
int main()
{
const char sorted_file[] = "A\\514650P2SD.txt";
union Allrecords unionarray;
char* temp2;
fstream sort_file;
sort_file.open(sorted_file, ios::in || ios::binary);
if(!sort_file)
{
cout<<"Cannot creat file"<< endl;
return EXIT_FAILURE;
}
while(sort_file.peek() != EOF)//read the whole file.
{
sort_file.read((char*) temp2, sizeof(temp2));
switch(temp2[1])
{
case 'i':
case 'I':
case 'r':
case 'R':
case 'd':
case 'D':
sort_function(unionarray);
default:;
}
}
return 0;
}