Thread: Struct Problem

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Struct Problem

    Code:
    #include <iostream> 
    using namespace std; 
    
    int main() 
    
    { 
    
    int index; 
    
    struct DOB{ 
    int day; 
    int month; 
    int year; 
    }; 
    
    DOB date[9]; 
    
    for (index = 0; index < 1; index ++) 
    { 
    cout << endl << "please enter the day "; 
    cin >> date[index].day; 
    cout << endl << "please enter the month "; 
    cin >> date[index].month; 
    cout << endl << "please enter the year "; 
    cin >> date[index].year; 
    } 
    
    cout << date[1]; 
    
    }
    I'm fairly new to using structs so bare with me. From what i've read this seems ok, but when it's run it says there is an error with the struct.
    I realise the values of the array are wrong at the moment but that's just until I get the actual code working.

    Can anybody help me?

    Thanks.

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    main should return 0.

    Code:
    cout << date[1];
    The streams library doesn't know how to output a struct DOB, you need to tell it how. e.g. write an operator<< such as

    Code:
    #include <iostream> 
    using namespace std; 
    
    struct DOB 
    { 
        int day; 
        int month; 
        int year; 
    }; 
    
    ostream& operator<<(ostream& os, const DOB& dob);
    
    int main() 
    { 
        int index; 
        DOB date[9]; 
    
        for (index = 0; index < 1; index ++) 
        { 
            cout << endl << "please enter the day "; 
            cin >> date[index].day; 
            cout << endl << "please enter the month "; 
            cin >> date[index].month; 
            cout << endl << "please enter the year "; 
            cin >> date[index].year; 
        } 
    
        cout << date[0]; 
    
        return(0);
    } 
    
    ostream& operator<<(ostream& os, const DOB& dob)
    {
        os << dob.day << "/" << dob.month << "/" << dob.year << endl;
        return(os);
    }

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for (index = 0; index < 1; index ++) 
    { 
    ...
    } 
    
    cout << date[1];
    index was in 0 to 0 range
    date[1] is not initialized
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> main should return 0.
    Why? It isn't necessary.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    Quote Originally Posted by Daved
    >> main should return 0.
    Why? It isn't necessary.
    If he is in school then marks will likely be deducted

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If he is in school then marks will likely be deducted
    That's debatable, and completely irrelevant.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If he is in school then marks will likely be deducted
    If marks will be deducted then of course it should be included. But since leaving it out is still completely standard and correct code, then perhaps such an instructor should re-think the grading guidelines.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    for a beginner, instead of creating overloaded operators it might be easier to understand to just use the normal struct way


    Code:
    cout << date[0].day << ' ' << date[0].year; // etc

  9. #9
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45
    >> main should return 0.
    Why? It isn't necessary.

    >>If he is in school then marks will likely be deducted
    That's debatable, and completely irrelevant.
    I haven't met a Prof that took points off due to this. I believe for a beginner, it's good to include it in code to grasp the concept of functions and the concept of them returning values whatever its value.

    Maniac, there is nothing wrong with the struct, but with the way in which you're trying to output its members values as others have already demonstrated:
    Code:
    #include <iostream> 
    using namespace std; 
    
    int main() 
    { 
      int index; 
    
      struct DOB { 
        int day; 
        int month; 
        int year; 
      }; 
    
      DOB date[9]; 
    
      for ( index = 0; index < 1; index++ ) { 
        cout << endl << "please enter the day "; 
        cin >> date[ index ].day; 
        cout << endl << "please enter the month "; 
        cin >> date[ index ].month; 
        cout << endl << "please enter the year "; 
        cin >> date[ index ].year; 
      } 
    
      cout << date[ 0 ].day << " " << date[ 0 ].month << " " << date[ 0 ].year << endl; 
    
      return 0;
    }
    Last edited by CaptainMorgan; 11-03-2006 at 01:17 PM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Quote Originally Posted by CaptainMorgan
    I haven't met a Prof that took points off due to this. I believe for a beginner, it's good to include it in code to grasp the concept of functions and the concept of them returning values whatever its value.

    Maniac, there is nothing wrong with the struct, but with the way in which you're trying to output its members values as others have already demonstrated:
    Code:
    #include <iostream> 
    using namespace std; 
    
    int main() 
    { 
      int index; 
    
      struct DOB { 
        int day; 
        int month; 
        int year; 
      }; 
    
      DOB date[9]; 
    
      for ( index = 0; index < 1; index++ ) { 
        cout << endl << "please enter the day "; 
        cin >> date[ index ].day; 
        cout << endl << "please enter the month "; 
        cin >> date[ index ].month; 
        cout << endl << "please enter the year "; 
        cin >> date[ index ].year; 
      } 
    
      cout << date[ 0 ].day << " " << date[ 0 ].month << " " << date[ 0 ].year << endl; 
    
      return 0;
    }

    Cheers for that! I see were I was going wrong now.

    I do know you're meant to put return 0, but like it's been said, it's not essential so I left it out here.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Not that it matters, but structs and classes are usually delcared outside the main function

    Code:
    struct Foo
    {
       int x;
    };
    
    int main ( void )
    {
       Foo fo;
    
       fo.x = 5;
    
       cout << "value of x: " << fo.x << endl;
    
       return 0;
    }
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Replies: 16
    Last Post: 10-29-2006, 05:04 AM