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

Recommended Answers

All 7 Replies

Did I miss where you posted the input?
Do you know to avoid loop control with eof(), or is that another "requirement"?
Does your compiler not bring up issues like these?

main.cpp
main.cpp: In member function `cStudent& cStudent::operator=(cStudent)':
main.cpp:40: warning: no return statement in function returning non-void
main.cpp: At global scope:
main.cpp:95: error: ISO C++ forbids declaration of `main' with no type
main.cpp: In function `int main()':
main.cpp:96: warning: unused variable 'TempStudent'
main.cpp: At global scope:
main.cpp:100: error: extra `;'
main.cpp:141: error: extra `;'
main.cpp:197: error: extra `;'

my input is under the code, it is the long list of first and last names followed by numbers

yes the .eof is used alot, we used it last semester as well as our default while loop control (for reading files of course)

no we have to use Vi in Linux to compile so I dont get any errors just improper output, but let me get on putty and repost the code (in case there was a loss in translation with the other files on my desktop)

also i do get errors when i use visual studio, but they are mostly regarding using and "int main" and not just "main"

here is the code code again

#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 );
                }//end cStudent &operator

                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 a)
                {       t1=a;}
                void set_t2(Grade_t b)
                {       t2=b;}
                void set_t3(Grade_t c)
                {       t3=c;}
                void set_t4(Grade_t d)
                {       t4=d;}
                void set_assigngrade(Grade_t e)
                {       assigngrade=e;}
                void set_examgrade(Grade_t f)
                {       examgrade=f;}
                //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()
{
        int N=0;
        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 a;
                Grade_t b;
                Grade_t c;
                Grade_t d;
                Grade_t e;
                Grade_t f;


                Datafile >> a;
                TempStudent[N].set_t1(a);


                Datafile >> b;
                TempStudent[N].set_t2(b);


                Datafile >> c;
                TempStudent[N].set_t3(c);


                Datafile >> d;
                TempStudent[N].set_t4(d);


                Datafile >> e;
                TempStudent[N].set_assigngrade(e);


                Datafile >> f;
                TempStudent[N].set_examgrade(f);

                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

my input is under the code, it is the long list of first and last names followed by numbers

D'oh! For some reason I thought that was your output. My bad.

Look here:

CSCI208Class[I].get_t1(t1);
                CSCI208Class[I].get_t2(t2);
                CSCI208Class[I].get_t3(t3);
                CSCI208Class[I].get_t4(t4);

Don't those functions return values?

[edit]The style being used is confusing as hell. But should the get functions be something like this?

int get_t1(Grade_t &t1) const
                {t1 = this->t1; return t1;}

that was it!! I cant believe that i forgot the pointer when i used it right above!!!!

thanks the code is working now but i totally agree with you it is a miracle that this code manages to compile anyway

thanks again for your help

also i personally would write all functions before main(). as it helps keep your code organized. but its just my preference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.