Hello all I am a second year C++ student in college and I am running into some issues with a code assignment I am writing. *Note: I realize that there may be better ways of doing the code but I am following my instructors guidelines so please no posts about that I have had bad expericane*

Anyway 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);

		void Read(Datafile_t &Datafile);

		void Print(Outfile_t &Outfile);
		//set

		void set_fname(Name_t fname)

		{	strcpy(fname,fname);}

		void set_lname(Name_t lname)

		{	strcpy(lname,lname);}

		void set_t1(Grade_t t1)

		{	t1=t1;}

		void set_t2(Grade_t t2)

		{	t2=t2;}

		void set_t3(Grade_t t3)

		{	t3=t3;}

		void set_t4(Grade_t t4)

		{	t4=t4;}

		void set_assigngrade(Grade_t assigngrade)

		{	assigngrade=assigngrade;}

		void set_examgrade(Grade_t examgrade)

		{	examgrade=examgrade;}

		//end set
		//get
		void get_fname(Name_t &fname)

		{	strcpy(fname,this->fname);}

		void get_lname(Name_t &lname)

		{	strcpy(lname,this->lname);}

		int get_t1(Grade_t &t1)

		{return t1;}

		int get_t2(Grade_t &t2)

		{return t2;}

		int get_t3(Grade_t &t3)

		{return t3;}

		int get_t4(Grade_t &t4)

		{return t4;}

		int get_assigngrade(Grade_t &assigngrade)

		{return assigngrade;}

		int get_examgrade(Grade_t &examgrade)

		{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);

	Datafile>>TempStudent::fname;

	while(!Datafile.eof())

	{

		N=0;

		Datafile>>TempStudent::lname;

		Datafile>>TempStudent::t1;

		Datafile>>TempStudent::t2;

		Datafile>>TempStudent::t3;

		Datafile>>TempStudent::t4;

		Datafile>>TempStudent::assigngrade;

		Datafile>>TempStudent::examgrade;

		

		CSCI208Class[N]=TempStudent;

		N=N+1;

		Datafile>>TempStudent::fname;

	}//end while loop

	cout<<"Closing Datafile............"<<endl;

	Datafile.close();

	cout<<"Begining secondary processes.............."<<endl;

	

};//end InputIntoArray

I am compiling in Vi (again because we have to) and here are the following errors I am getting:

assign3.cpp: In function ‘void InputIntoArray(cStudent (&)[30], int&)’:
assign3.cpp:140: error: ‘TempStudent’ is not a class or namespace
assign3.cpp:144: error: ‘TempStudent’ is not a class or namespace
assign3.cpp:145: error: ‘TempStudent’ is not a class or namespace
assign3.cpp:146: error: ‘TempStudent’ is not a class or namespace
assign3.cpp:147: error: ‘TempStudent’ is not a class or namespace
assign3.cpp:148: error: ‘TempStudent’ is not a class or namespace
assign3.cpp:149: error: ‘TempStudent’ is not a class or namespace
assign3.cpp:150: error: ‘TempStudent’ is not a class or namespace
assign3.cpp:152: error: no match for ‘operator=’ in ‘CSCI208Class[N] = TempStudent’
assign3.cpp:17: note: candidates are: cStudent& cStudent::operator=(const cStudent&)
assign3.cpp:154: error: ‘TempStudent’ is not a class or namespace

Im really stuck as to what is wrong, I have tried declaring the class in diffrent areas, etc... everything I know to do but have come up with nothing.

Is there any help out there?

Thanks
-atticusr5

IMO this is the result of the indescriminant use of typedefs.

On line 266 TempStudent is declared to be of type StudentArray. Therefore, rather than the :: operator to access member variables of whatever a StudentArray object is, you will need to use one of the following: TempStudent.memberVariable, TempStudent->memberVariable or possibly TempStudent.memberVariable or TempStudent->memberVariable depending one whether TempStudent is a single object or an array of objects.

commented: "What is indiscriminant use of typedefs, Alex?" Correct! +1

there are a few things that are pretty wrong here.

From line 270-298 you write TempStudent::something.

TempStudent is an array so you should use [] to access some element. I seems like you would want to write TempStudent[N]::something

And Im not sure if :: will work either. You have setters for those variables so why dont you ise them instead? It is private access to those members so you should use setters.

you could also make all members public and write Datafile>>TempStudent[index].member

Secondly you haven't defined an operator= for cStudent. Should look something like this...

cStudent & operator=( const cStudent & );

and ofcourse define it to.

Thanks for the replys, I understand now how I was misusing the TempStudent so I went ahead and changed some of my "get" methods to new versions and I have managed to end up with the following errors:

assign3.cpp: In function âvoid InputIntoArray(cStudent (&)[30], int&)â:
assign3.cpp:141: error: âfirstâ was not declared in this scope
assign3.cpp:145: error: âlastâ was not declared in this scope
assign3.cpp:146: error: âxâ was not declared in this scope
assign3.cpp:153: error: no match for âoperator=â in âCSCI208Class[N] = TempStuden tâ
assign3.cpp:67: note: candidates are: cStudent& cStudent::operator=(const cStuden t&)

here is the modified 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);
                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)
                {       strcpy(fname,this->fname);}
                void get_lname(Name_t &lname)
                {       strcpy(lname,this->lname);}
                int get_t1(Grade_t &t1)
                {return t1;}
                int get_t2(Grade_t &t2)
                {return t2;}
                int get_t3(Grade_t &t3)
                {return t3;}
                int get_t4(Grade_t &t4)
                {return t4;}
                int get_assigngrade(Grade_t &assigngrade)
                {return assigngrade;}
                int get_examgrade(Grade_t &examgrade)
                {return assigngrade;}
                cStudent & operator=(const cStudent&);
                //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);
        Datafile>>TempStudent[N].set_fname(first);
        while(!Datafile.eof())
        {
                N=0;
                Datafile>>TempStudent[N].set_lname(last);
                Datafile>>TempStudent[N].set_t1(x);
                Datafile>>TempStudent[N].set_t2(x);
                Datafile>>TempStudent[N].set_t3(x);
                Datafile>>TempStudent[N].set_t4(x);
                Datafile>>TempStudent[N].set_assigngrade(x);
                Datafile>>TempStudent[N].set_examgrade(x);

                CSCI208Class[N]=TempStudent;
                N=N+1;
                Datafile>>TempStudent[N].set_fname(first);
        }//end while loop
        cout<<"Closing Datafile............"<<endl;
        Datafile.close();
        cout<<"Begining secondary processes.............."<<endl;

};//end InputIntoArray

do i declare the variables x, first and last inside the function itself? is it ok to declare the"first" and "last" variables in the 'char' type?

I agree with you lerner, in that the typedef is really annoying for me, but again instructors rules...do you know of any applications where using a typedef would be viable? (for my own knowledge)

SasseMan thanks for the reply, but i dont quite understand why "=" wont work. in your code:

cStudent & operator=( const cStudent & );

would i put (const cStudent &TempStudent)?

thanks again for your help I'm just trying to understand programing in general :)

-regards
Atticusr5

yes you need to declare first last and x inside your function in order to use them. what SasseMan was saying is you need to define the operator = for your class. it is a god idea to write your own because the default just dose a shadow copy of the class and this could lead to problems latter on. in his function that he gave you he just had cStudent & operator=( const cStudent & ); . when you declare a function you don't have to supply a variable name unless you are writing it inline. if you write it like your other functions where the definition follows directly after the declaration then you will need to have a variable name.

class foo
{
       //...
       cStudent & operator=( const cStudent & );
       //...
};

cStudent & cStudent::operator=(const cStudent & student)
{
       //  define function here.
}

or

class foo
{
       //...
       cStudent & operator=( const cStudent & student)
      {
              // define function here
       }
       //...
};

You need to store the output from >> somewhere before passing it to the setter methods, your getter methods should be const and line 153 makes no sense

CSCI208Class[N]=TempStudent;

, should probably be indexed

CSCI208Class[N]=TempStudent[N];

I took the liberty of correcting your code, just corrected the compile errors, I have no idea if it will run succesfully. Here it is...

#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;
        TempStudent[N].set_fname(first);
        
        while(!Datafile.eof())
        {
                N=0;
                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

On a sidenote you should really question your teacher if this is the way in which he teaches you C++. I would recommend you buying a copy of C++ Primer, or the C++ book written by Bjarne Stroustrup, which is more advanced.

I noticed on line 144 that you set N=0 inside the while loop. Should probably be outside.

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.