Hey everyone I'm a college student taking semester two of C++ and we have a project using classes. This semester we are strictly using Vi to edit and compile my code. WARNING THIS CODE IS NOT THE WAY I WOULD PERSONALY WRITE IT, BUT MY INSTRUCTOR INSISTS ON THIS FORMAT. Here is my code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//************************************
const int NAMESIZE=11;
const int FILENAMESIZE=51;
const int ARRAYSIZE=20;
int N=0;
typedef char Name_t[NAMESIZE];
typedef int Grade_t;
typedef char Filename_t[FILENAMESIZE];
typedef fstream Datafile_t;
typedef fstream Outfile_t;
//*************************************
class cStudent
{
private:
Name_t fname;
Name_t lname;
Grade_t t1;
Grade_t t2;
Grade_t t3;
Grade_t t4;
Grade_t assigngrade;
Grade_t examgrade;
public:
cStudent(){}//default constructor
cStudent(Name_t NewName, Grade_t NewGrade);
cStudent & operator=( const cStudent student ) {
student.get_fname( fname );
student.get_lname( lname );
student.get_t1(t1);
student.get_t2(t2);
student.get_t3(t3);
student.get_t4(t4);
student.get_assigngrade( assigngrade );
student.get_examgrade( examgrade );
}
void Read(Datafile_t &Datafile);
void Print(Outfile_t &Outfile);
//set
void set_fname(Name_t first)
{ strcpy(fname,first);}
void set_lname(Name_t last)
{ strcpy(lname,last);}
void set_t1(Grade_t x)
{ t1=x;}
void set_t2(Grade_t x)
{ t2=x;}
void set_t3(Grade_t x)
{ t3=x;}
void set_t4(Grade_t x)
{ t4=x;}
void set_assigngrade(Grade_t x)
{ assigngrade=x;}
void set_examgrade(Grade_t x)
{ examgrade=x;}
//end set
//get
void get_fname(Name_t &fname) const
{ strcpy(fname,this->fname);}
void get_lname(Name_t &lname) const
{ strcpy(lname,this->lname);}
int get_t1(Grade_t &t1) const
{return t1;}
int get_t2(Grade_t &t2) const
{return t2;}
int get_t3(Grade_t &t3) const
{return t3;}
int get_t4(Grade_t &t4) const
{return t4;}
int get_assigngrade(Grade_t &assigngrade) const
{return assigngrade;}
int get_examgrade(Grade_t &examgrade) const
{return assigngrade;}
//end get
};//end of class cStudent
typedef cStudent StudentArray[30];
StudentArray CSCI208Class;//array
StudentArray TempStudent;//array #2
void InputIntoArray(StudentArray &CSCI208Class, int &N);
void PrintFromArray(StudentArray CSCI208Class, int N);
//***MAIN***
main()
{
StudentArray TempStudent;
StudentArray CSCI208Class;
InputIntoArray(CSCI208Class,N);
PrintFromArray(CSCI208Class,N);
};//***end_MAIN***
void PrintFromArray(StudentArray CSCI208Class, int N)
{
Filename_t Filename;
Outfile_t Outfile;
cout<<"Please enter the name of the output file to be created:"<<endl;
cin>>Filename;
cout<<"Opening output file......."<<endl;
Outfile.open(Filename,ios::out);
Outfile<<setw(10)<<"Name"<<" "<<"Test Grades"<<" "<<"Assignment Grade"<<" "<<"Exam Grade"<<endl;
Outfile<<endl;
for(int I=0; I<N; I++)
{
Name_t fname;
Name_t lname;
Grade_t t1;
Grade_t t2;
Grade_t t3;
Grade_t t4;
Grade_t assigngrade;
Grade_t examgrade;
CSCI208Class[I].get_fname(fname);
CSCI208Class[I].get_lname(lname);
CSCI208Class[I].get_t1(t1);
CSCI208Class[I].get_t2(t2);
CSCI208Class[I].get_t3(t3);
CSCI208Class[I].get_t4(t4);
CSCI208Class[I].get_assigngrade(assigngrade);
CSCI208Class[I].get_examgrade(examgrade);
Outfile<<setw(7)<<fname<<" "<<lname<<setw(6)<<t1<<" "<<t2<<" "<<t3<<" "<<t4<<" "<<assigngrade<<" "<<examgrade<<endl;
Outfile<<"******************************************************"<<endl;
};//end for loop
cout<<endl;
cout<<"The number of reccords in the output is "<<N<<"."<<endl;
cout<<"Now closing all files............"<<endl;
Outfile.close();
cout<<"Terminating program........GOODBYE...."<<endl;
};//end print from array
void InputIntoArray(StudentArray &CSCI208Class, int &N)
{
Filename_t Filename;
Datafile_t Datafile;
cout<<"Enter the name of the data file:"<<endl;
cin>>Filename;
cout<<"Opening Datafile..............."<<endl;
StudentArray TempStudent;
Datafile.open(Filename,ios::in);
Name_t first;
Datafile >> first;
N=0;
TempStudent[N].set_fname(first);
while(!Datafile.eof())
{
Name_t last;
Datafile >> last;
TempStudent[N].set_lname(last);
Grade_t x;
Datafile >> x;
TempStudent[N].set_t1(x);
Datafile >> x;
TempStudent[N].set_t2(x);
Datafile >> x;
TempStudent[N].set_t3(x);
Datafile >> x;
TempStudent[N].set_t4(x);
Datafile >> x;
TempStudent[N].set_assigngrade(x);
Datafile >> x;
TempStudent[N].set_examgrade(x);
CSCI208Class[N]=TempStudent[N];
N=N+1;
Name_t first;
Datafile >> first;
TempStudent[N].set_fname(first);
}//end while loop
cout<<"Closing Datafile............"<<endl;
Datafile.close();
cout<<"Begining secondary processes.............."<<endl;
};//end InputIntoArray
The code compiles and links properly but I am getting strange results when I run the program. Here is the data file I am using:
John
Doe
100
99
98
97
96
95
Jane
Doe
95
96
97
98
99
100
Sam
Doe
100
99
98
97
96
95
Paul
Doe
95
96
97
98
99
100
Bob
Dole
100
100
100
100
100
100
Fred
Doe
98
98
98
98
78
100
Peter
Parker
100
99
99
99
89
50
basically when I use this data file my program is able to print the names correctly but the outfile prints all the numbers at the end of the data file.
does anyone see a problem with how i read and print?
thanks in advance-
atticusr5