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

C++ JAVA
Post Reply
User avatar
hotboyrz
Нов
Нов
Posts: 74
Joined: Mon Sep 18, 2006 2:20 am

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

Post by hotboyrz » Wed Jan 09, 2013 10:24 am

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

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

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

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

Code: Select all

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct students
&#123;
	char name&#91;15&#93;;
	char familia&#91;15&#93;;
	long double fn;
	float uspeh;
	int godini;
	int pol;
&#125;arr&#91;30&#93;,arm&#91;30&#93;,arj&#91;30&#93;,temp&#91;30&#93;,tempj&#91;30&#93;;

void input&#40;&#41;;
void pz&#40;&#41;;
void nm&#40;&#41;;
void load&#40;&#41;;
void save&#40;&#41;;
int i=0;
int k;
int p;
int br=0;


void main&#40;&#41;
&#123;
int i;
load&#40;&#41;;
do
&#123;
cout<<"\n        *****MENU*****"<<endl;
cout<<"\n 1.Vavejdane na inforfmaciq za student"<<endl;
cout<<"\n 2.Prezapisvane v 2 otdelni masiva&#91;maje-jeni&#93;"<<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&#40;i>0 || i<5&#41;
&#123;
	switch&#40;i&#41;
	&#123;
	case 1&#58;input&#40;&#41;;break;
	case 2&#58;pz&#40;&#41;;break;
	case 3&#58;nm&#40;&#41;;break;
	case 4&#58;save&#40;&#41;;break;
	&#125;
&#125;
else
cout<<"\n Nevalidna funkciq"<<endl;
&#125;
while&#40;i!=5&#41;;
&#125;



void input&#40;&#41;
&#123;	
	cout<<"\n Vuvedete broj studenti&#58;";
	cin>>br;
	if&#40;br<1||br>30&#41;
	cout<<"\n Maksimalniqt broi studenti e 30!"<<endl;
	else
	for&#40;i=0;i<br;i++&#41;
	&#123;
	cout<<"\n Ime&#58; ";
	cin>>arr&#91;i&#93;.name;
	cout<<"\n Familia&#58; ";
	cin>>arr&#91;i&#93;.familia;
	cout<<"\n Fakulteten nomer&#58; ";
	cin>>arr&#91;i&#93;.fn;
	cout<<"\n Uspeh&#58; ";
	cin>>arr&#91;i&#93;.uspeh;
	cout<<"\n Vuzrast&#58; ";
	cin>>arr&#91;i&#93;.godini;
	cout<<"\n Pol &#40;1=maj/2=jena&#41;&#58; ";
	cin>>arr&#91;i&#93;.pol;
	cout<<"-----------------"<<endl;
	fflush&#40;stdin&#41;;
	&#125;
	cout<<"\n Tekushto sadarjanie&#58;"<<endl;
	cout<<" --------------------"<<endl;
	for&#40;i=0;i<br;i++&#41;
	&#123;
	cout<<"Ime&#58; "<<arr&#91;i&#93;.name<<endl;	
	cout<<"Familiq&#58; "<<arr&#91;i&#93;.familia<<endl;	
	cout<<"F.N.&#58; "<<arr&#91;i&#93;.fn<<endl;
	cout<<"Uspeh&#58; "<<arr&#91;i&#93;.uspeh<<endl;
	cout<<"Godini&#58; "<<arr&#91;i&#93;.godini<<endl;
	cout<<"Pol &#40;1=maj/2=jena&#41;&#58; "<<arr&#91;i&#93;.pol<<endl;
	cout<<" --------------"<<endl;
	&#125;
&#125;

void pz&#40;&#41;
&#123;
	int t;
	float srm=0;
	float srj=0;
	for&#40;i=0;i<br;i++&#41;
	&#123;
		if&#40;arr&#91;i&#93;.pol==1&#41;
		&#123;
			arm&#91;k&#93;=arr&#91;i&#93;;k++;t++;
		&#125;
		else
		&#123;
		arj&#91;p&#93;=arr&#91;i&#93;;p++;
	&#125;
	&#125;
	cout<<"\n Maje&#58;"<<endl;
	for&#40;i=0;i<k;i++&#41;
	&#123;
	cout<<"Ime&#58; "<<arm&#91;i&#93;.name<<endl;	
	cout<<"Familiq&#58; "<<arm&#91;i&#93;.familia<<endl;	
	cout<<"F.N.&#58; "<<arm&#91;i&#93;.fn<<endl;
	cout<<"Uspeh&#58; "<<arm&#91;i&#93;.uspeh<<endl;
	cout<<"Godini&#58; "<<arm&#91;i&#93;.godini<<endl;
	cout<<"Pol &#40;1=maj/2=jena&#41;&#58; "<<arm&#91;i&#93;.pol<<endl;
	cout<<" --------------"<<endl;
	srm+=arm&#91;i&#93;.godini;
	&#125;
	cout<<"\n Sredna vazrast maje&#58;"<<srm/k<<endl;

	cout<<"\n Jeni&#58;"<<endl;

	for&#40;i=0;i<p;i++&#41;
	&#123;	
	cout<<"Ime&#58; "<<arj&#91;i&#93;.name<<endl;	
	cout<<"Familiq&#58; "<<arj&#91;i&#93;.familia<<endl;	
	cout<<"F.N.&#58; "<<arj&#91;i&#93;.fn<<endl;
	cout<<"Uspeh&#58; "<<arj&#91;i&#93;.uspeh<<endl;
	cout<<"Godini&#58; "<<arj&#91;i&#93;.godini<<endl;
	cout<<"Pol &#40;1=maj/2=jena&#41;&#58; "<<arj&#91;i&#93;.pol<<endl;
	cout<<" --------------"<<endl;
	srj+=arj&#91;i&#93;.godini;
	&#125;
	cout<<"\n Sredna vazrast jeni&#58;"<<srj/p<<endl;
&#125;


void nm&#40;&#41;
&#123;
	
	int i;
	char x;
	int j;
	for&#40;i=0;i<br-1;i++&#41;
		for&#40;j=i+1;j<br;j++&#41;
		if&#40;arr&#91;i&#93;.godini>arr&#91;j&#93;.godini&#41;
		&#123;
			temp&#91;x&#93;=arr&#91;i&#93;;
			arr&#91;i&#93;=arr&#91;j&#93;;
			arr&#91;j&#93;=temp&#91;x&#93;;
		&#125;

	for&#40;i=0;i<p-1;i++&#41;
		for&#40;j=i+1;j<p;j++&#41;
		if&#40;arj&#91;i&#93;.godini>arj&#91;j&#93;.godini&#41;
		&#123;
			tempj&#91;x&#93;=arj&#91;i&#93;;
			arj&#91;i&#93;=arj&#91;j&#93;;
			arj&#91;j&#93;=tempj&#91;x&#93;;
		&#125;
		cout<<"\n Nai-mlada studentka&#58;"<<endl;

	
	cout<<"Ime&#58; "<<arj&#91;0&#93;.name<<endl;	
	cout<<"Familiq&#58; "<<arj&#91;0&#93;.familia<<endl;	
	cout<<"F.N.&#58; "<<arj&#91;0&#93;.fn<<endl;
	cout<<"Uspeh&#58; "<<arj&#91;0&#93;.uspeh<<endl;
	cout<<"Godini&#58; "<<arj&#91;0&#93;.godini<<endl;
	cout<<"Pol &#40;1=maj/2=jena&#41;&#58; "<<arj&#91;0&#93;.pol<<endl;
	cout<<" --------------"<<endl;
	


	cout<<"\n Vazhodqsht spisak po godini&#58;"<<endl;
	for&#40;i=0;i<br;i++&#41;
	&#123;
	cout<<"Ime&#58; "<<arr&#91;i&#93;.name<<endl;	
	cout<<"Familiq&#58; "<<arr&#91;i&#93;.familia<<endl;	
	cout<<"F.N.&#58; "<<arr&#91;i&#93;.fn<<endl;
	cout<<"Uspeh&#58; "<<arr&#91;i&#93;.uspeh<<endl;
	cout<<"Godini&#58; "<<arr&#91;i&#93;.godini<<endl;
	cout<<"Pol &#40;1=maj/2=jena&#41;&#58; "<<arr&#91;i&#93;.pol<<endl;
	cout<<" --------------"<<endl;
	&#125;

&#125;

void load&#40;&#41;
&#123;
	FILE *fp;
	int i;
	if&#40;&#40;fp=fopen&#40;"students.dat","rb"&#41;&#41;==NULL&#41;
	&#123;
		cout<<"\n File not found";return;;
	&#125;
	for&#40;i=0;i<br;i++&#41;
		if&#40;fread&#40;&arr&#91;i&#93;,sizeof&#40;arr&#91;i&#93;&#41;,1,fp&#41;!=1&#41;
		&#123;
			cout<<"\n Error reading students";exit&#40;1&#41;;
		&#125;
fclose&#40;fp&#41;;
&#125;

void save&#40;&#41;
&#123;
	FILE *fp;
	int i;
	if&#40;&#40;fp=fopen&#40;"students.dat","wb"&#41;&#41;==NULL&#41;
	&#123;
		cout<<"\n File error";exit&#40;1&#41;;
	&#125;
	for&#40;i=0;i<br;i++&#41;
		if&#40;fwrite&#40;&arr&#91;i&#93;,sizeof&#40;arr&#91;i&#93;&#41;,1,fp&#41;!=1&#41;
		&#123;
			cout<<"\n Error writing struct";exit&#40;1&#41;;
		&#125;
fclose&#40;fp&#41;;
&#125;
Програмата работи еднократно - след повторно отваряне намира файла, но операциите не работят с информацията в него, трябва да се въвежда нова информация.Откъде идва този проблем ?

Post Reply