Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   why do get segmentation fault ? help plz !!! (http://www.go4expert.com/forums/segmentation-fault-help-plz-t19911/)

Adilo 28Oct2009 23:22

why do get segmentation fault ? help plz !!!
 
:disappoin
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();
}
}
*/


xpi0t0s 28Oct2009 23:49

Re: why do get segmentation fault ? help plz !!!
 
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.

Adilo 29Oct2009 00:19

Re: why do get segmentation fault ? help plz !!!
 
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

xpi0t0s 29Oct2009 01:24

Re: why do get segmentation fault ? help plz !!!
 
How did you try to initialise dpt?

Adilo 29Oct2009 01:38

Re: why do get segmentation fault ? help plz !!!
 
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 ... !!!

xpi0t0s 29Oct2009 01:40

Re: why do get segmentation fault ? help plz !!!
 
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?

Adilo 29Oct2009 01:52

Re: why do get segmentation fault ? help plz !!!
 
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

xpi0t0s 29Oct2009 02:02

Re: why do get segmentation fault ? help plz !!!
 
Must be the classD stuff then. Obviously I know nothing about memory corruption caused by writing to memory with an uninitialised pointer.

xpi0t0s 29Oct2009 02:09

Re: why do get segmentation fault ? help plz !!!
 
Here's a thought: why not initialise dpt to zero? i.e. char *dpt=0; Then what happens?

Adilo 29Oct2009 02:51

Re: why do get segmentation fault ? help plz !!!
 
:nonod: still the same issue !!!


All times are GMT +5.5. The time now is 00:18.