Using string as part of a structure variable

I'm working on a project, and I need your help.

I want to call the first part of a structure variable with a string.

For example, I would like:
1
2
3
4
5
6
7
8
9
struct person;
   int person.age = 0;
   int person.height = 0;
person john;
   john.age = 26;
   john.height = 70;
string name;
name = "john";
cout << name.height; 

to output John's height. What needs to be done for this to work?

Thank you in advance,

mynameinc
I feel stupid. Could you show me how to do that? I can't seem to get the pointers to work.
You can't do what you are suggesting like that. You would have to create something like an std::map and map the names to the people.

By the way, your structure code is completely wrong.
meh... store the name in the structure too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string> 

struct person
{
  std::string name;
  unsigned short age;
  unsigned short height;
};

int main() 
{
  person p1;
  p1.name = "John";
  p1.age = 26;
  p1.height = 70;
  std::cout << p1.name << " is " << p1.age << " years old, and " << p1.height << " inches tall."
    << std::endl;
  return 0;
}


~psault
Thanks for the help with structures and pointers; I learned it from a fellow amateur programmer.

mynameinc
Last edited on
Topic archived. No new replies allowed.