Jan 15, 2010 at 7:17pm UTC
Hello can some one help with this code . when ever i run this code it will print
Teacher:
press any key to continue..,,
This is the code
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class cube;
class water;
class wine;
class steak {
private:
char* name;
int cre, hour;
water* tea;
wine* cla;
public:
steak() { next=0; tea=0; cla=0; }
steak( char* na, int c, int h) {
name=na;
cre=c;
hour=h;
next=0;
tea=0;
cla=0;
}
void setName(char *na) {
int len=strlen(na);
name=new char[len+1];
strcpy(name, na);
}
char* getName() { return name; }
int getCre() {return cre;}
int getHour() { return hour; }
wine* getClass() { return cla;}
water* getTeacher() {return tea;}
void setTeacher(water* t) { tea=t;}
void setClass( wine* c) {cla=c; }
void setCre(int cr) {cre=cr; }
void setHour(int h) {hour=h; }
void print();
steak* next;
};
class wine {
private:
char* name;
cube* cous;
public:
wine() { next=0; cous=0; }
wine( char* na ) { name=na; next=0; cous=0; }
void setName( char* na ) {
int len=strlen(na);
name= new char[len+1];
strcpy( name, na );
}
char* getName() { return name; }
void setNode( cube* no ) { cous=no; }
cube* getNode() { return cous; }
wine* contain( char* na ) {
if ( strcmp( na, name ) == 0 )
return this;
else if ( next == 0 ) return 0;
else return next->contain( na );
}
void print();
wine* next;
};
class water{
private:
char* name;
cube* cous;
public:
water() { next=0; cous=0; }
water( char* na ) { name=na; next=0; cous=0; }
void setName( char* na ) { name=na; }
char* getName() { return name; }
void setNode( cube* no ) { cous=no; }
cube* getNode() { return cous; }
water* contain( char* na ) {
if ( strcmp( na, name ) == 0 )
return this;
else if ( next == 0 ) return 0;
else return next->contain( na );
}
void print();
water* next;
};
class cube {
private:
steak* cous;
public:
cube() { cous=0; next=0;}
void setCourse( steak* c) {cous=c; }
steak* getCourse() { return cous;}
cube* next;
};
void steak::print() {
cout <<"Course: " << " ";
cout << cla->getName() <<" ";
cout << name <<" " << cre << " " << hour<< " ";
cout << tea->getName() << endl;
}
void water::print() {
cout <<"Teacher: " << endl;
cube *np = cous;
while ( np != 0 ) {
np->getCourse()->print();
np =np->next;
}
}
void wine::print() {
cout <<"Class: " << endl;
cube *np = cous;
while(np != 0) {
np->getCourse()->print();
np = np->next;
}
}
char *data[6]={"4en21 QPlanning 3 3 Tiuss", "4en21 QReading 3 3 Nenaa",
"4en22 QWriting 5 5 Biuss", "4en23 QMath 2 2 Tiuss",
"4en23 QChinese 6 6 Xentt", "4en22 QSpeech 4 4 Aukky" };
int main() {
steak *head_cou = 0, *coup;
water *head_tea=0,*teap;
wine *head_cla=0,*clap;
int i,j,x;
char *couname,*teaname, *claname;cube *nodep;
for(i=0; i < 6; i++)
{
coup = new steak();
coup->next = head_cou;
head_cou = coup;
char *couname = new char[11];
couname[10] = '\0';
for(j=0;j<10;j++) couname[i] = data[i][j+6];
coup->setName( couname );
char num[2];
num[1] = '\0';
num[0] = data[i][17];
x = atoi( num);
coup->setCre(x);
num[1] = '\0';
num[0] = data[i][19];
x = atoi( num);
coup->setHour(x);
char *teaname = new char[6];
teaname[5] = '\0';
for(j=0;j<5; j++) teaname[j] = data[i][j+21];
if( head_tea != 0)
teap = head_tea->contain(teaname);
else teap=0;
if(teap == 0 ) {
teap = new water();
teap->setName( teaname);
nodep = new cube();
nodep->setCourse( coup );
teap->setNode( nodep );
teap->next = head_tea;
head_tea = teap;
}
else {
nodep = new cube();
nodep->setCourse( coup );
nodep->next = teap->getNode();
teap->setNode(nodep);
}
coup->setTeacher(teap);
char *claname=new char[6];
claname[5]='\0';
for ( j=0; j<5; j++ ) claname[j]=data[i][j];
if ( head_cla !=0 ) clap = head_cla->contain( claname );
else clap = 0;
if ( clap == 0 )
{ clap = new wine();
clap->setName( claname );
nodep = new cube();
nodep->setCourse( coup );
clap->setNode( nodep );
clap->next = head_cla;
head_cla = clap;
}
else {
nodep=new cube();
nodep->setCourse( coup );
nodep->next = clap->getNode();
clap->setNode( nodep );
}
coup->setClass( clap );
}
water *s =new water;
s->print();
s = head_tea->contain("Tiuss");
system("PAUSE");
return 0;
}
Jan 16, 2010 at 1:21am UTC
As far as I can tell the code is working just fine, you're creating a water object s with the default constructor which initialises both pointers as null, so the while loop would never run.
Perhaps you need to check what you're trying to do?