why do get segmentation fault ? help plz !!!

Light Poster
28Oct2009,23:22   #1
Adilo's Avatar

Hello Guys
I have been working on for hours and hours and I need help becuase I'm stuck . this program which might be super simple for you guys, basically it will read data from the file like " CPE 202 Introduction to Computer Science". and will cout to the screen.
mine works but when I just read in CPE 202 but when I try to add the the third character, I get seg fault and it crashes...
Code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;

class course
  {
    public:
     course ();//constructor
    ~course();//destructor 
      
        
    void setdept(char *);
    char * getdept();

    void setcode(int);
    int  getcode();
   
    void setclassD(char * );
    char * getclassD();
    void print() const ;


    private:
            
    char * dept;
    int   code;
    char * classD;
        
 };

course::course()
{
dept = new char [20];
classD = new char [20];

classD[0] = '\0';
dept[0]= '\0';
code = 0;
}

course::~course()
{
delete [] dept;
delete [] classD;
dept = '\0';
classD= '\0';
code = 0;


}

 void course::setdept(char* dpt)
 { 
 
    dept = new char [20];
    int i = 0;
    while(dpt[i]!='\0')
        {
        dept[i]=dpt[i];
        i++;
    }
    dept[i] = '\0';

 }
 void course::setcode(int kode)
 {
    code = kode;
 }

 char* course::getdept()
 {
    return dept;
 }
int course::getcode()
{    
    return code;
}

void course::setclassD(char * Des)
{

    
    Des = new char [20];
    int i = 0;
    while(Des[i]!='\0')
        {
        classD[i]=Des[i];
        i++;
    }
    classD[i] = '\0';

}

char * course::getclassD()
{

 return classD;
 }

 int main()
  {    
//declaration 
        ifstream fin;
        int NUM=1; 
        int i;
        int kode;
           course * cptr;
        char * Des;
        char * dpt;
        
         cptr = new course [NUM];
 
 
    fin.open("course.txt");    
    
    
    for(i=0; i <NUM; i++)
    {
                fin >>dpt;
                 (*cptr).setdept(dpt);

                fin >> kode;
                (*cptr).setcode(kode);

                fin.getline( Des, 50 );
               (*cptr).setclassD(Des);
                  
    }

    cout << " <Schedule> " << endl;
    cout<<endl;

    for(i=0; i<NUM; i++)
    {
        cout << "Course " << ": " <<(*cptr).getdept() << " "<< (*cptr).getcode() << endl;
        cout<<endl;        
        cout<< " class Des " << ":" <<(*cptr).getclassD()<<endl;    
    }         
 system("pause");    
     
    return 0;
}


// copy constructors
/* 
course::course(const course &rhs)
{
dept = rhs.dept;
code = rhs.code'
classD = rhs.classD;
 

dept = new char [len+1];
//deep copy 
while (rhs.dept[i] ='\0')
{
strcopy();
}
}
*/
Mentor
28Oct2009,23:49   #2
xpi0t0s's Avatar
char * dpt; defines dpt as a pointer to a char, but you never initialise this pointer anywhere. So when you use it at fin >>dpt; this writes to unknown memory.
Light Poster
29Oct2009,00:19   #3
Adilo's Avatar
thanks for responding !!!
I tried to but it doesn't work still !!! at least that what i get !!!
the problem start when I add the classD !!!
plz help
Mentor
29Oct2009,01:24   #4
xpi0t0s's Avatar
How did you try to initialise dpt?
Light Poster
29Oct2009,01:38   #5
Adilo's Avatar
oh !!! I must doing it wrong then because I was declaring it char * dpt;
and i get an error that I'm re declaring it again !!!
seriously I'm being blind now after over 6 hours trying to see what's wrong ... !!!
Mentor
29Oct2009,01:40   #6
xpi0t0s's Avatar
You have to set it equal to something. I don't know what you're trying to do, but at a guess you want to store some characters in memory, would that be correct? If so, how many characters? If not, what is the point of dpt and what exactly do you want to do with it?
Light Poster
29Oct2009,01:52   #7
Adilo's Avatar
when I comment out classD stuff, the program works fine, but as soon as i add it it crashes, dpt is just a temporary pointer that will store dept( department which is CPE). so when i run just the two i get CPE 202 . as soon as I add classD to get the word " introduction to computer science" the program crashes !!!
let me know if you need more clarifications thanks for your help
Mentor
29Oct2009,02:02   #8
xpi0t0s's Avatar
Must be the classD stuff then. Obviously I know nothing about memory corruption caused by writing to memory with an uninitialised pointer.
Mentor
29Oct2009,02:09   #9
xpi0t0s's Avatar
Here's a thought: why not initialise dpt to zero? i.e. char *dpt=0; Then what happens?
Light Poster
29Oct2009,02:51   #10
Adilo's Avatar
still the same issue !!!