Thread: Indexing a string

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    Indexing a string

    I was making a little thing, just learning some stuff, and I came up with this...

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct datacore {
      int id_number;
      int age;
      int securitycode;
    };
    
    int main()
    {
      string name;
      datacore bob,charlie,george;
    
      bob.id_number = 338;
      bob.age = 27;
      bob.securitycode=1229;
    
      charlie.id_number = 890;
      charlie.age = 39;
      charlie.securitycode=8894;
    
      george.id_number = 115;
      george.age = 70;
      george.securitycode = 1126;
    
      cout<<"Enter an employee name to access information about them";
      cin>>name;
      cin.ignore();
    
         cout<<[name].id_number<<"\n"<<[name].age<<"\n"<<[name].securitycode;
    
      cin.get();
      return 1;
    
    }

    I wanted it to index whatever name was entered and use that id_number,age, and securitycode. But It doesn't work, it just gives an error, it is just calling it a parse error. So how do you index a string?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Name should not be in brackets first of all.

    Second of all, the names of variables are not avalable at runtime. If you want to remmember the person each datacore represents, make datacore contain a string storing that person's name. Then you'll have to do the actual indexing. You have to search through all the datacores to find the one who's name matches the user-entered name.

    I'm not sure what the best way for you to learn to do this is. You need to learn how to use arrays, pointers, and loops, but you also need to learn how to use STL containers, itterators, and algorithims.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I wanted it to index whatever name was entered and use that id_number,age, and securitycode.
    How about a map?
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    struct datacore {
      int id_number;
      int age;
      int securitycode;
    };
    
    int main()
    {
      string name;
      datacore bob,charlie,george;
      map<string,datacore> Map;
    
      bob.id_number = 338;
      bob.age = 27;
      bob.securitycode=1229;
      Map["bob"] = bob;
    
      charlie.id_number = 890;
      charlie.age = 39;
      charlie.securitycode=8894;
      Map["charlie"] = charlie;
    
      george.id_number = 115;
      george.age = 70;
      george.securitycode = 1126;
      Map["george"] = george;
    
      cout<<"Enter an employee name to access information about them: ";
      cin>>name;
      cin.ignore(80, '\n');
    
      if (Map.find(name) != Map.end())
      {
         cout<<Map[name].id_number<<"\n"<<Map[name].age<<"\n"<<Map[name].securitycode;
      }
    
      cin.get();
      return 0;
    }

  4. #4
    Me
    Join Date
    Jul 2006
    Posts
    71
    Well I really don't know what or how maps are used. That does work however, but I don't understand it.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    maps (and other STL containers and algorithms) are an excellent idea for what to learn next.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    What to learn next is key there... relyt_123 is treating this like it is a scripting language. Perhaps you could change that to use an array of type 'datacore' then ask what element the person wants to look at.

    Code:
    struct datacore {
      string name;
      int id_number;
      int age;
      int securitycode;
    };
    
    datacore people[3];
    
    people[0].name = "Bob";
    people[0].id_number = 338;
    people[0].age = 27;
    people[0].securitycode=1229;
    
    
    people[1].name = "Charlie";
    people[1].id_number = 890;
    people[1].age = 39;
    people[1].securitycode=8894;
    
    
    people[2].name = "George";
    people[2].id_number = 115;
    people[2].age = 70;
    people[2].securitycode = 1126;
    Once you can do that, you will be a bit better off . Check out the book Thinking in C++ by Bruce Eckel (google it) it is a free book and will teach you a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM