Hello everyone....this time a have a competition program that I want ur help but this time i just want from u answers to some problems that i faced when i was working on it :

this is the program:

Mail merge is a software function describing the production of multiple (and potentially
large numbers of) documents from a single template form and a structured data source.
This helps to create personalized letters and pre-addressed envelopes or mailing labels for
mass mailings from a word processing document which contains fixed text, which will be
the same in each output document, and variables, which act as placeholders that are
replaced by text from the data source. The data source is typically a spreadsheet or a
database which has a field or column matching each variable in the template. When the
mail merge is run, the word processing system creates an output document for each row
in the database, using the fixed text exactly as it appears in the template, but substituting
the data variables in the template with the values from the matching columns.
This technique of merging data to create mailshots gave rise to the term mail merge.
As a part of the competition that will take place at the end of February, you have to
implement the mail merge software that is able to:
1. Read the students’ records and saves them to a file.
2. Search for any record and generate a file that contains the student’s record
(Transcript).
3. Add a student record to the file in its corresponding place.
4. Execute the mail merge to generate the students’ transcripts. You have to follow
the same steps as above. To read the students data from a file and to generate
different files (Transcripts).
5. Sort all the records in descending or ascending order.
Note: You can use .dat and .txt files. You will have extra points if you can do it using .xls
and .doc files.

the questions are:
1- according to question 3 , i first of all made a dynamic array in order to be the size as much as the number of students the user will enter, my question is : how can i add a new student to the array that has already a certain size? My idea was to make another dynamic array which its size is the number of new students the user will add plus the munber of the previous students that has been added before, then i will ask the user to enter thier records and finally to copy the records of the previous students, but the problem is : if the user wants to add another students again, i have to put the new array in a for loop but could the size of the array change again? and what will happen to the data that have been saved in the new array?

2- according to the bonus points, how can i let the program reads from a .doc file and to save them in an .xls file in a neat form that replaces each data in a column and in its corresponding place?

Please reply to me as soon as possible and thanks for anyone who can help me

bye

1) it will depend on how you created the array. If you use <vector> then you don't have to do anything special because it will auto expand as you add data. But if you use something like int* array = new int[25]; then you have to first allocate a new array of the required new size, copy all data from old array to new array, add new item, delete old array pointer, set old array pointer to temp pointer. All that time-consuming processor is why most array managers such as <vector> will actually allocate the array to be larger than that needed to hold the data, such that when you want to add another item the memory has already been allocated.

The way I do this is to have another integer that holds the current size of the array (number elements allocated to the array). When the number of elements used in the array equals this integer then new memory has to be allocated plus some extra for future expansion.

Read this link for .doc and .xls file formats

Thanks for the information but i actually done the second type of array because i'm still studying the basics of c++.
I didn't understand how can i delete the old information in the array and to input other data again, if u could provide an example to me or repeat the explanation in a simpler way i will be very pleased

sorry for annoying

to expand an array you could do this in your code

int MAX_SIZE = 256;                  // just using 256 as an example
int arraySize = MAX_SIZE;
int * array= new int[MAX_SIZE];
//  rest of your code
//  while in your loop of entering data
if(arraySize % MAX_SIZE == 0)
{
       MakeArrayBigger(array, arraySize, MAX_SIZE);
}

//  definition to MakeArrayBigger
void MakeArrayBigger(int * array, int size, int incrementSize)
{
       int * temp = new int[size + incrementSize];
       for (int i = 0; i < size; i++)
       {
              temp[i] = array[i];
       }
       delete [] array;
       array = temp;
       delete [] temp;
}

this is just a quick dirty example but i think you will get the jist of it

AFAIK you cant call delete[] temp here (line #21). Am I right?

well.......i didn't take (delete) yet..... if you have time could you explain to me the last steps in the code?

First delete [] array; is called and it's ok since memory pointed by array will not be used.
Next, array = temp; , array points to new part of memory with data.
And then we have delete [] temp; which is, I believe, wrong, because you realease memory pointed by array . What's more if you call MakeArrayBigger again, you will realease the same part of memory one more time. So, IMO this line should be removed.
Just remeber to call delete [] array; somewhere at the end of your program (because of this line int * array= new int[MAX_SIZE]; )

I almost finish the code but i still have a problem in opening more than one file in one code

this is my code:

# include <iostream>
# include <sstream>
# include <string>
# include <fstream>
#include <iomanip>
using namespace std;

struct nameType
{
	string first;
	string last;
};
struct tempt
{
	nameType name;
	int id;
	int tel;
	string add;
	int grade[5];
};
void readKeb (tempt stdrec[], int x);
void copyFile (tempt a[], int x);
void Search (tempt stdrec[], int x);
int Search_ID (tempt stdrec[], int x, int ID);
int Search_Name (tempt stdrec[], int x, string First, string Last);
void Generate (tempt stdrec[], int x, int answer);
void add ( tempt stdrec[], int x,tempt stdfile[],int &a);
void sort1 (tempt stdrec[], int x);
void swap (tempt & a, tempt & b);
void readFile(tempt a[],int &b);
void Tran(tempt a[], int b);

int main()
{
	int x,a,b, z=0,v,n;
	cout << "Enter the number of students: ";
	cin >> x;
	tempt *stdrec = new tempt[x];

	readKeb (stdrec, x);
	copyFile (stdrec, x);
	cout<<"To search for a student press 1: "<<endl;
	cout<<"To pass this request press 0: "<<endl;
	cin>>v;
	if (v==1)
		Search (stdrec, x);
	/*Tran (stdrec, z);*/
	cout<<"If you want to add new students press 1, to choose another thing press 0: "<<endl;
	cin>>v;
	switch (v)
	{
	case 1 :
		{
			cout << "Enter the number of new students you want to add :  ";
			cin >> b;
			a=x+b;
			tempt *stdfile = new tempt [a];
			add ( stdrec, x,stdfile,a);
			break;
		}
	case 0:
		{
			cout<<"To sort the students press 2"<<endl;
			cout<<"To end press any number"<<endl;
			cin>>n;
			if (n==2)
			{
				sort1 (stdrec, x);
				copyFile (stdrec, x);
			}
			cout<<"If you want the program to read the file of the students and execute file for each one press 1: "<<endl;
			cout<<"To see the file yourself press any number: "<<endl;
			cin>>v;
			if (v==1)
			{
				readFile (stdrec,x);
				Tran(stdrec, x);
				cout<<"Thank you for using the code"<<endl;
			}
		}
	}
	
	return 0;
}
void readKeb (tempt stdrec[], int x)
{
	for(int i=0; i<x; i++)
	{
		cout << "enter student " << i+1 << " record: " << endl;
		cout << "----------------------------" << endl;
		cout << "First Name: "; cin >> stdrec[i].name.first; cout << endl;
		cout << "Last Name: "; cin >> stdrec[i].name.last; cout << endl;
		cout << "ID: "; cin >> stdrec[i].id; cout << endl;
		cout << "Telephone: "; cin >> stdrec[i].tel; cout << endl;
		cout << "Address: "; cin >> stdrec[i].add; cout << endl;
		cout << "Grades: ";
		for(int j=0; j<5; j++)
		{
			cout<<j+1<<") ";
			cin >> stdrec[i].grade[j];
		}
	}
}
void copyFile (tempt a[], int x)
{
	ofstream outfile;
	outfile.open("C:\\My resieved files\\print.xls");
	if (!outfile)
	{
		cout<<"ERROR!!"<<endl;
		exit(1);
	}
	for(int i=0; i<x; i++)
	{
		outfile << a[i].name.first <<"\t";
		outfile << a[i].name.last <<"\t";
		outfile << a[i].id << "\t";
		outfile << a[i].tel << "\t";
		outfile << a[i].add<<"\t";
		for(int j=0; j<5; j++)
			outfile<< a[i].grade[j]<<"\t";
		outfile<<endl;
	}
	outfile.close();
}
void Search (tempt stdrec[], int x)
{
	int choice, ID, answer;
	string First, Last;
	cout << "\nPlease choose the way you want to search for the record: ";
	cout << "\nFor searching according to ID\t\t\tPress 1";
	cout << "\nFor searching acoording to the student's name\tPress 2" << endl;
	cin >> choice;
	switch(choice)
	{
	case 1:
		{
			cout << "Please enter the ID: \n";
			cin >> ID;
			answer = Search_ID(stdrec, x, ID);
			while (answer == -1)
			{
				cout << "The ID you have entered is invalid !!";
				cout << "Please enter the student's ID: ";
				cin >> ID;
				answer = Search_ID(stdrec, x, ID);
			}
			Generate (stdrec, x, answer);
			break;
		}
	case 2:
		{
			cout << "Please enter the student's name: \n";
			cin >> First >> Last;
			answer = Search_Name(stdrec, x, First, Last);
			while (answer == -1)
			{
				cout << "The student's name you have entered is invalid !!";
				cout << "Please enter the student's name: ";
				cin >> First >> Last;
				answer = Search_Name(stdrec, x, First, Last);
			}
			Generate (stdrec, x, answer);
			break;
		}
	}
}
int Search_ID (tempt stdrec[], int x, int ID)
{
	for(int i=0; i<x; i++)
	{
		if(stdrec[i].id == ID)
			return i;
	}
	return -1;
}
int Search_Name (tempt stdrec[], int x, string First, string Last)
{
	for(int i=0; i<x; i++)
	{
		if(stdrec[i].name.first == First && stdrec[i].name.last == Last)
			return i;
	}
	return -1;
}
void Generate (tempt stdrec[], int x, int answer)
{
	ofstream outfile;
	if(!outfile)
	{
		cout << "The file doesn't exist!\n";
		exit(1);
	}
	outfile.open("C:\\My resieved files\\search.xls");
	outfile << "Name:\t";
	outfile << stdrec[answer].name.first << "\t" << stdrec[answer].name.last << endl;
	outfile << "ID:\t";
	outfile << stdrec[answer].id << endl;
	outfile << "Telephone:\t" << stdrec[answer].tel << endl;;
	outfile << "Address:\t" ;
	outfile << stdrec[answer].add << endl;
	outfile << "Grades:";
	for(int j=0; j<5; j++)
		outfile << "\t" << stdrec[answer].grade[j];
	outfile << endl;
	outfile.close();
}

void add ( tempt stdrec[], int x,tempt stdfile[], int &a)
{
	int v;
	for (int j=0;j<x;j++)
	{
		stdfile[j].name.first=stdrec[j].name.first;
		stdfile[j].name.last=stdrec[j].name.last;
		stdfile[j].id=stdrec[j].id;
		stdfile[j].tel=stdrec[j].tel;
		stdfile[j].add=stdrec[j].add;
		for (int i=0;i<5;i++)
			stdfile[j].grade[i]=stdrec[j].grade[i];
	}
	for (int k=x;k<a;k++)
	{
		cout << "enter student " << k+1 << " record: " << endl;
		cout << "----------------------------" << endl;
		cout << "First Name: "; cin >> stdfile[k].name.first; cout << endl;
		cout << "Last Name: "; cin >> stdfile[k].name.last; cout << endl;
		cout << "ID: "; cin >> stdfile[k].id; cout << endl;
		cout << "Telephone: "; cin >> stdfile[k].tel; cout << endl;
		cout << "Address: "; cin >> stdfile[k].add; cout << endl;
		cout << "Grades: "<<endl;
		for(int f=0; f<5; f++)
		{
			cout<<f+1<<") ";
			cin >> stdfile[k].grade[f];
		}
	}
	sort1 (stdfile,a);
	copyFile (stdfile, a);
	cout<<"If you want the program to read the file of the students and execute file for each one press 1: "<<endl;
	cout<<"To see the file yourself press 0: "<<endl;
	cin>>v;
	if (v==1)
	{
		readFile (stdfile,a);
		Tran(stdfile,a);
	}
	cout<<"Thank you for using the code"<<endl;
}

void sort1 (tempt a[], int b)
{
	int g;
	cout<<"To sort the students according to their ID press 1"<<endl;
	cout<<"To sort the students according to their names press 2"<<endl;
	cout<<"To end press 0"<<endl;
	cin>>g;
	switch (g)
	{
	case 1:
		{
			cout<<"To sort in accending order press 3 "<<endl;
			cout<<"To sort in deccending order press 4 "<<endl;
			cin>>g;
			if (g==3)
			{
				for (int i=0;i<b-1;i++)
				{
					int z=i;
					for (int j=i+1;j<b;j++)
					{
						if (a[z].id>a[j].id)
							z=j;
					}
					swap (a[z], a[i]);
				}
				break;
			}
			if (g==4)
			{
				for (int i=0;i<b-1;i++)
				{
					int z=i;
					for (int j=i+1;j<b;j++)
					{
						if (a[z].id<a[j].id)
							z=j;
					}
					swap (a[z], a[i]);
				}

				break;
			}
			break;
		}
	case 2:
		{
			cout<<"To sort in accending order press 3 "<<endl;
			cout<<"To sort in deccending order press 4 "<<endl;
			cin>>g;
			if (g==3)
			{
				int n1;
				for (int i=0;i<b-1;i++)
				{
					n1=i;
					for (int j=i+1;j<b;j++)
					{
						if (a[n1].name.first==a[j].name.first)
						{
							if (a[n1].name.last>a[j].name.last)
							{
								n1=j;
							}
						}
						if (a[n1].name.first>a[j].name.first)
						{
							n1=j;
						}
					}
					swap (a[n1],a[i]);
				}
				break;
			}
			if (g==4)
			{
				int n1;
				for (int i=0;i<b-1;i++)
				{
					n1=i;
					for (int j=i+1;j<b;j++)
					{
						if (a[n1].name.first==a[j].name.first)
						{
							if (a[n1].name.last<a[j].name.last)
							{
								n1=j;
							}
						}
						if (a[n1].name.first<a[j].name.first)
						{
							n1=j;
						}
					}
					swap (a[n1],a[i]);
				}
				break;
			}
			break;
		}
		case 0:
			break;
	}
}
void swap (tempt & a, tempt & b)
{
	tempt temp;
	temp=a;
	a=b;
	b=temp;
}

void readFile(tempt a[],int &b)
{
	ifstream infile;
	infile.open("C:\\My resieved files\\print.xls");
	if (!infile)
	{
		cout<<"ERROR!"<<endl;
		exit(1);
	}
	int i=0;
	while (!infile.eof())
	{
		infile >> a[i].name.first;
		infile >> a[i].name.last;
		infile >> a[i].id;
		infile >> a[i].tel;
		infile >> a[i].add;
		for(int j=0; j<5; j++)
			infile >> a[i].grade[j];
		i++;
	}
	b=i-1;
	cout<<"\nThe number of students are: "<<b<<endl;
	ofstream out;
	out.open("C:\\My resieved files\\print.xls");
	while (!out)
	{
		cout<<"ERR!!!!!!"<<endl;
		exit(1);
	}
	out<<"first name\t"<<"last name\t"<<"ID\t"<<"telephone\t"<<"Address\t"<<"Gr1\t"<<"Gr2\t";
	out<<"Gr3\t"<<"Gr4\t"<<"Gr5\t"<<endl;
	for(int i=0; i<b; i++)
	{
		out << a[i].name.first <<"\t";
		out << a[i].name.last <<"\t";
		out << a[i].id << "\t";
		out << a[i].tel << "\t";
		out << a[i].add<<"\t";
		for(int j=0; j<5; j++)
			out<< a[i].grade[j]<<"\t";
		out<<endl;
	}
	infile.close();
	out.close();
}
void Tran(tempt a[], int b)
{
	string *file=new string[b];
	for (int i=0;i<b;i++)
		file[i]="C:\\My resieved files\\"+a[i].name.first+"_"+a[i].name.last+".xls";
	ofstream outfile;
	for (int i=0;i<b;i++)
	{
		string filename;
		filename=file[i];
		cout<<"File number "<<i+1<<" name is: "<<filename<<endl;
		outfile.open(filename);
		while (!outfile)
		{
			cout<<"ERR"<<endl;
			exit(1);
		}
		outfile<<"first name\t"<<"last name\t"<<"ID\t"<<"telephone\t"<<"Address\t"<<"Gr1\t"<<"Gr2\t";
		outfile<<"Gr3\t"<<"Gr4\t"<<"Gr5\t"<<endl;
		outfile << a[i].name.first <<"\t";
		outfile << a[i].name.last <<"\t";
		outfile << a[i].id << "\t";
		outfile << a[i].tel << "\t";
		outfile << a[i].add<<"\t";
		for(int j=0; j<5; j++)
			outfile<< a[i].grade[j]<<"\t";
		outfile<<endl;
	}

}

please anyone help me to solve the error that i have in the code

line 420: open() takes const char* but you are passing std::string. change it like this: outfile.open(filename.c_str());

when i did it, the program couldn't open the file and said ERR as i told him
what should i do?

the program has just made one file which is the first file and then couldn't make the other files
could you give me another solution?

For starters, readKeyb() has several problems. It should be calling getline() to get strings that may contain spaces, such as addresses. And you need to remove the '\n' (Enter key) from the keyboard buffer after entering numeric data.

void readKeb (tempt stdrec[], int x)
{
	for(int i=0; i<x; i++)
	{
		cout << "enter student " << i+1 << " record: " << endl;
		cout << "----------------------------" << endl;
		cout << "First Name: "; cin >> stdrec[i].name.first; cout << endl;
		cout << "Last Name: "; cin >> stdrec[i].name.last; cout << endl;
		cout << "ID: "; cin >> stdrec[i].id; cout << endl;
        cin.ignore(); // remove '\n' from keyboard buffer
		cout << "Telephone: "; cin >> stdrec[i].tel; cout << endl;
        cin.ignore(); // remove '\n' from keyboard buffer
		cout << "Address: "; getline(cin,stdrec[i].add); cout << endl;
		cout << "Grades: ";
		for(int j=0; j<5; j++)
		{
			cout<<j+1<<") ";
			cin >> stdrec[i].grade[j];
		}
        cin.ignore(); // remove '\n' from keyboard buffer
	}
}

function copyfile() -- rename the print.xls to something else because you are not writing the contents of the file in .xls format. I'd suggest you name it print.csv.

it didn't work with me when i use either getline or cin.get

The code I posted above works.

Other problems: One reason your program is not creating the files is because you are leaving some files open and then attempting to use the same fstream object to open the file again. You need to call file.close() then file.clear() before attempting to call file.open() again. That is the case in function Tran().

Make it a habit to close files immediately when done writing (or reading) to them.

when i write getline (cin,stdrec.add)<<endl; and execute the program it passes through it without allowing me to write things in the address
why is this happening?

questions
1-: why do i have to put cin.ignore(); ?

2- this is the function for opening files :

void Tran(tempt a[], int b)
{
	string *file=new string[b];	
	ofstream *outfile=new ofstream [b];
	cout<<"The names of the files are: "<<endl;
	for (int i=0;i<b;i++)
	{
		file[i]="C:\\My resieved files\\"+a[i].name.first+"_"+a[i].name.last+".xls";
		outfile[i].open(file[i].c_str());
		while (!outfile[i])
		{
			cout<<"ERR"<<endl;
			exit(1);
		}
		cout<<"File number "<<i+1<<" name is: "<<file[i]<<endl;
		outfile[i]<<"first name\t"<<"last name\t"<<"ID\t"<<"telephone\t"<<"Address\t"<<"Gr1\t"<<"Gr2\t";
		outfile[i]<<"Gr3\t"<<"Gr4\t"<<"Gr5\t"<<endl;
		outfile[i] << a[i].name.first <<"\t";
		outfile[i] << a[i].name.last <<"\t";
		outfile[i] << a[i].id << "\t";
		outfile[i] << a[i].tel << "\t";
		outfile[i] << a[i].add<<"\t";
		for(int j=0; j<5; j++)
			outfile[i]<< a[i].grade[j]<<"\t";
		outfile[i]<<endl;
		outfile[i].close();
	}

}

is it right?

3- why did you tell me that i have to do file.clear(); ?
i want to keep the files not to erease them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.