text file output problem c++

I have a text file template.txt:
*********************************************************
To:
|1| |2| |3|
|4|
|5|, |6| |7|
Dear |1| |3|:
You and the |3| family may be the lucky winner of $1,000,000 in the C++ programming competition!.....
*****************************************************************************
and i have a text file database.txt:
*********************************************************
Mr.|Harry|Morgan|1105 Main st|San Francisco|CA|95014|
Dr.|John|Lee|702 Ninth St Apt 4|San Jose|CA|95109|
Miss|Evelyn|Garcia|100 University Place|Ann Arbor|MI|48105|
***************************************************************

my code can display the first line in database.txt as the format in template.txt correctly.

my output in the last.txt(created by myself) :

To:
Mr. Harry Morgan
1105 Main st
San Francisco, CA 95014
Dear Mr. Morgan:
You and the Morgan family may be the lucky winner of $1,000,000 in the C++ programming competition!.....

I want to display the next two lines as the same format in last.txt as well.
I used while loop, even make a new function, it does not work at all.
Hope someone can help me! thanks!


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
  #include<iostream>
#include<fstream>
#include<string>
#include <cstdlib>
using namespace std;


void edit(ifstream& data, ifstream& temp,ofstream& last)
{
	char x;
	char x2, x3; 
	string arr[21];
	string line;
	

	for( int j=1;j<21;j++)
	{
		getline(data, line,'|');
		arr[j] = line;
		cout << arr[j] << endl;
	}

	while (temp.get(x))
	{
		if ('|' == x)
		{
			temp.get(x2);
			if (isdigit(x2))
			{
				temp.get(x3);
				if ('|' == x3)
				{
					int n = x2 - '0';
					//n+=7;
					last <<arr[n];
				}
				else
					temp.putback(x3);
			}
			else
				temp.putback(x2);
		}
		else
			last << x;	
	}
}

int main()
{
	ofstream last;
	ifstream temp;
	ifstream data;
	temp.open("template.txt");
	data.open("database.txt");
	last.open("last.txt",fstream::app);
	if (data.fail()) { return 0; }

	
	edit(data,temp,last);


	system("pause");
}
Topic archived. No new replies allowed.