By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

built in types

P: n/a
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[i].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[i].Newcrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.

for( i=0; i<loop; i++)
while(strcmp(str_ptr2[i].Newcrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1[i].Newirrecord.customercode,
str_ptr2[i].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[i].dreccusto, unionaarry[i] +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;

}
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
muser wrote:

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];'
Jul 22 '05 #2

P: n/a
"lilburne" <li******@godzilla.net> wrote...
muser wrote:

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?
[...]

Jul 22 '05 #3

P: n/a
Victor Bazarov wrote:
"lilburne" <li******@godzilla.net> wrote...
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?

Jul 22 '05 #4

P: n/a
lilburne <li******@godzilla.net> wrote in message news:<bq*************@ID-203936.news.uni-berlin.de>...
Victor Bazarov wrote:
"lilburne" <li******@godzilla.net> wrote...
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[i].Determinefunction().customercode, '\0') );

can this be made to work if this isn't the correct way of going about it?
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.