: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(); } } */
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.
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
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 ... !!!
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?
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
Must be the classD stuff then. Obviously I know nothing about memory corruption caused by writing to memory with an uninitialised pointer.