Whats wrong with my vector usage?

This is a discussion on Whats wrong with my vector usage? within the C++ Programming forums, part of the General Programming Boards category; It prints out the first three names but the program freezes/stop when I resize it. Whats up? Code: #include <iostream> ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    57

    Whats wrong with my vector usage?

    It prints out the first three names but the program freezes/stop when I resize it. Whats up?

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    typedef struct _Data
    {
     string Name;
     int Age;
    }Data;
    
    int main(int argc, char *argv[])
    {
     vector<Data> Access(3);
     Access[0].Name= "John";
     Access[1].Name= "Abraham";
     Access[2].Name= "Jamal";
     int i;
    
     for(i= 0; i< Access.size(); i++)
     {
      Access[i].Age= 14 + i;
     }
    
     for(i= 0; i< Access.size(); i++)
     {
      cout << Access[i].Name << "\t\t" << Access[i].Age << endl;
     }
    
     Access.resize(5);
     Access[4].Name= "Sean";
     Access[5].Name= "James";
    
     for(i= 4; i< Access.size(); i++)
     {
      Access[i].Age= 13 + i;
     }
    
     for(i= 4; i< Access.size(); i++)
     {
      cout << Access[i].Name << "\t\t" << Access[i].Age << endl;
     }
    
     cin.get();
     return 0;
    }
    "A Programmer being told to 'go to' hell sees the 'go to' part of the sentence as the worst part." - Master Foto

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    A vector of size n has addressable elements 0 to n-1.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    57
    ok..........anyways, does anyone know why my program stops when it resizes the vector?
    "A Programmer being told to 'go to' hell sees the 'go to' part of the sentence as the worst part." - Master Foto

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    I just told you...

    It should be stopping just after you resize the vector, when you assign a value to the element with index 5.

    When you resize the vector to length 5(that would be n), that means you can address values from 0 to 4 (n-1). But not 5.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    57
    ok, I get it now
    "A Programmer being told to 'go to' hell sees the 'go to' part of the sentence as the worst part." - Master Foto

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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