Dynamic Multitype Array

Hello!

I'm trying to make a simple array that will increase its column size dynamicly and it will have 3 rows.
On the 1st row i want the user to enter a name(aka char type)
and on the other two rows i want the user to enter some float values which refer to the name of the 1st row.

the result i want to be like this

 Jack  Tom     Helen  .....  Jenny
 1.3   3      523.2  .....   23.1
  2.5  144.3   10000  .....   12.1


any suggestions?

Thank you in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct namesAndFloats
{
  string name;
  float value1;
  float value2;
};

vector<namesAndFloats> allTheObjects;

namesAndFloats a,b,c;

a.name = Jack;
a.value1=1.3;
a.value2=2.5;

allTheObjects.push_back(a);

// etc for b and c and all the other. 
closed account (3hM2Nwbp)
Another way to skin your cat could be std::tuple if you're using a compatible compiler.

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

typedef std::tuple<std::string, float, float> Object_Type;

std::vector<Object_Type> objects;

int main()
{
  objects.push_back(std::make_tuple("Jack", 1.3, 2.5));
  std::cout <<
  "String: " << std::get<0>(objects[0]) << '\n' <<
  "Float1: " << std::get<1>(objects[0]) << '\n' <<
  "Float2: " << std::get<2>(objects[0]) << std::endl;
  return 0;
}
Last edited on
Topic archived. No new replies allowed.