-
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?
-
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.
-
>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;
}
-
Well I really don't know what or how maps are used. That does work however, but I don't understand it.
-
maps (and other STL containers and algorithms) are an excellent idea for what to learn next.
-
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 :D. Check out the book Thinking in C++ by Bruce Eckel (google it) it is a free book and will teach you a lot.