Now I want to expand on the code above. Instead of using a single number I want each element of the vector to hold multiple variables. I made up this code, which works, but I had to use a struct inside the class.
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class My_Class
{
public:
My_Class();
~My_Class();
void Add(int line_num);
void Get(vector<int>::iterator iter2);
vector<int> ref_list;
struct My_Struct
{
string name;
string address;
string city;
};
My_Struct Customer_Info;
private:
vector<My_Struct> customer_list;
};
int main()
{
My_Class Customer;
int line_num = 0;
int choice;
do
{
cout<<endl
<<"1 add" <<endl
<<"2 display" <<endl
<<"0 exit" <<endl <<endl
<<"choice : ";
cin>>choice; cin.ignore();
cout<<endl;
switch (choice)
{
case 1 :
cout<<"name : ";
getline(cin, Customer.Customer_Info.name);
cout<<"address : ";
getline(cin, Customer.Customer_Info.address);
cout<<"city : ";
getline(cin, Customer.Customer_Info.city);
Customer.Add(line_num);
++line_num;
break;
case 2 :
if (Customer.ref_list.size() != 0)
{
vector<int>::iterator iter;
int i = 1;
for (iter = Customer.ref_list.begin();
iter != Customer.ref_list.end();
++iter)
{
Customer.Get(iter);
cout<<i <<" " <<Customer.Customer_Info.name <<" "
<< Customer.Customer_Info.address <<" "
<< Customer.Customer_Info.city <<endl;
++i;
}
cout<<endl;
}
else
cout<<"no pick ups" <<endl <<endl;
break;
case 0 :
break;
default :
cout<<"invalid choice" <<endl <<endl;
}
}
while (choice != 0);
return 0;
}
My_Class::My_Class()
{
}
My_Class::~My_Class()
{
}
void My_Class::Add(int line_num)
{
customer_list.push_back(Customer_Info);
ref_list.push_back(line_num);
return;
}
void My_Class::Get(vector<int>::iterator iter2)
{
vector<My_Struct>::iterator iter1;
iter1 = customer_list.begin() + *iter2;
Customer_Info = *iter1;
return;
}
I know there is a way of doing this without using the struct but when I took it out like and did the class like this
Code:
class My_Class
{
public:
My_Class();
~My_Class();
void Add(int line_num);
void Get(vector<int>::iterator iter2);
vector<int> ref_list;
string name;
string address;
string city;
private:
vector<My_Struct> customer_list;
};
all I could get were segmentation errors at run time. I am sure that there is something simple that I am doing, I just can't see it.