Hi everybody!I am a beginner in C++ and I can`t find a solution for this problem.I`ve written a program that calculates the points of sportmen in competition from 6 rounds,sums the points,sorts them, and outputs the first three sportmen with the highest points.I filled the 2 files with the data in the exe. file. The idea is to add a function,which reads from the file below and after the sort phase, the three sportmen to be outputed from the file,that i filled previously, after i pressed 3.Here is the code:
//&br is the number of the sportmen
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <string>
#include<stdlib.h>
using namespace std;
const int N=20;
struct sportmen
{
string name;
double results[6];
double sum;
int place;
};
fstream fp;
do
{menu();
do{cin>>ans;
} while((ans<1)||(ans>4));
switch(ans)
{
case 1:young[br++]=vhod_1();break;
case 2:sort_1(young,br);break;
case 3:top3_1(young);break;
case 4:cout<<"End of the program";exit(0);
}
}
while(ans!=4);
system("pause");
}
sportmen vhod_1()
{
sportmen y;
cout<<"Enter the name of the sportman"<<endl<<"\t";
cin>>y.name;
int i;
cout<<endl<<"Enter the points from the 6 rounds"<<"\t";
y.sum=0;
for(i=0;i<6;i++)
{
cout<<"Discipline number"<<i+1<<endl<<"\t";
cin>>y.results[i];
y.suma+=y.results[i];
}
fp.open("Results.dat",ios::binary|ios::app);
if(fp.fail())
{
cout<<"Error opening the file";exit(0);}
fp.write((char*)y.results,sizeof(double));
void menu()
{
cout<<endl<<"Make your choice from the menu";
cout<<endl<<"*******************Menu********************";
cout<<endl<<"1.Enter the data from one sportman and record it in a file the results from the 6 disciplines";
cout<<endl<<"2.Sort the array of sportmen and record it in a file the whole information for everyone ";
cout<<endl<<"3.Output the 3 top sportmen with the highest points";
cout<<endl<<"4.Exit";
}
After the last fp.close() is the place ,where i want the data to be read and outputed after the sorting in the function.And one more thing- all,which is typed mas[],sportmen mas[], etc is an the array of the sportmen from the structure sportmen.
I tried it but unfortunately it didn`t work.I thought about typing fp.open("Info.dat",ios::binary|ios::in)
fp.read((char*)mas,br*sizeof(sportmen));
But it also didn`t work