0

I am new to C++ so excuse my mistakes; I am trying to create a program that asks the user to input how many siblings do they have, and based on their input, they write down the sibling's Name Age Gender (separated by a space).

std::string name, age, gender;
int n;
std::vector<str::string> siblings;

std::cout << "How many siblings do you have? ";
std::cin >> n;

for (int a=0;a<n;a++){
    std::string x;
    std::cout << "Please enter info for sibling #" << (a+1) << ": ";
    std::cin >> name;
    std::cin >> age;
    std::cin >> gender;
    x += " " + name;
    x += " " + age;
    x += " " + gender;
    siblings.emplace_back(x)
}

Let's say that the user inputted "3" siblings with the information:

Michael 14 Male
Sam 20 Female
Anna 8 Female

How do I get access to the first sibling's age, second sibling's age, third sibling's age? Same with name? Gender?

I tried doing

std::cout << age

But that only gives me one result.

Kara
  • 765
  • 5
  • 11
  • 29

4 Answers4

3

Store the data in a struct or a class, for example:

struct PeopleStats
{
    std::string name, age, gender;
};

Then create a vector of objects of this class, instead of a vector of strings:

std::vector<PeopleStats> siblings;

Then your input loop, instead of jamming all the data together in a string, store it in one of these objects, then put that object in the vector.

for (int a=0;a<n;a++){
    PeopleStats x;
    std::cout << "Please enter info for sibling #" << (a+1) << ": ";
    std::cin >> x.name;
    std::cin >> x.age;
    std::cin >> x.gender;    
    siblings.emplace_back(x)
}

Then you can display, for example, everyone's age like this:

for (auto const & sibling : siblings)
    std::cout << sibling.age << '\n';
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

You need to create an array dynamically based on the input number 'n' and store each siblings details.

You can also use vector. Currently you are only using a string to obtain the age, name etc. Hence you see only one output.

Dinesh
  • 2,194
  • 3
  • 30
  • 52
1
std::vector<std::string> names, ages, genders;
..
for (int a=0;a<n;a++){
    std::string x;
    std::cout << "Please enter info for sibling #" << (a+1) << ": ";
    std::cin >> name;
    std::cin >> age;
    std::cin >> gender;
    names.push_back(name);
    ages.push_back(age);
    genders.push_back(gender);
}
std::cout << names[0] << " " << names[1] ... and so on
weisert
  • 185
  • 5
1

As you've discovered, you're overwriting the previous inputs on every iteration of the loop. So you need to save the data for each sibling individually, and you don't know how many siblings there will be to start with. This is an ideal use case for a vector. And let's create a struct named sibling that will hold the data for each sibling.

struct sibling
{
  std::string name, age, gender;
};

int n;
std::vector<sibling> siblings;

std::cout << "How many siblings do you have? ";
if((std::cin >> n) && (n >= 0)) { // always perform error checking
  siblings.reserve(n);            // not necessary, but pre-allocates storage
  for (int a=0;a<n;a++){
    sibling s;
    std::cout << "Please enter info for sibling #" << (a+1) << ": ";
    std::cin >> s.name;
    std::cin >> s.age;
    std::cin >> s.gender;

    // you should perform error checking for the inputs above too
    siblings.push_back(std::move(s));
  }
}
Praetorian
  • 106,671
  • 19
  • 240
  • 328