Linked List inside another Linked List

Ok, since I had trouble implementing and populating my created lists (list within a list) and since I didn't find any valuable information on the web, I decided, after some hours of sheer struggling, to register an account here and ask for some help before I go on and vent off on my keyboard again.

This is a lab assignment for my Data Strustures course as you will no doubt figure out as you go on reading. Or maybe you've figured it out already. Anyway...

I created a list of accounts (of type Accounts) which holds an account_id as well as a list of Bills (class).

1
2
3
4
5
6
class Accounts
{
public:
	int account_id;
	list<Bills> bills;
};


1
2
3
4
5
6
7
class Bills
{
public:
	int billing_id;
	double debt;
	int days_left;
};


In the main section, I went on and declared the list with the name "accounts".

list<Accounts> accounts;

If I am not mistaken, "accounts" is now a list and contains an "account_id"(int) as well as a list of Bills which by itself holds three more variables : "billing_id(int)", "debt"(double) and "days_left"(int).

"accounts"
__________________
| account_id |
| _____________ |
| | billing_id | |
| | debt | |
| | days_left | |
| |____________ | |
|__________________|

Note that "accounts" can hold information about many accounts and each account can hold many bills. That in mind, I decided to create two iterators. The first iterator, "a", will point to the beginning of the Account list and the second one, b", pointing to the beginning of the Bills list.

list<Accounts>::iterator a = accounts.begin();

list<Bills>::iterator b = a->bills.begin();

So far so good and I went on to the populating part.
Among other things, the program needs to check if an account_id already exists. If not, the program will need to create a new Account block and populate it accordingly. Here lies the problem. Although I create a new block to store the account information :

Accounts* new_account = new Accounts;

new_account->account_id = account_id;

I can't find a way to link a new bill block inside that account and populate that as well.

1
2
3
4
5
Bills* new_bill = new Bills;
new_bill->billing_id = billing_id;
new_bill->days_left = days_left;
new_bill->debt = debt;
//link to the created account?? 


I find it to be a little weird that I can't guide the newly created "new_account" to point to the desired values that are to be populated, either via the "new_account" direclty or via the Accounts:iterator "a".
Several of my tries failed to work:

a->bills->account_id; (error : expression must have pointer type)

new_account->bills->account_id; (same error)

new_account->bills = new_bill; (error : no operator matches these operands).

My question is, how can I create a new block inside a newly created block? In other words, how can I link those blocks together?

My second question is more like a "yes or no" one.
With this code :

1
2
3
4
5
6
7
8
9
10
while(a!=accounts.end())
  {
    if(a->account_id==account_id)
    {
      // check if an existing billing_id exists
      // create a new bill and populate it and bla bla bla
         break;
    }
    a++;
  }


If I break the while loop, the iterator points to the account block on which there was a match? If so, if I want to perform many searches inside the lists per program run, do I need to reset the iterator to the beginning of the list?

Thanks for reading my long post... (sigh)...
Last edited on
Yes, you have several points of confusion present here.

You should not be using new for anything here. You are working with lists of objects here, not lists of object pointers.
The objects will be allocated, via the mechanism for doing so employed by the list class, on push_back(). You want to prepare a local instance of object for push_back to copy from.

Addressing the errors:
a->bills->account_id; (error : expression must have pointer type)
What type is a->bills? It is a list<Bills>, not a int*, so it can't point to an int, such as account_id.

new_account->bills = new_bill; (error : no operator matches these operands).

Again, account->bills is type list<Bills> and new_bill is a Bill*.

The idea you have of using iterators to different types for populating lists of different types should work.

I was about to work it out here, but it's your lab.
See if correcting the errors you have helps you to see the rest more clearly.

EDIT: I have fully worked out the method which it seems you are trying for here, and I don't recommend using it.

Here's why:
It looks like you are developing a method for adding a new Acconts to accounts by:
1) push_back a default constructed Accounts object.
accounts.push_back( Accounts() );
2) Obtain an iterator to the element just pushed back:
1
2
list<Accounts>::iterator a = accounts.end();
--a;// now a "points" to the back element (the one just pushed back). 

3) a->account_id = account_id;// and so on

You are initializing the new Accounts object via an iterator into a list you just pushed it into.

The approach I have been taught is to prepare a fully initialized instance of an Accounts object (locally), then push it back into the accounts list.

Last edited on
Thanks for the quick response. Indeed, it's my lab. I mentioned it in advance because I litterally hate served solutions. Thanks for not implementing actual code here. Now, back to the issue at hand...

I guess new blocks need to be created for example when, no bill block exists for a given account or when the list is completely empty. Iterators don't seem to work with the functions included in the <list> library such as push_back(). For example, a code like :

a->account_id=accounts.push_back(account_id);

returns the error C2664: 'void std::list<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'int' to 'Accounts &&

Same happens with pointer type objects :

1
2
Accounts* new_account = new Accounts;
new_account->account_id = accounts.push_back(account_id);


The problem at hand is that I can't find a way to link the created bill block(s) to the corresponding account block. Or work my way from a specific account down to the corresponding bill(s) and populate accordingly...

EDIT : The code :

1
2
3
4
5
6
Accounts* new_account = new Accounts;
new_account->account_id = account_id;
Bills* new_bill = new Bills;
new_bill->billing_id = billing_id;
new_bill->days_left = days_left;
new_bill->debt = debt;


creates the two desired blocks, I can populate them normally, but I can't link them together (join them in a single account block). They are completely separate inside the heap. I get your local instance of the push_back method you suggested for me to use, still, I can't go from the accounts block to the bill block(s) present in there.
Last edited on
I guess new blocks need to be created for example when, no bill block exists for a given account or when the list is completely empty. Iterators don't seem to work with the functions included in the <list> library such as push_back().
That's right! An iterator into a list is not a list itself!

Re error:
a->account_id=accounts.push_back(account_id);
returns the error C2664: 'void std::list<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'int' to 'Accounts &&
There are several errors present here!!
What is accounts type? It is a list<Accounts>. The objects stored in this list are Account objects, not integers.
Hence, the attempt to push_back an integer into the accounts list:
accounts.push_back(account_id);// account_id is an integer
Is, quite frankly, senseless.
The only type of object that you can put in a list<Accounts> is an Accounts object.

Next, you are assigning the return value from the push_back() (type = void) to an integer:
a->account_id=accounts.push_back();

Same happens with pointer type objects :
Accounts* new_account = new Accounts;
new_account->account_id = accounts.push_back(account_id);

Same errors there.

I'm afraid you won't get far until you better understand the several data types that you are working with here.

The code snippet you gave actually frames a different problem.
Linking the new_bill to the new_account requires just one more line:
1
2
3
4
5
6
7
8
Accounts* new_account = new Accounts;
new_account->account_id = account_id;
Bills* new_bill = new Bills;
new_bill->billing_id = billing_id;
new_bill->days_left = days_left;
new_bill->debt = debt;

new_account.bills.push_back( new_bill );// add the new_bill to the new_account 

But, I somehow suspect that's not what you are trying to do...
The code line :

new_account->bills.push_back(new_bill);

returns the same error -> C2664: 'void std::list<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Bills *' to 'Bills &&'

Actually, the *dot* link doesn't work here. The affected link which raises compile errors is the one highlighted below :

new_account->bills.push_back(new_bill);
---------------------^---------------------

In true fact, I want to create a double link between those two blocks by those iterators I mentioned. It is frustrating that, although the iterators are in place, there is still no link between the "account" and its corresponding "bills". Indeed, I may as well be in no position to get it done. Anyhow, I am a bit exhausted for today, perhaps I will see things from a new perspective after a good night sleep.

EDIT : Perhaps I will be forced to use "Accounts* next" and "Bills* next" pointers to travel through the blocks instead of using iterators. I won't relax if I don't make it work with iterators though...
Last edited on
You're correct. I got 2 things wrong on that line.
Both new_account and new_bill are pointers so it should have been:
new_account->bills.push_back( *new_bill );// must dereference the Bills* here
Ok, I implemented the code, the program works as I intend to. I will post the code here for future reference, it seems there aren't many (if any) "linked list within linked list" examples floating around.

Many thanks to fun2code for the assistance!

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <list>
#include <string>

using namespace std;

// class Bills definition
class Bills
{
public:
	int billing_id;
	double debt;
	int days_left;
	Bills::Bills();
};
Bills::Bills()
{
	int billing_id=0;
	debt=0;
	days_left=0;
}
//class Accounts definition
class Accounts
{
public:
	int account_id;
	list<Bills> bills;
	Accounts();
};
Accounts::Accounts()
{
	account_id=0;
}
//global variables
double budget;
list<Accounts> accounts;
list<Accounts>::iterator a;


//Function addBill declaration
void addBill(int account_id, int billing_id, double debt, int days_left);
void show_menu();
void show_accounts();

//main program
int main()
{
	cout<<"Specify the company budget : ";
	cin>>budget;
	int account_id, billing_id, days_left;
	double debt;
	char option;
	
	while(true)
	{
		system("cls");
		show_menu();
		cin>>option;
		while(option!='1' && option!='2' && option!='3')
		{
			system("cls");
			cout<<"\nWrong input selected!\n\n";
			show_menu();
			cin>>option;
		}
		if(option=='1')
		{
			system("cls");
			cout<<"\nInput account id : ";
			cin>>account_id;
			cout<<"\nInput billing id : ";
			cin>>billing_id;
			cout<<"\nInput debt : ";
			cin>>debt;
			cout<<"\nInput days left : ";
			cin>>days_left;
			addBill(account_id,billing_id,debt,days_left);
			cout<<endl<<endl;
			system("pause");
		}
		if(option=='2')
		{
			system("cls");
			show_accounts();
			cout<<endl;
			system("pause");
		}
		if(option=='3')
		{
			break;
		}
	}
	return 0;
	system("pause");
}

//function show_accounts
void show_accounts()
{
	list<Bills>::iterator b;
	for(a=accounts.begin(); a!=accounts.end(); a++)
	{
		cout<<"Account ID : "<<a->account_id<<endl;
		for(b = a->bills.begin(); b!=a->bills.end(); b++)
		{
			cout<<"Billing ID : "<<b->billing_id<<endl;
			cout<<"      Debt : "<<b->debt<<" euro"<<endl;
			cout<<"  Deadline : "<<b->days_left<<" days"<<endl;
		}
		cout<<endl;
	}
}

//function show_menu definition
void show_menu()
{
	cout<<"\n1. Add new bill";
	cout<<"\n2. View all accounts and bills";
	cout<<"\n3. Exit";
	cout<<"\n\nOption? (1-3) -> ";
}

//function addBill definition
void addBill(int account_id, int billing_id, double debt, int days_left)
{
	bool account_found=false;
	bool bill_found=false;
	string confirm ="";
	list<Bills>::iterator b;
	a = accounts.begin();
	while(a!=accounts.end())
	{
		if(a->account_id==account_id)
		{
			account_found=true;
			break;
		}
		a++;
	}
	if(account_found==false)
	{
		Accounts* new_account = new Accounts;
		new_account->account_id = account_id;
		Bills* new_bill = new Bills;
		new_bill->billing_id = billing_id;
		new_bill->days_left = days_left;
		new_bill->debt = debt;
		new_account->bills.push_back(*new_bill);
		accounts.push_back(*new_account);
		budget -= new_bill->debt;
		delete new_account;
		delete new_bill;
		cout<<"\nNew account created!";
		cout<<"\n\nCompany budget is now : "<<budget;
	}
	if(account_found==true)
	{
		b = a->bills.begin();
		while(b!=a->bills.end())
		{
			if(b->billing_id==billing_id)
			{
				bill_found = true;
				break;
			}
			b++;
		}
		if(bill_found==false)
		{
			cout<<"Account with id : "<<a->account_id<<" exists. No bill with id : "
				<<billing_id<<" found"<<endl;
			cout<<"Add new bill with id "<<billing_id<<" ? (type 'y' or 'Y' to confirm) : ";
			cin>>confirm;
			if(confirm == "y" || confirm == "Y")
			{
				Accounts* new_account = new Accounts;
				new_account->account_id = account_id;
				Bills* new_bill = new Bills;
				new_bill->billing_id = billing_id;
				new_bill->days_left = days_left;
				new_bill->debt = debt;
				new_account->bills.push_back(*new_bill);
				accounts.push_back(*new_account);
				budget -= new_bill->debt;
				delete new_account;
				delete new_bill;
				cout<<"\nNew bill created!";
				cout<<"\nCompany budget is now : "<<budget;
			}
		}
		if(bill_found==true)
		{
			cout<<"Account with id : "<<a->account_id<<" exists."<<endl;
			cout<<"Bill with id "<<b->billing_id<<" exists."<<endl;
			cout<<"\n Billing information is as follows...\n\n";
			cout<<"Billing_id : "<<b->billing_id<<endl;
			cout<<"      Debt : "<<b->debt<<" euro"<<endl;
			cout<<"  Deadline : "<<b->days_left<<" days"<<endl;
			cout<<"\n\n New billing information is as follows...\n\n";
			cout<<"Billing_id : "<<billing_id<<endl;
			cout<<"      Debt : "<<debt<<" euro"<<endl;
			cout<<"  Deadline : "<<days_left<<" days"<<endl;
			cout<<"\nUpdate this bill? (type 'y' or 'Y' to confirm) : ";
			cin>>confirm;
			if(confirm == "y" || confirm == "Y")
			{
				Accounts* new_account = new Accounts;
				new_account->account_id = account_id;
				budget += b->debt;
				Bills* new_bill = new Bills;
				new_bill->billing_id = billing_id;
				new_bill->days_left = days_left;
				new_bill->debt = debt;
				new_account->bills.push_back(*new_bill);
				accounts.push_back(*new_account);
				budget -= new_bill->debt;
				delete new_account;
				delete new_bill;
				cout<<"\nBill edited!";
				cout<<"\nCompany budget is now : "<<budget;
			}
		}
	}
}
Complete and revised code including some bug fixes.

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <iostream>
#include <list>
#include <string>

using namespace std;

// class Bills definition
class Bills
{
public:
	int billing_id;
	double debt;
	int days_left;
	Bills::Bills();
};
Bills::Bills()
{
	int billing_id=0;
	debt=0;
	days_left=0;
}
//class Accounts definition
class Accounts
{
public:
	int account_id;
	list<Bills> bills;
	Accounts();
};
Accounts::Accounts()
{
	account_id=0;
}
//global variables
double budget;
list<Accounts> accounts;
list<Accounts>::iterator a;

//Functions' declarations
void incrementBudget(double amount);

void addBill(int account_id, int billing_id, double debt, int days_left);

void show_menu();

void show_accounts();

int breakEven();

void settleBill(int account_id, int billing_id);

double caclulateDebtUpto(int days_left); 

int largestAccount();

int maxAccount();

//main program
int main()
{
	cout<<"Specify the company budget : ";
	cin>>budget;
	int account_id=0, billing_id=0, days_left=0;
	double debt=0, temp =0;
	string option="";
	bool neg_finance = false;
	
	while(true)
	{
		system("cls");
		show_menu();
		cin>>option;
		while(option!="1" && option!="2" && option!="3" && option!="4" && option!="5" && option!="6"
			&& option!="7" && option!="8" && option!="9")
		{
			system("cls");
			cout<<"\nWrong input selected!\n";
			show_menu();
			cin>>option;
		}
		if(option=="1")
		{
			system("cls");
			cout<<"\nInput account id : ";
			cin>>account_id;
			cout<<"\nInput billing id : ";
			cin>>billing_id;
			cout<<"\nInput debt : ";
			cin>>debt;
			cout<<"\nInput days left : ";
			cin>>days_left;
			addBill(account_id,billing_id,debt,days_left);
			cout<<endl<<endl;
			system("pause");
		}
		if(option=="2")
		{
			system("cls");
			show_accounts();
			cout<<endl;
			system("pause");
		}
		if(option=="3")
		{
			system("cls");
			double amount;
			cout<<"Value to be incremented to company's budget : ";
			cin>>amount;
			incrementBudget(amount);
			cout<<endl<<endl;
			system("pause");
		}
		if(option=="4")
		{
			system("cls");
			temp=0;
			list<Bills>::iterator b;
			for(a=accounts.begin(); a!=accounts.end(); a++)
			{
				for(b=a->bills.begin(); b!=a->bills.end(); b++)
				{
				temp+=b->debt;
				}
			}
			neg_finance=breakEven();
			if(neg_finance==0)
				cout<<"Financial status : OK\n";
			if(neg_finance==1)
				cout<<"Financial status : Danger!\n";
			cout<<"\nBudget is : "<<budget<<" with total debt : "<<temp;
			cout<<endl<<endl;
			system("pause");
		}
		if(option=="5")
		{
			system("cls");
			cout<<"Specify account_id : ";
			cin>>account_id;
			cout<<"Specify billing_id : ";
			cin>>billing_id;
			settleBill(account_id, billing_id);
			cout<<endl<<endl;
			system("pause");
		}
		if(option=="6")
		{
			temp=0;
			system("cls");
			cout<<"Input days-window (days) : ";
			cin>>days_left;
			temp=caclulateDebtUpto(days_left);
			cout<<"\nAmount to be paid in a period of "<<days_left<<" days : "<<temp <<" euro";
			cout<<endl<<endl;
			system("pause");
		}
		if(option=="7")
		{
			system("cls");
			account_id=largestAccount();
			cout<<"Account with the largest overall debt is the one with id : "<<account_id;
			cout<<endl<<endl;
			system("pause");
		}
		if(option=="8")
		{
			system("cls");
			account_id=maxAccount();
			cout<<"Account containg the largest Bill debt is the one with id : "<<account_id;
			cout<<endl<<endl;
			system("pause");
		}
		if(option=="9")
		{
			break;
		}
	}
	return 0;
	system("pause");
}
//end of main

//function maxAccount definition
int maxAccount()
{
	double total_bill_debt = 0, t=0;
	int account_id;
	list<Bills>::iterator b;
	for(a=accounts.begin(); a!=accounts.end(); a++)
	{
		for(b=a->bills.begin(); b!=a->bills.end(); b++)
		{
			t=b->debt;
			if(total_bill_debt<t)
			{
				total_bill_debt=t;
				account_id=a->account_id;
			}
		}
	}
	return account_id;
}

//function largestAccount definition
int largestAccount()
{
	double total_account_debt=0, t=0;
	int account_id;
	list<Bills>::iterator b;
	for(a=accounts.begin(); a!=accounts.end(); a++)
	{
		for(b=a->bills.begin(); b!=a->bills.end(); b++)
		{
			t+=b->debt;
		}
		if(total_account_debt<t)
		{
			total_account_debt=t;
			account_id=a->account_id;
		}
		t=0;
	}
	return account_id;
}

//function calculateDebtUpto definition
double caclulateDebtUpto(int days_left)
{
	list<Bills>::iterator b;
	double t=0;
	for(a=accounts.begin(); a!=accounts.end(); a++)
	{
		for(b=a->bills.begin(); b!=a->bills.end(); b++)
		{
			if(b->days_left<=days_left)
				t+=b->debt;
		}
	}
	return t;
}

//function settleBill definition
void settleBill(int account_id, int billing_id)
{
	list<Bills>::iterator b;
	bool found_bill=false, found_account=false;
	for(a=accounts.begin(); a!= accounts.end(); a++)
	{
		if(a->account_id == account_id)
		{
			for(b=a->bills.begin(); b!=a->bills.end(); b++)
			{
				if(b->billing_id==billing_id)
				{
					a->bills.erase(b);
					found_bill=true;
					break;
				}
			}
			found_account=true;
			break;
		}
	}
	if(found_account==false)
		cout<<"\n No account with id : "<<account_id<<" exists.";
	if(found_account==true && found_bill==false)
		cout<<"\n Account with id : "<<account_id<<" contains no bill with id : "<<billing_id;
	if(found_account==true && found_bill==true)
		cout<<"\n Bill with id : "<<billing_id<<" deleted from account with id : "<<account_id;
}

//function breakEven definition
int breakEven()
{
	list<Bills>::iterator b;
	double total_debt = 0;
	for(a=accounts.begin(); a!=accounts.end(); a++)
	{
		for(b=a->bills.begin(); b!=a->bills.end(); b++)
		{
			total_debt+=b->debt;
		}
	}
	if(budget<total_debt)
		return 1;
	return 0;
}

//function incrementBudget definition
void incrementBudget(double amount)
{
	if(amount<=0)
	{
		cout<<"\nError : input only positive values, as implied by 'Increase'.";
	}
	else
	{
		budget+=amount;
		cout<<"\nBudget incremented by the given amount";
	}
}

//function show_accounts
void show_accounts()
{
	list<Bills>::iterator b;
	for(a=accounts.begin(); a!=accounts.end(); a++)
	{
		cout<<"Account ID : "<<a->account_id<<endl;
		cout<<"==================="<<endl;
		for(b = a->bills.begin(); b!=a->bills.end(); b++)
		{
			cout<<"Billing ID : "<<b->billing_id<<endl;
			cout<<"      Debt : "<<b->debt<<" euro"<<endl;
			cout<<"  Deadline : "<<b->days_left<<" days"<<endl;
			if(++b!=a->bills.end())
				cout<<"-------------------"<<endl;
			--b;
		}
		cout<<endl;
	}
}

//function show_menu definition
void show_menu()
{
	cout<<"\n1. Add new bill";
	cout<<"\n2. View all accounts and bills";
	cout<<"\n3. Increase company budget";
	cout<<"\n4. Company financial status";
	cout<<"\n5. Remove bill from existing account";
	cout<<"\n6. Specific debt inside chosen days-window";
	cout<<"\n7. Account with largest total debt";
	cout<<"\n8. Account with largest bill";
	cout<<"\n9. Exit";
	cout<<"\n\nOption? (1-9) -> ";
}
Last edited on
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
//function addBill definition
void addBill(int account_id, int billing_id, double debt, int days_left)
{
	bool account_found=false;
	bool bill_found=false;
	string confirm ="";
	list<Bills>::iterator b;
	a = accounts.begin();
	while(a!=accounts.end())
	{
		if(a->account_id==account_id)
		{
			account_found=true;
			break;
		}
		a++;
	}
	if(account_found==false)
	{
		Accounts* new_account = new Accounts;
		new_account->account_id = account_id;
		Bills* new_bill = new Bills;
		new_bill->billing_id = billing_id;
		new_bill->days_left = days_left;
		new_bill->debt = debt;
		new_account->bills.push_back(*new_bill);
		accounts.push_back(*new_account);
		delete new_account;
		delete new_bill;
		cout<<"\nNew account created successfully. Specified bill added.";
	}
	if(account_found==true)
	{
		b = a->bills.begin();
		while(b!=a->bills.end())
		{
			if(b->billing_id==billing_id)
			{
				bill_found = true;
				break;
			}
			b++;
		}
		if(bill_found==false)
		{
			cout<<"\nAccount with id : "<<a->account_id<<" exists. No bill with id : "
				<<billing_id<<" found"<<endl<<endl;
			cout<<"Add new bill with id : "<<billing_id<<" ? (type 'y' or 'Y' to confirm) : ";
			cin>>confirm;
			if(confirm == "y" || confirm == "Y")
			{
				Bills* new_bill = new Bills;
				new_bill->billing_id = billing_id;
				new_bill->days_left = days_left;
				new_bill->debt = debt;
				a->bills.push_back(*new_bill);
				delete new_bill;
				cout<<"\nNew bill added inside the existing account.";
			}
		}
		if(bill_found==true)
		{
			cout<<"\nAccount with id : "<<a->account_id<<" exists."<<endl;
			cout<<"Bill with id : "<<b->billing_id<<" exists."<<endl;
			cout<<"\n=====Billing information is as follows=====\n\n";
			cout<<"Billing_id : "<<b->billing_id<<endl;
			cout<<"      Debt : "<<b->debt<<" euro"<<endl;
			cout<<"  Deadline : "<<b->days_left<<" days"<<endl;
			cout<<"\n===New billing information is as follows===\n\n";
			cout<<"Billing_id : "<<billing_id<<endl;
			cout<<"      Debt : "<<debt<<" euro"<<endl;
			cout<<"  Deadline : "<<days_left<<" days"<<endl;
			cout<<"\nUpdate this bill? (type 'y' or 'Y' to confirm) : ";
			cin>>confirm;
			if(confirm == "y" || confirm == "Y")
			{
				Bills* new_bill = new Bills;
				new_bill->billing_id = billing_id;
				new_bill->days_left = days_left;
				new_bill->debt = debt;
				a->bills.push_back(*new_bill);
				delete new_bill;
				cout<<"\nBill updated successfully.";
			}
		}
	}
}
Last edited on
Topic archived. No new replies allowed.