Skip to content

Instantly share code, notes, and snippets.

@lnrsoft
Last active January 21, 2017 21:38
Show Gist options
  • Save lnrsoft/e9f0d8a67d7cb622f896 to your computer and use it in GitHub Desktop.
Save lnrsoft/e9f0d8a67d7cb622f896 to your computer and use it in GitHub Desktop.
[C++] Store different types in one single vector
// This source code written by Roland Ihasz
#include <iostream>
#include <vector>
#include <string>
class User_data
{
public:
User_data(int i, std::string fn, std::string sn, std::string d, double w)
: id(i), fname(fn), sname(sn), dob(d), weight(w) { }
int id;
std::string fname;
std::string sname;
std::string dob;
double weight;
};
int main()
{
std::vector<User_data> single_vector;
int n, id_number;
std::string firtsname;
std::string surname;
std::string dateofbirth;
double weight;
std::cout << "How many records you want to create?: ";
std::cin >> n;
for(auto j = 0; j < n; ++j)
{
std::cout << "Record Number: " << j + 1 << std::endl;
std::cout << "ID: ";
std::cin >> id_number;
std::cout << "Firts Name: ";
std::cin >> firtsname;
std::cout << "Surname: ";
std::cin >> surname;
std::cout << "Date of Birth (MM/DD/YYYY): ";
std::cin >> dateofbirth;
std::cout << "Weight: ";
std::cin >> weight;
std::cout << std::endl;
single_vector.push_back(User_data(id_number, firtsname, surname, dateofbirth, weight));
}
std::cout << "Listing..." << std::endl;
for(unsigned int i = 0; i < single_vector.size(); ++i)
{
std::cout << single_vector[i].id << " " << single_vector[i].fname << " "
<< single_vector[i].sname << " " << single_vector[i].dob << " "
<< single_vector[i].weight << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment