Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,680 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,486 people online right now. Registration is fast and FREE... Join Now!




help with templates in structure project

 
Reply to this topicStart new topic

help with templates in structure project

Frenchy
post 21 Nov, 2005 - 06:52 PM
Post #1


New D.I.C Head

*
Joined: 15 Sep, 2005
Posts: 12


My Contributions


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)




wink2.gif

This post has been edited by Frenchy: 21 Nov, 2005 - 06:55 PM
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 21 Nov, 2005 - 06:59 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


Can you provide an overview of what your application is supposed to do?
User is online!Profile CardPM

Go to the top of the page

Mrafcho001
post 21 Nov, 2005 - 08:23 PM
Post #3


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


and please use code tags or color code it! this is very unreadable!

For Color Coder, go to my website (MY SIG) and youll see it in the download section ( it changes the font to courier so its easy to read).

Thank you!

This post has been edited by Mrafcho001: 21 Nov, 2005 - 08:39 PM
User is offlineProfile CardPM

Go to the top of the page

Frenchy
post 23 Nov, 2005 - 06:45 AM
Post #4


New D.I.C Head

*
Joined: 15 Sep, 2005
Posts: 12


My Contributions


#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;
}




the program is an structure that calculate the average of students and calculate the average of the high quizz, the program is in I/O and do that calculates by 5 names, 5 ssn and 5 scores of 5 quizes for each person.
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 23 Nov, 2005 - 01:45 PM
Post #5


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


Templates in Classes:

Templates in classes are easy to use. You declare that its a templated like this:

template < class cClass>
class MyClass
{
public:
cClass classObject; // This classObject is now what the class has been declared
};


when using the class this is the syntax:
MyClass<int> instOfMyClass; //Makes a MyClass object templated with int

instOfMyClass.classObject = 1; // classObject is an int, because we declared it to be an int class


When you have functions in the class such as:

template < class tmpClass >
class MyClass
{
public:
tmpClass classObject;
void setclsObj(tmpClass tmpObj);
};

template < class tmpClass > //Notice its templated function and examine the syntax
void MyClass< tmpClass>::setclsObj(tmpClass tmpObj)
{
classObject = tmpObj;
}


thats pretty much about it about templating classes, structures are the exact same way. You should look at this tutorial
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 07:45AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month