I need help implementing tempaltes in my project of structure, I dont have any idea about the funtions of templates, How can use the templates in that project!!!! plzzzzz
h. file
#ifndef RECORDS_H
#define RECORDS_H
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_RECS = 10;
const int MAX_EXAMS = 5;
struct Estudiante //Definition of structure "Estudiante"
{
string Name;
string SSN;
float Score[MAX_EXAMS];
};
typedef Estudiante Student;
class Records
{
//Definition of "Student" as type "Estudiante"
private: //Class Attributes
Student Record_Array[MAX_RECS];
int count_recs;
public:
//Class Methods
Records();
void Get_Records(ifstream&);
void Get_Average(ofstream&);
void Get_Quiz_Average(ofstream&);
};
#endif
main program:
#include "Records.h"
#include "Records.cc"
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void menu(); //Function declarations
void openfiles (ifstream&, ofstream&);
void closefiles (ifstream&, ofstream&);
ifstream inword; // Input file stream variable
ofstream outword; // Output file stream variable
int main()
{
openfiles(inword, outword); //Call to open files
menu(); //Call to menu
return 0;
}
void menu() //Menu definition
{
int selection; //Stores the user's desired operation
Records instance;
instance.Get_Records(inword);
do
{
system("clear");
cout<< endl<<"Entre el numero de la operacion que desea ejecutar:"<<endl<<endl;
cout << " 1. Obtener el promedio mas alto." <<endl;
cout << " 4. Promedio de quiz mas alto."<<endl;
cout << " 5. Salida." <<endl;
cin >> selection;
switch (selection) //Calls user desired operation
{
case 1:
instance.Get_Average(outword);
break;
case 4:
instance.Get_Quiz_Average(outword);
break;
case 5:
closefiles(inword, outword);
cout<<endl<<"Good bye."<<endl<<endl;
break;
default: cout<<selection<<" No es una opción valida"<<endl;
}
}while(selection != 5);
}
void openfiles (ifstream& inword, ofstream& outword) //Connects the file stream object's "inword and outword" to external file names
{
string infile; //stores the input file name
string outfile; //stores the output file name
cout<<endl<<"Entra el nombre del archivo del cual se va a leer..."<<endl;
cin>>infile;
inword.open(infile.c_str());
if (inword.fail())
{
cout<<endl<<"No se pudo abrir el archivo de entrada," <<endl<<"asegurese de que el archivo exista antes de correr el programa."<<endl;
cout<<"Adios..."<<endl<<endl;
exit(1);
}
cout<<endl<<"Ahora entra el nombre del archivo al cual se va a escribir..."<<endl;
cin>>outfile;
outword.open(outfile.c_str(), ios::app);
if (outword.fail())
{
cout<<endl<<"No se pudo abrir el archivo de salida."<<endl;
cout<<"Adiós..."<<endl<<endl;
exit(1);
}
}
void closefiles (ifstream& inword, ofstream& outword) //Disconnects the file stream object's "inword and outword" from the external file names
{
inword.close();
outword.close();
}
.cpp file
#include "Records.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void Read_Recs(ifstream&, Student[], int&);
void Avg(int, Student[], ofstream&);
void quiz_Average(int,Student[],ofstream&);
Records::Records()
{
//Just to make the compiler happy.
}
void Records::Get_Records(ifstream& inword) //Reads student records from an input file and stores them in an array
{
Read_Recs(inword, Record_Array, count_recs);
}
void Read_Recs(ifstream& inword, Student Record_Array[], int& count_recs)
{
int i = 0;
while((!inword.eof()) && (i<MAX_RECS))
{
inword>>Record_Array[i].Name>>Record_Array[i].SSN;
for (int j = 0; j < MAX_EXAMS; j++)
{
inword>>Record_Array[i].Score[j];
}
i++;
}
count_recs = i;
}
void Records::Get_Average(ofstream& outword)
{
Avg(count_recs, Record_Array, outword);
}
void Avg(int count_recs, Student Record_Array[], ofstream& outword) //Computes the student's average score
{
int position;
float Sum = 0;
float Avg_Grade, Top_Avg = 0;
for (int i = 0; i < count_recs; i++)
{
for (int j = 0; j < MAX_EXAMS; j++)
{
Sum = Sum + Record_Array[i].Score[j];
}
Avg_Grade = Sum/MAX_EXAMS;
if (Avg_Grade > Top_Avg)
{
Top_Avg = Avg_Grade;
position = i;
}
Sum = 0;
}
outword << "El promedio mas alto lo obtuvo "<<Record_Array[position].Name<<", seguro social "<<Record_Array[position].SSN<<", con un promedio de "<<Top_Avg<<endl;
}
void Records::Get_Quiz_Average(ofstream& outword)
{
quiz_Average(count_recs, Record_Array, outword);
}
void quiz_Average(int count_recs,Student Record_Array[], ofstream& outword)
{
int number = 0;
float sum = 0;
cout<< "Entre un numero para ver el promedio de ese quiz ";
cin>>number;
number = number - 1;
for(int i=0; i < count_recs; i++)
sum += Record_Array[i].Score[number];
outword << "El promedio de quiz #" << number+1 << " = " << sum/count_recs << endl;
}
by Frenchy!!
btw, are some dialoge that is in spanish! Because I'm from Puerto Rico(spanish)
This post has been edited by Frenchy: 21 Nov, 2005 - 06:55 PM