Грешка в C++ задача

hotboyrz

Registered
Здравейте,
имам проблем с кода на една задача на С++

Условието е следното:

Съставете програма с функции за:
а) Добавяне в масив и файл на данни за студенти от група (до 30): име,
факултетен номер,среден успех, възраст, пол.Извеждане на текущото съдържание на
масива (файла) на екран.
б) Презаписване на данните за мъжете и жените в 2 отделни масива, определяне
на съответната средна възраст (за мъжете и жените) и извеждане на съдържанието
на масивите и резултатите на екрана.
в)Определяне на наи-младата студентка и подреждане на данните за студентите
във възходящ ред по възраст.Извеждане на резултатите на екран.

Главна функция main() - с меню за избор и функции и проверка за състоянието на данните

Код:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct students
{
	char name[15];
	char familia[15];
	long double fn;
	float uspeh;
	int godini;
	int pol;
}arr[30],arm[30],arj[30],temp[30],tempj[30];

void input();
void pz();
void nm();
void load();
void save();
int i=0;
int k;
int p;
int br=0;


void main()
{
int i;
load();
do
{
cout<<"\n        *****MENU*****"<<endl;
cout<<"\n 1.Vavejdane na inforfmaciq za student"<<endl;
cout<<"\n 2.Prezapisvane v 2 otdelni masiva[maje-jeni]"<<endl;
cout<<"\n 3.Opredelqne na nai-mladata studentka i sortirane po vazrast"<<endl;
cout<<"\n 4.Zapisvane vav fail"<<endl;
cout<<"\n 5.Izhod"<<endl;

cin>>i;
if(i>0 || i<5)
{
	switch(i)
	{
	case 1:input();break;
	case 2:pz();break;
	case 3:nm();break;
	case 4:save();break;
	}
}
else
cout<<"\n Nevalidna funkciq"<<endl;
}
while(i!=5);
}



void input()
{	
	cout<<"\n Vuvedete broj studenti:";
	cin>>br;
	if(br<1||br>30)
	cout<<"\n Maksimalniqt broi studenti e 30!"<<endl;
	else
	for(i=0;i<br;i++)
	{
	cout<<"\n Ime: ";
	cin>>arr[i].name;
	cout<<"\n Familia: ";
	cin>>arr[i].familia;
	cout<<"\n Fakulteten nomer: ";
	cin>>arr[i].fn;
	cout<<"\n Uspeh: ";
	cin>>arr[i].uspeh;
	cout<<"\n Vuzrast: ";
	cin>>arr[i].godini;
	cout<<"\n Pol (1=maj/2=jena): ";
	cin>>arr[i].pol;
	cout<<"-----------------"<<endl;
	fflush(stdin);
	}
	cout<<"\n Tekushto sadarjanie:"<<endl;
	cout<<" --------------------"<<endl;
	for(i=0;i<br;i++)
	{
	cout<<"Ime: "<<arr[i].name<<endl;	
	cout<<"Familiq: "<<arr[i].familia<<endl;	
	cout<<"F.N.: "<<arr[i].fn<<endl;
	cout<<"Uspeh: "<<arr[i].uspeh<<endl;
	cout<<"Godini: "<<arr[i].godini<<endl;
	cout<<"Pol (1=maj/2=jena): "<<arr[i].pol<<endl;
	cout<<" --------------"<<endl;
	}
}

void pz()
{
	int t;
	float srm=0;
	float srj=0;
	for(i=0;i<br;i++)
	{
		if(arr[i].pol==1)
		{
			arm[k]=arr[i];k++;t++;
		}
		else
		{
		arj[p]=arr[i];p++;
	}
	}
	cout<<"\n Maje:"<<endl;
	for(i=0;i<k;i++)
	{
	cout<<"Ime: "<<arm[i].name<<endl;	
	cout<<"Familiq: "<<arm[i].familia<<endl;	
	cout<<"F.N.: "<<arm[i].fn<<endl;
	cout<<"Uspeh: "<<arm[i].uspeh<<endl;
	cout<<"Godini: "<<arm[i].godini<<endl;
	cout<<"Pol (1=maj/2=jena): "<<arm[i].pol<<endl;
	cout<<" --------------"<<endl;
	srm+=arm[i].godini;
	}
	cout<<"\n Sredna vazrast maje:"<<srm/k<<endl;

	cout<<"\n Jeni:"<<endl;

	for(i=0;i<p;i++)
	{	
	cout<<"Ime: "<<arj[i].name<<endl;	
	cout<<"Familiq: "<<arj[i].familia<<endl;	
	cout<<"F.N.: "<<arj[i].fn<<endl;
	cout<<"Uspeh: "<<arj[i].uspeh<<endl;
	cout<<"Godini: "<<arj[i].godini<<endl;
	cout<<"Pol (1=maj/2=jena): "<<arj[i].pol<<endl;
	cout<<" --------------"<<endl;
	srj+=arj[i].godini;
	}
	cout<<"\n Sredna vazrast jeni:"<<srj/p<<endl;
}


void nm()
{
	
	int i;
	char x;
	int j;
	for(i=0;i<br-1;i++)
		for(j=i+1;j<br;j++)
		if(arr[i].godini>arr[j].godini)
		{
			temp[x]=arr[i];
			arr[i]=arr[j];
			arr[j]=temp[x];
		}

	for(i=0;i<p-1;i++)
		for(j=i+1;j<p;j++)
		if(arj[i].godini>arj[j].godini)
		{
			tempj[x]=arj[i];
			arj[i]=arj[j];
			arj[j]=tempj[x];
		}
		cout<<"\n Nai-mlada studentka:"<<endl;

	
	cout<<"Ime: "<<arj[0].name<<endl;	
	cout<<"Familiq: "<<arj[0].familia<<endl;	
	cout<<"F.N.: "<<arj[0].fn<<endl;
	cout<<"Uspeh: "<<arj[0].uspeh<<endl;
	cout<<"Godini: "<<arj[0].godini<<endl;
	cout<<"Pol (1=maj/2=jena): "<<arj[0].pol<<endl;
	cout<<" --------------"<<endl;
	


	cout<<"\n Vazhodqsht spisak po godini:"<<endl;
	for(i=0;i<br;i++)
	{
	cout<<"Ime: "<<arr[i].name<<endl;	
	cout<<"Familiq: "<<arr[i].familia<<endl;	
	cout<<"F.N.: "<<arr[i].fn<<endl;
	cout<<"Uspeh: "<<arr[i].uspeh<<endl;
	cout<<"Godini: "<<arr[i].godini<<endl;
	cout<<"Pol (1=maj/2=jena): "<<arr[i].pol<<endl;
	cout<<" --------------"<<endl;
	}

}

void load()
{
	FILE *fp;
	int i;
	if((fp=fopen("students.dat","rb"))==NULL)
	{
		cout<<"\n File not found";return;;
	}
	for(i=0;i<br;i++)
		if(fread(&arr[i],sizeof(arr[i]),1,fp)!=1)
		{
			cout<<"\n Error reading students";exit(1);
		}
fclose(fp);
}

void save()
{
	FILE *fp;
	int i;
	if((fp=fopen("students.dat","wb"))==NULL)
	{
		cout<<"\n File error";exit(1);
	}
	for(i=0;i<br;i++)
		if(fwrite(&arr[i],sizeof(arr[i]),1,fp)!=1)
		{
			cout<<"\n Error writing struct";exit(1);
		}
fclose(fp);
}

Програмата работи еднократно - след повторно отваряне намира файла, но операциите не работят с информацията в него, трябва да се въвежда нова информация.Откъде идва този проблем ?
 

Горе