Thread: structure : (another) problem

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    70

    structure : (another) problem

    this does NOTHING , it opens and close immediatly
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        struct leden
        {
               string voornaam;
               string achternaam;
               };
               leden naam[5];
               naam[1].voornaam="Roel";
               naam[2].voornaam="ROEL2";
               naam[3].voornaam="Roe3";
               naam[4].voornaam="ROEL4";
               naam[5].voornaam="Roe5";
              
                         
               for (int namen; namen<=6; namen++) {
                   cout<<naam[namen].voornaam<<" "<<naam[namen].achternaam;
                   }
                   cin.get();
                   }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Your "leden" array has an off-by-one error, and your array loop index is uninitialised.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    mm i understand only the second thing (aargh stupid english lol)
    this become
    Code:
               for (int namen; namen<=5; namen++){ 
                   cout<<naam[namen].voornaam;
                   }
    and now what about that array???

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > for (int namen;
    What's the initial value here?
    Any random number > 6 (that's about half of all possible numbers an int will store) will be outside this range.

    The other half (all the negative ones) will cause all sorts of fun as you attempt to subscript your arrays with negative values!

    for (int namen = 0;

    Arrays run from 0 to N-1 for an array[N]
    Not 1 to N
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    k ,thnxxx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM