Hi guys!!!
everytime I try to read the course description from the file it crashes, (fin>>Des; (*cptr).setclassD(Des);) I even used the getline function and still not workling ... maybe this a very easy fix but I have been trying to fix it for hours and now I'm turning to you guys . the test file should look like " CPE 202 Introduction to computer "
any thought would be great thanks a lot
#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];
dept[0]= '\0';
code = 0;
}
course::~course()
{
delete [] dept;
dept = '\0';
code = 0;
}
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;
}
char* course::getdept()
{
return dept;
}
int course::getcode()
{
return code;
}
void course::setclassD(char * Des)
{
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;
}