List inside a List...?

How do I manage to create a new list inside an exist list and keep the ability to get to the secound list threu the first list...?
I want to use the <list> class...

thanks a lot...
um std::list<std::list<obj> > l?
I'm afraid no ...

I don't understand the <list> idea at all :(
1
2
3
4
5
6
7
8
9
10
std::list<std::list<obj> > first_list;
std::list<obj> second_list;

obj o1;
second_list.push_back(o1); // store a copy of the obj in the second list
first_list.push_back(second_list); // store a copy of the second list in the first list

obj o2
first_list.back().push_back(o2); // with 'first_list.back()' you access the (last added) second list
// and with '.push_back(o2)' you store a second obj in the second list 
does that help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <ostream>
#include <istream>
#include "GasStation.h"
#include <fstream>
#include <string>
#include <sstream>
#include <list>
using namespace std;

int main (int argc, char* argv[] )
{
	/*cout<<argc<<endl;*/

	list<Gas_station> lst; // Creates list // Creates list of argc-2 copies of Gas_station() the -2 is because first file is the exe file secound is the simulation and after comes the Stations informaitions. 
	for( int counter=0,counter_arg_v=1 ; counter< argc-1; counter++,counter_arg_v++ )
	{
		Gas_station Station1;
		lst.push_front(Station1);

		//list<T> lst(n, e); // Creates list of n copies of e. 

		char nameafter[40];

		string str98 ("Octan98");
		string str95 ("Octan95");
		string line  ;
		ifstream myfile (argv[counter_arg_v]); // open the txt file
		if (myfile.is_open())
		{
			while ( myfile.good() )//scan each row and do the right commands untill the end of the file
			{
				getline (myfile,line);
				if (line[0]!='#')
				{
					if(line[0]=='S') // to get the station name
					{
						std::string y;
						const std::string::size_type pos = line.find(":"); // after :
						if(pos != std::string::npos)
							y = line.substr(pos + 1);
						strcpy(nameafter,y.c_str());
						std::cout<<nameafter<<endl<<endl; // the name of the station got from file
						Station1.set_m_station_name(nameafter);//copy the name of the station from the txt file into the object		
					}

					else if(line[0]=='O')  //    Octan cheak ang get values of price
					{
						std::string part2;
						std::stringstream ss2(line);
						std::getline(ss2, part2, ',');
						if(part2.compare(str98)==0)
						{
							std::cout<<"Using "<<str98<<endl;
							getline(ss2,part2, ',');
							std::cout<<"Full service"<< part2<<endl;
							Station1.set_mFull_price(atof(part2.c_str())); //convert the string from string to int and assing

							getline(ss2,part2, ',');
							std::cout<<"Self service "<<part2<<endl<<endl;
							Station1.set_mSelf_price(atof(part2.c_str()));//convert the string from string to int and assing

						}
						else if(part2.compare(str95)==0)
						{
							std::cout<<"Using "<<str95<<endl;
							getline(ss2,part2, ',');
							std::cout<<"Full service"<< part2<<endl;
							Station1.set_m_Full_price(atof(part2.c_str()));//convert the string from string to int and assing

							getline(ss2,part2, ',');
							std::cout<<"Self service "<<part2<<endl<<endl;
							Station1.set_m_Self_price(atof(part2.c_str()));//convert the string from string to int and assing
						}
						else
						{
							std::cout<<"You enter a wrong fild of Fule , pleas try again"<<endl;
							exit(1);
						}
					}
					else //fullers list
					{
						list<FULER_INFO> lst2; //list of fulers in the station

						lst2.push_front(Station1.FULER_info);

						std::string part2;
						std::stringstream ss2(line);


						std::getline(ss2, part2, ',');
						std::cout<<"The car number is "<< part2<<endl;
						strcpy(Station1.FULER_info.car_id,part2.c_str());// copy the car id to fild

						std::getline(ss2, part2, ','); //copy the fuling kind 95/98
						std::cout<<"The fuling kind is "<< part2<<endl;
						if(part2[0]=='9')
						{
							if(part2[1]=='5')
								Station1.FULER_info.Octan_type=octan95;
						}
						else  if(part2[0]=='9')
						{
							if ((part2[1])=='8')
								Station1.FULER_info.Octan_type=octan98;
						}
						else
						{
							cerr<<"You enterd wrong value of fuel"<<endl;
							exit(1);
						}

						std::getline(ss2, part2, ','); // copy the number of liters feuled
						std::cout<<"The number of liters is "<< part2<<endl;
						Station1.FULER_info.num_of_liteRs=atof(part2.c_str());

						std::getline(ss2, part2, ',');
						std::cout<<"The fuling type is "<< part2<<endl<<endl;
						if(part2[0]=='F') //means full service is used
							Station1.FULER_info.SERVICE_TYPE_=FullService;
						else if (part2[0]=='S')//means self service is used
							Station1.FULER_info.SERVICE_TYPE_=SelfService;
						else
						{
							cout<<"You enter wrong service type"<<endl;
							exit(1);
						}

					} //else //fullers list
				}  //if (line[0]!='#')
			}//while ( myfile.good() )

		} //if (myfile.is_open())
		else std::cout << "Unable to open file"<<endl; 

		myfile.close();

		std::cout<<lst.size()<<endl;


	}//for(...)
	return 0;
}//main()  


this is my code the 2 last problams is

1. I need to make a list of Station object's
2.I need to make a list of persons used the specific station in the object of the Station

my classes are

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Gas_station
{
private:

	char m_station_name [40];
	double m_Full_price95;
	double m_Self_price95;
	double m_Full_price98;
	double m_Self_price98;
	

public:
	Gas_station(char *name,int full_price,int self_price ); //constructor
	Gas_station();//default constructor
	void set_m_station_name(char* station_name); //set the name of the station from txt file to obj
	void set_m_Full_price(double priceF95);//set the price for full price 95
	void set_m_Self_price(double priceS95);//set the price for self price 95
	void set_mFull_price(double priceF98);//set the price for full price 98
	void set_mSelf_price(double priceS98);//set the price for self price 98
	FULER_INFO FULER_info;//conection to save the fulers
	void PrintStation() const ;
};

class Person_using_the_Gas_station
{
private:

	char m_car_number [40];
	Fulle_type m_Fulle_type; //enum type
	double m_Amount_of_Fuel_the_person_fill;
	Service_type m_Service_type; //enum type

public:
	Person_using_the_Gas_station( char *name , Fulle_type full_type , int litters_fill , Service_type serv_type ); //constructor
}; 


any help will be great... I'm really stuck
Where's the problem? Look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Person_using_the_Gas_station
{
private:

    char m_car_number [40];
    Fulle_type m_Fulle_type; //enum type
    double m_Amount_of_Fuel_the_person_fill;
    Service_type m_Service_type; //enum type

public:
    Person_using_the_Gas_station( char *name , Fulle_type full_type , int litters_fill , Service_type serv_type ); //constructor
};

class Gas_station
{
private:

    char m_station_name [40];
    double m_Full_price95;
    double m_Self_price95;
    double m_Full_price98;
    double m_Self_price98;
    std::list<Person_using_the_Gas_station> m_PersonList;
    

public:
    Gas_station(char *name,int full_price,int self_price ); //constructor
    Gas_station();//default constructor
    void set_m_station_name(char* station_name); //set the name of the station from txt file to obj
    void set_m_Full_price(double priceF95);//set the price for full price 95
    void set_m_Self_price(double priceS95);//set the price for self price 95
    void set_mFull_price(double priceF98);//set the price for full price 98
    void set_mSelf_price(double priceS98);//set the price for self price 98
    FULER_INFO FULER_info;//conection to save the fulers
    void PrintStation() const ;
};


This
1
2
        Gas_station Station1;
        lst.push_front(Station1);
is more a problem since you can't use Station1 the way you do. Just like this:
1
2
        lst.push_front(Gas_station()); // add a gas station to the list
        Gas_station &Station1 = lst.front(); // NOTE the '&' 
so what I need 2 change is
1.
1
2
3
4
5
6
7
8
9
10
11
class Gas_station
{
private:

    char m_station_name [40];
    double m_Full_price95;
    double m_Self_price95;
    double m_Full_price98;
    double m_Self_price98;
    std::list<Person_using_the_Gas_station> m_PersonList; // <<========this
 


2. and insted
1
2
3
4
5
Gas_station Station1;
        lst.push_front(Station1);
 

 


do
1
2
3
        lst.push_front(Gas_station()); // add a gas station to the list
        Gas_station &Station1 = lst.front(); // NOTE the '&' 
 


is it what your advice?
is it what your advice?
well, yes
so it's like this???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 #include <iostream>
#include <ostream>
#include <istream>
#include "GasStation.h"
#include <fstream>
#include <string>
#include <sstream>
#include <list>
using namespace std;

int main (int argc, char* argv[] )
{
	/*cout<<argc<<endl;*/

	list<Gas_station> lst; // Creates list // Creates list of argc-2 copies of Gas_station() the -2 is because first file is the exe file secound is the simulation and after comes the Stations informaitions. 
	for( int counter=0,counter_arg_v=1 ; counter< argc-1; counter++,counter_arg_v++ )
	{
		lst.push_front(Gas_station()); // add a gas station to the list
        Gas_station &Station1 = lst.front(); // NOTE the '&' 

	/*	Gas_station Station1;
		lst.push_front(Station1);*/

		//list<T> lst(n, e); // Creates list of n copies of e. 

		char nameafter[40];

		string str98 ("Octan98");
		string str95 ("Octan95");
		string line  ;
		ifstream myfile (argv[counter_arg_v]); // open the txt file
		if (myfile.is_open())
		{
			while ( myfile.good() )//scan each row and do the right commands untill the end of the file
			{
				getline (myfile,line);
				if (line[0]!='#')
				{
					if(line[0]=='S') // to get the station name
					{
						std::string y;
						const std::string::size_type pos = line.find(":"); // after :
						if(pos != std::string::npos)
							y = line.substr(pos + 1);
						strcpy(nameafter,y.c_str());
						std::cout<<nameafter<<endl<<endl; // the name of the station got from file
						Station1.set_m_station_name(nameafter);//copy the name of the station from the txt file into the object		
					}

					else if(line[0]=='O')  //    Octan cheak ang get values of price
					{
						std::string part2;
						std::stringstream ss2(line);
						std::getline(ss2, part2, ',');
						if(part2.compare(str98)==0)
						{
							std::cout<<"Using "<<str98<<endl;
							getline(ss2,part2, ',');
							std::cout<<"Full service"<< part2<<endl;
							Station1.set_mFull_price(atof(part2.c_str())); //convert the string from string to int and assing

							getline(ss2,part2, ',');
							std::cout<<"Self service "<<part2<<endl<<endl;
							Station1.set_mSelf_price(atof(part2.c_str()));//convert the string from string to int and assing

						}
						else if(part2.compare(str95)==0)
						{
							std::cout<<"Using "<<str95<<endl;
							getline(ss2,part2, ',');
							std::cout<<"Full service"<< part2<<endl;
							Station1.set_m_Full_price(atof(part2.c_str()));//convert the string from string to int and assing

							getline(ss2,part2, ',');
							std::cout<<"Self service "<<part2<<endl<<endl;
							Station1.set_m_Self_price(atof(part2.c_str()));//convert the string from string to int and assing
						}
						else
						{
							std::cout<<"You enter a wrong fild of Fule , pleas try again"<<endl;
							exit(1);
						}
					}
					else //fullers list
					{
						list<FULER_INFO> lst2; //list of fulers in the station

						lst2.push_front(Station1.FULER_info);

						std::string part2;
						std::stringstream ss2(line);


						std::getline(ss2, part2, ',');
						std::cout<<"The car number is "<< part2<<endl;
						strcpy(Station1.FULER_info.car_id,part2.c_str());// copy the car id to fild

						std::getline(ss2, part2, ','); //copy the fuling kind 95/98
						std::cout<<"The fuling kind is "<< part2<<endl;
						if(part2[0]=='9')
						{
							if(part2[1]=='5')
								Station1.FULER_info.Octan_type=octan95;
						}
						else  if(part2[0]=='9')
						{
							if ((part2[1])=='8')
								Station1.FULER_info.Octan_type=octan98;
						}
						else
						{
							cerr<<"You enterd wrong value of fuel"<<endl;
							exit(1);
						}

						std::getline(ss2, part2, ','); // copy the number of liters feuled
						std::cout<<"The number of liters is "<< part2<<endl;
						Station1.FULER_info.num_of_liteRs=atof(part2.c_str());

						std::getline(ss2, part2, ',');
						std::cout<<"The fuling type is "<< part2<<endl<<endl;
						if(part2[0]=='F') //means full service is used
							Station1.FULER_info.SERVICE_TYPE_=FullService;
						else if (part2[0]=='S')//means self service is used
							Station1.FULER_info.SERVICE_TYPE_=SelfService;
						else
						{
							cout<<"You enter wrong service type"<<endl;
							exit(1);
						}

					} //else //fullers list
				}  //if (line[0]!='#')
			}//while ( myfile.good() )

		} //if (myfile.is_open())
		else std::cout << "Unable to open file"<<endl; 

		myfile.close();

		std::cout<<lst.size()<<endl;


	}//for(...)
	return 0;
}//main()  


header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 #ifndef GasStation_H
#define GasStation_H
#include <iostream>
#include <list>
#include <stdlib.h>
#include <string.h>

typedef enum fulle_type {octan95,octan98} Fulle_type;
typedef enum  service_type{FullService,SelfService} Service_type ;

class FULER_INFO{ // class to chain the fulers used the station
public:
	char car_id [20];
	Fulle_type Octan_type;
	double num_of_liteRs;
	Service_type SERVICE_TYPE_;
	FULER_INFO* NEXT;
	FULER_INFO() {}; //default constructor
};

class Gas_station
{
private:

	char m_station_name [40];
	double m_Full_price95;
	double m_Self_price95;
	double m_Full_price98;
	double m_Self_price98;
	

public:
	Gas_station(char *name,int full_price,int self_price ); //constructor
	Gas_station();//default constructor
	void set_m_station_name(char* station_name); //set the name of the station from txt file to obj
	void set_m_Full_price(double priceF95);//set the price for full price 95
	void set_m_Self_price(double priceS95);//set the price for self price 95
	void set_mFull_price(double priceF98);//set the price for full price 98
	void set_mSelf_price(double priceS98);//set the price for self price 98
	FULER_INFO FULER_info;//conection to save the fulers
	std::list<FULER_INFO> m_PersonList;
	 
	void PrintStation() const ;
};

is it will save also the stations and also the list of persons that used the station ?
um, where's the good old 'Person_using_the_Gas_station' gone?

you don't need the lines 86 and 88 anymore.

Generally: initialize your member variables in your constructor.

I'd recommend a more object orientd approach: Introduce a function for each class that gets the stream and picks the data it needs (and maybe later write it back)
Topic archived. No new replies allowed.