Hi guys
I have been trying to figure out the problem with this program but i couldn't. my program basically crahes everythime I run and I get a seg fault.
I tured in as is, but for learning purposes I want to know what's wrong with and how I should fix it, since the our TA and instructor don't correct lab nor tell you what's wrong
thanks alot
the file attached to my posting .
opps sorry I forgot to include text file

Hint, when you declare a function prototype inside a class, make sure it
has the same signature outside of its class, that is when you define it.

Hey
thanks for responding ...
so the deal is create and remove functions are part of that class, and basically they will have the same functionality as the constructors and destructors. so do you mean the create and remove functions have to have the set and get..sorry i didnt quit understand :-)

I'm having issues with this one here

Do you mind if I post your code here?

of course!!

#include <iostream>

#include <stdlib.h>

#include <fstream>

using namespace std;


const int NUM=4; 


class course

{

	public:
      

     void create();
     

  	void remove() ;



	void setdept(char *);

	char * getdept();



	void setcode(int);

	int  getcode();

	

	void setclassD(char *);

	char *getclassD();



        void setCallN(int);

        int getCallN();



        void setDays(char *);

        char *getDays();



	void setStartTime(int );

	int getStartTime();



	void setEndTime(int);

	int getEndTime();



	void setInstructor(char *);

	char *getInstructor();



	void setSection(int);

	int getSection();



	void setFees(string);

	string getFees();



	void setCredit(int);

	int getCredit();

   





    private:

            

	char * dept;

	int   code;

    int   CallN;

	char *classD;

    char *Days;

	int   StartTime;

	int   EndTime;

	char *Instructor;

	int    Section;

	string Fees;

	int    Credit;

	

	

};

//everytime I use these functions my program crashes 

void course::create()
{

        dept = new char [20];

        classD = new char [50];

        Instructor = new char [50];

        Days   = new char [20];

}
void course::remove()
{
    dept = new char [20];

        classD = new char [50];

        Instructor = new char [50];

        Days   = new char [20];

}



void course::setdept(char* dpt)

{ 
   
   

	int i = 0;

	while(dpt[i]!='\0')

        {

		dept[i]=dpt[i];

		i++;

	}

	dept[i] = '\0';



}

void course::setcode(int kode)

{

	code = kode;

}


void course::setclassD(char * Dscrpt)

{


     //call the creat function here



     int i = 0;

	while(Dscrpt[i]!='\0')

       {

	classD[i]=Dscrpt[i];

	i++;

       }

	classD[i] = '\0';
  

}

void course::setCallN(int call)

{

   CallN = call;


}



void course::setDays(char *day)

{
   //call the creat function here




   int i =0;

   while (day[i]!= '\0')

    {

   Days[i]=day[i];

   i++;

    }

   Days[i]= '\0';



}



void course::setStartTime(int stime)

{

     StartTime= stime;

     

}

void course::setEndTime(int  etime)

{

     EndTime = etime;

     

}

void course::setInstructor(char *teacher)

{    


       int i = 0;

     	while(teacher[i]!='\0')

       {

		Instructor[i]=teacher[i];

		i++;

	}

	Instructor[i] = '\0';
     
}


void course::setSection(int  section)

{

     Section = section;

     

}

void course::setFees(string  fees)

{

     Fees = fees;

     

}



void course::setCredit(int credit)

{

     Credit = credit;

     

}




char* course::getdept()

{

	return dept;

}

int course::getcode()

{	

	return code;

}



char* course::getclassD()

{

       

     return classD;

 }  



int course::getCallN()

{

return CallN;



}

 

char* course::getDays()

{



      return Days;

}





int  course::getStartTime()

{

	return StartTime;

}



char * course::getInstructor()

{

	return Instructor;

}

int  course::getEndTime()

{

	return EndTime;

}

int  course::getSection()

{

	return Section;

}

string course::getFees()

{

	return Fees;

}

int  course::getCredit()

{

	return Credit;

}



void print( course * );
void write_out(course *);



 int main()

  {	

//declaration 



     	course * cptr;

     	cptr = new course [NUM];

        ifstream fin;

    

    	char * dpt;

        char * Dscrpt;

        char * teacher;

        char * day;
        
        //temp 

	    int kode;

        int etime;

        int stime;

        int call;

        int credit;

        int section;

        string fees;

      // file name 

	char * fileName = new char [20]; 

	

	cout << "Please enter the file name " << endl;

	cin >> fileName;    

	

	fin.open(fileName);  

	

	if(!fin.good())		

	{

		cout << "File not found!" << endl;

	

	}



	for(int i=0; i <NUM; i++)

	{

// when I was doing this  fin>> (*cptr).setdept; it was making the program to crash 

	             fin >> dpt;
	             cptr[i].setdept(dpt);


	         	 fin >> kode;
                cptr[i].setcode(kode);


                fin>>section;

                cptr[i].setSection(section);

                fin>>call;

                cptr[i].setCallN(call);

                fin.getline( Dscrpt, 50 );

                cptr[i].setclassD(Dscrpt);


                fin>>day;

                cptr[i].setDays(day);


	          	fin>>stime;

                cptr[i].setStartTime(stime);


                fin>>etime;

                cptr[i].setEndTime(etime);


                fin>>credit;

                cptr[i].setCredit(credit);


                fin>>fees;

                cptr[i].setFees(fees);

                fin.getline( teacher, 50 );

                cptr[i].setInstructor(teacher);

        	

	}

            	fin.close();


	cout << "****** CLASSSES ******" << endl;



	cout << "\n Schedule\n" << endl;


    // print out function 

    print ( cptr );

    // write out the data to the file 
  
     write_out(cptr);

    // call the delete function  remove instead doing it like this :
  
     	delete [] cptr;
      	delete [] dpt;

        delete [] Dscrpt;

        delete [] teacher;

        delete [] day;



     

     





	cout<< " has been written to the file "<<endl;

	return 0;

}




void print( course * cptr )

{



for(int i=0; i<NUM; i++)

	{
		cout << "Class " << (i + 1) << endl;		


		cout << "Course " << ": " <<cptr[i].getdept() << " "<< cptr[i].getcode() << endl;



	    cout<<  "Class Discreption"<< " : " <<cptr[i].getclassD()<<endl;

          

        cout<<  "Call Number " << ": " << cptr[i].getCallN()<<endl;


		cout<< "start and end time  " << ": "<< cptr[i].getStartTime() << " - " << cptr[i].getEndTime()<<endl;



		cout<<  "Instructor " << ": " << cptr[i].getInstructor()<<endl;



		cout<< "Section " << ": " <<cptr[i].getSection()<<endl;



        cout<< "Days " <<  ": " <<cptr[i].getDays()<<endl;



		cout<< "Credits : " <<cptr[i].getCredit()<<endl;



		cout<< "Fees " << ": " <<cptr[i].getFees()<<endl << endl;
       

			

	}	 	

}

void write_out(course * cptr)


{


for ( int i =0; i<NUM; i++)
{
ofstream fout;
fout.open("output.txt");


         cout << "Class " << (i+1) << endl;		


		fout<<"Course " << ": " <<cptr[i].getdept() << " "<< cptr[i].getcode() << endl;


       fout<<  "Class Discreption"<< " : " <<cptr[i].getclassD()<<endl;

          
       fout<<  "Call Number " << ": " << cptr[i].getCallN()<<endl;


		fout<< "start and end time  " << ": "<< cptr[i].getStartTime() << " - " << cptr[i].getEndTime()<<endl;



		fout<<  "Instructor " << ": " << cptr[i].getInstructor()<<endl;



		fout<< "Section " << ": " <<cptr[i].getSection()<<endl;


        fout<< "Days " <<  ": " <<cptr[i].getDays()<<endl;



		fout<< "Credits : " <<cptr[i].getCredit()<<endl;



		fout<< "Fees " << ": " <<cptr[i].getFees()<<endl << endl;
      fout.close();
     }
}

First create a constructor and initialize all member variables.
Make sure you initialize all variables before using them.

First create a constructor and initialize all member variables.
Make sure you initialize all variables before using them.

hey thanks !!
in the past week assignment wasn't required to use the constructors so bsically the instructor want us to write the Create function so we can see how the constructors work ...I honestly tried to fix it but I just don't see it, it might be a very easy fix...but haha i'm super blind now... I approciate your help

First create a constructor and initialize all member variables.
Make sure you initialize all variables before using them.

hey thanks !!
in the past week assignment wasn't required to use the constructors so bsically the instructor want us to write the Create function so we can see how the constructors work ...I honestly tried to fix it but I just don't see it, it might be a very easy fix...but haha i'm super blind now... I approciate your help

Do you mind if I post your code here?

hey you can post if if you want ...please I really need help!!! the problem is we have to add more functionality to this code, for this weeks lab!!! if i dont get this working I will not be able to move on !!! this one was due last week, and I got 82% which wasn't bad at all lol
thanks again !!!

What can I do to help?

i just can't get that this to run like it supposed to !!! basically the program should read in all the data from the file and write out to the screen and a file using classes .

here is a short example of what might help you :

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

class FileReader
{
	string fileName;
	string fileContent;
	bool fileRead;
public :
	FileReader(const string FileName) {
		fileName = FileName; 
		fileContent = "";
		fileRead = false;
	} 	
	bool printFileData();
	bool readFileData();
	void reset(){
		*this = FileReader(fileName);
	}
	void reset(string newFileName){
		*this = FileReader(newFileName);
	}
};

bool FileReader::readFileData()
{			
	//open file for reading
	ifstream  readingFile(fileName.c_str());	

	if(! readingFile ) { //check if file exist
		return false;
	}
	char ch = 0;
	fileRead = true;
	while( readingFile.get(ch)) fileContent += ch;	

	return true;
}
bool FileReader::printFileData()
{
	if( fileRead == false) return false;
	cout << fileContent << endl;
	return true;
}
int main()
{		
	FileReader fileIn("readMe.txt");
	
	if(! fileIn.readFileData() ){
		cout<<"File1 does not exist... exiting...\n";
		return 1;
	}

	fileIn.printFileData();

	fileIn.reset("readMe2.txt"); //give it new file to open
	if( ! fileIn.readFileData()) {
		cout<<"File2 does not exist... exiting...\n";	
		return 1; 
	} 
	
	fileIn.printFileData();

	return 0;
}
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.