0

I have to make a table with a two dimensional array in c++. The array will need to hold two strings and two integers. Is it possible to have strings and integers in the same array? And how?

Please help ! I`m new to programming

3
  • 3
    Smells a lot like XY problem. What is the underlying problem you want to solve? Why can't you for instance use two distinct arrays? Commented Jan 6, 2016 at 23:21
  • Well, I have to make a 4x4 table. The first two columns will contain names and the last two will contain age numbers. Commented Jan 6, 2016 at 23:28
  • 1
    Consider using a struct instead of a vector to represent your columns. Commented Jan 6, 2016 at 23:58

1 Answer 1

4

This sounds like you probably want an array (or vector) of structures, where each structure contains a string and an int:

struct person {
    int age;
    std::string name;
};

std::vector<person> people(2);

In this case, you refer to the "rows" by number, and the "columns" by name, so the first string would be: people[0].name and the second integer would be people[1].age.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.