structure : (another) problem

This is a discussion on structure : (another) problem within the C++ Programming forums, part of the General Programming Boards category; this does NOTHING , it opens and close immediatly Code: #include <iostream> #include <string> using namespace std; int main() { ...

  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
    21,941
    Your "leden" array has an off-by-one error, and your array loop index is uninitialised.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 01:53 PM

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