built in types

Discussion in 'C++' started by muser, Nov 26, 2003.

  1. muser

    muser Guest

    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;

    }
    muser, Nov 26, 2003
    #1
    1. Advertising

  2. muser

    lilburne Guest

    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];'
    lilburne, Nov 26, 2003
    #2
    1. Advertising

  3. "lilburne" <> 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?

    > [...]
    Victor Bazarov, Nov 26, 2003
    #3
  4. muser

    lilburne Guest

    Victor Bazarov wrote:

    > "lilburne" <> 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?
    lilburne, Nov 26, 2003
    #4
  5. muser

    muser Guest

    lilburne <> wrote in message news:<bq39s8$1u8faf$-berlin.de>...
    > Victor Bazarov wrote:
    >
    > > "lilburne" <> 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.Determinefunction().customercode, '\0') );

    can this be made to work if this isn't the correct way of going about it?
    muser, Nov 27, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky
    Replies:
    13
    Views:
    626
    Richard Herring
    Jun 29, 2004
  2. Howard
    Replies:
    3
    Views:
    440
    Siemel Naran
    Jul 22, 2004
  3. ma740988
    Replies:
    6
    Views:
    411
    ma740988
    Aug 26, 2004
  4. Pierre Senellart

    "Default constructor" for built-in types

    Pierre Senellart, Jan 25, 2005, in forum: C++
    Replies:
    9
    Views:
    4,303
    Ron Natalie
    Jan 27, 2005
  5. Alona
    Replies:
    2
    Views:
    647
Loading...

Share This Page