Confusion with assigning values to structs.

This is a discussion on Confusion with assigning values to structs. within the C++ Programming forums, part of the General Programming Boards category; For some reason....i'm having a bit of trouble getting input from the user and assigning that input to structure members... ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    220

    Confusion with assigning values to structs.

    For some reason....i'm having a bit of trouble getting input from the user and assigning that input to structure members...

    The error I get is:
    attempt to take.


    And here is my code for the struct:
    [code]
    struct MyBools
    {
    USHORT b1 : 1;
    USHORT b2 : 1;
    USHORT b3 : 1;
    USHORT b4 : 1;
    USHORT b5 : 1;
    USHORT b6 : 1;
    USHORT b7 : 1;
    USHORT b8 : 1;
    USHORT b9 : 1;
    USHORT b10 : 1;
    };
    [code]
    For future reference, USHORT is an unsigned short, I used typedef for that.

    And here is main():
    Code:
    int main()
    {
        MyBools bOne;
        cout << "Enter in 10 1's or 0's: ";
        cin >> bOne.b1 >> bOne.b2 >> bOne.b3 >> bOne.b4 >> bOne.b5 >> bOne.b6
            >> bOne.b7 >> bOne.b8 >> bOne.b9 >> bOne.b10;
        cout >> bOne.b1 >> bOne.b2 >> bOne.b3 >> bOne.b4 >> bOne.b5 >> bOne.b6
            >> bOne.b7 >> bOne.b8 >> bOne.b9 >> bOne.b10;
        getch();
    return 0;
    }
    Now from what I take it, I should be able to assign 1 or 0 to those members because there both 1 bit in size, but I don't get.....why it won't let that happen? Everything seems fine to me..
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    I have'nt compiled the code yet but just off hand I see that the output operator (<<) is backwards in the entire cout statement that outputs the structure.
    Last edited by Josh Norton; 03-21-2004 at 06:50 PM.
    Regards,
    ~Joshua Norton

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    In order to be able to store an input, cin must know the address of the target. A bit field does not have an address.

    You can use cin to read the characters into 10 variables, of type char or USHORT, or whatever, then assignment statements will use only the least significant bit.

    Code:
        cin >> s1 >> s2 >> s3 >> s4 >> s5 >> s6 >> s7 >> s8 >> s9 >> s10;
        bOne.b1 = s1;
        bOne.b2 = s2;
        bOne.b3 = s3;
        bOne.b4 = s4;
        bOne.b5 = s5;
        bOne.b6 = s6;
        bOne.b7 = s7;
        bOne.b8 = s8;
        bOne.b9 = s9;
        bOne.b10 = s10;
        cout << bOne.b1 << bOne.b2 << bOne.b3 << bOne.b4 << bOne.b5 << bOne.b6
            << bOne.b7 << bOne.b8 << bOne.b9 << bOne.b10;
        getchar();
    Dave

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    I dont know a lot about structs but, that's the first time I have seen a colon used in them. Is that an error or? If your trying to assign an initial value to each of them I don't think your allowed to do so in the structure declaration but, I could be wrong. Most people I have seen initialize the struct when they instantiate it like:

    Code:
    MyBools bOne = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    Regards,
    ~Joshua Norton

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    26
    Originally posted by Dave Evans
    In order to be able to store an input, cin must know the address of the target. A bit field does not have an address.
    hmm. Maybe incorrect but, it works fine as it's written in my compiler.

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    struct MyBools
    {
        unsigned short b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
    };
    
    int main()
    {
        MyBools bOne = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        cout << "Enter in 10 1's or 0's: ";
        cin >> bOne.b1 >> bOne.b2 >> bOne.b3 >> bOne.b4 >> bOne.b5 >> bOne.b6
            >> bOne.b7 >> bOne.b8 >> bOne.b9 >> bOne.b10;
        cout << bOne.b1 << bOne.b2 << bOne.b3 << bOne.b4 << bOne.b5 << bOne.b6
            << bOne.b7 << bOne.b8 << bOne.b9 << bOne.b10;
        getch();
        return 0;
    }
    edit: typo
    Regards,
    ~Joshua Norton

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Thank you Dave and Josh I appreciate your help.

    But sadly I have one more question...



    >In order to be able to store an input, cin must know the address of the target. A bit field does not have an address.

    Why doesn't a bitfield have an address?

    Also..

    >I dont know a lot about structs but, that's the first time I have seen a colon used in them.

    From what I know about bitfields, the ':' in structs designates a bit "size" of a variable. Thus bitfield. What follows the colon( is the size of the variable in bits.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by Tronic
    Thank you Dave and Josh I appreciate your help.

    But sadly I have one more question...



    >In order to be able to store an input, cin must know the address of the target. A bit field does not have an address.

    Why doesn't a bitfield have an address?

    From what I know about bitfields, the ':' in structs designates a bit "size" of a variable. Thus bitfield. What follows the colon( is the size of the variable in bits.
    A bit field is not a memory location. It defines a particular bit position (or positions if the width is >1) within a data item.

    The number after the ":" is the number of bits for that struct element. Successive bit fields are packed as much as possible into as few as possible of the indicated data type. (So all 10 of your struct elements fit into a single USHORT --- assuming USHORT is a 16-bit or longer quantity.)

    That's why people use bit fields: reduce storage requirements.

    Did your compiler give you an error message? What did it say?

    Dave

    Dave
    Last edited by Dave Evans; 03-21-2004 at 07:13 PM.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Ahh, thank you

    Well the error is fixed actually. I had fixed it before you posted the result but I had forgot to post that I had fixed it..got lost in the code lol.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by Josh Norton
    hmm. Maybe incorrect but, it works fine as it's written in my compiler.

    Code:
    struct MyBools
    {
        unsigned short b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
    };
    edit: typo
    But those aren't bit fields. His original post defined bit field elements of the struct.

    Dave

  10. #10
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    Ahh got it. Thanks.
    Regards,
    ~Joshua Norton

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with assignin values to an array of structs...
    By laczfinador in forum C Programming
    Replies: 2
    Last Post: 05-09-2009, 09:42 AM
  2. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  3. Replies: 2
    Last Post: 11-26-2008, 09:25 AM
  4. Question about assigning values
    By DCMann2 in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 07:36 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21