built in types

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;

}
 
L

lilburne

muser said:
if(sort_file.peek(temp2[0]) == 'c' | 'C')//is this synatically correct

Yes! but it is not sematically correct if it is your
intention to compare temp2[0] with either 'c' or 'C'. What
you are doing is comparing the | (bit-or) of 'c' (0x63) and
'C' (0x43), which just happens to be 0x63 ('c') against the
value in temp2[0]. In other words you'll only ever detect 'c'.

temp2[0]) == 'c' || temp2[0]) == 'C'

is the expression you need.
while(sort_file.peek() != EOF)//read the whole file.
{
sort_file.read((char*) temp2, sizeof(temp2));

I don't suppose that it is your intention to read 4 bytes,
but that is what you are doing here try sizeof(*temp).
You'll also need to intialize temp2 to point somewhere. You
could try 'char temp2[1];'
 
V

Victor Bazarov

lilburne said:
muser said:
if(sort_file.peek(temp2[0]) == 'c' | 'C')//is this synatically correct

Yes! but it is not sematically correct if it is your
intention to compare temp2[0] with either 'c' or 'C'. What
you are doing is comparing the | (bit-or) of 'c' (0x63) and
'C' (0x43), which just happens to be 0x63 ('c')

That's only on ASCII-based systems...
against the
value in temp2[0]. In other words you'll only ever detect 'c'.

Actually, since '==' has "higher precedence" than '|', the expression
is executed as

( sort_file.peek(temp2[0]) == 'c' ) | 'C'

(first the comparison, then bitwise OR). The result can be either
'C', if 'peek' returns something different from 'c', or something
potentially different, depending on the _code_ used in the system.
temp2[0]) == 'c' || temp2[0]) == 'C'

is the expression you need.

You mean

sort_file.peek(temp2[0]) == 'c'
||
sort_file.peek(temp2[0]) == 'C'

, right?
 
L

lilburne

Victor said:
Actually, since '==' has "higher precedence" than '|', the expression
is executed as

( sort_file.peek(temp2[0]) == 'c' ) | 'C'

(first the comparison, then bitwise OR). The result can be either
'C', if 'peek' returns something different from 'c', or something
potentially different, depending on the _code_ used in the system.

temp2[0]) == 'c' || temp2[0]) == 'C'

is the expression you need.


You mean

sort_file.peek(temp2[0]) == 'c'
||
sort_file.peek(temp2[0]) == 'C'

, right?

Thank you. Not a very good post at all was it?
 
M

muser

lilburne said:
Victor said:
Actually, since '==' has "higher precedence" than '|', the expression
is executed as

( sort_file.peek(temp2[0]) == 'c' ) | 'C'

(first the comparison, then bitwise OR). The result can be either
'C', if 'peek' returns something different from 'c', or something
potentially different, depending on the _code_ used in the system.

temp2[0]) == 'c' || temp2[0]) == 'C'

is the expression you need.


You mean

sort_file.peek(temp2[0]) == 'c'
||
sort_file.peek(temp2[0]) == 'C'

, right?

Thank you. Not a very good post at all was it?

Thank you for your help, I was wondering if I put the above code into a function,
e.g void DetermineStruct( Allrecords unionarray ofstream& validdata )
{
union Allrecords *str_ptr1, *str_ptr2, tempstr;

if( validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) == 'C')
{
str_ptr1 = str_ptr1.Newcrecord.customercode, '\0';
}
if( validdata.peek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I')
//etc etc
str_ptr2 = str_ptr1 + 1;

if( validdata.peek(temp2[0]) == something, i think you get the gist of it

can I then write in the sort function.

for( int i=0; i< loop; i++)
while( strcmp( str_ptr.Determinefunction().customercode, '\0') );

can this be made to work if this isn't the correct way of going about it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

unions 2
std::sort whole program 2
Short program 3
function error 6
Access violation error 10
Crossword 2
Reading a file 1
changing char value 4

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top