Solved

Debug my program

Posted on 1997-04-24
3
251 Views
Last Modified: 2008-03-06
Thanks.  Tonight I'll check your answers

You wrote ... I'd leave the code in ...
Could you complete this sentence?
You may always reply by e-mail

tom.cornelis@glo.be


Below you find an assignment with its solution.
There are still 3 errors, ( I indicated them with //?? and a description ),
could you correct them?
Could you adjust  the program so the program asks in case 3 (3. Retrieve
all data) for the name of the student from whom you want to retrieve the
data?
Is this a good solution?


Assignment:


      Develop a class where the following data is                 kept:
            +name student
            +street
            +zipcode
            +city
            +course (number, title, number of                                  hours); the number of courses is not
             known in advance

      Next seperate member functions must be used:
            +input of the personal data
            +addition of a course; courses must be                                    kept in a binary tree
            +retrieve all data; courses must be                                          shown in the sequence of their
             course number

Solution:


class CCOURSE
{
      int iNumber;
      int iHours;
      char *cName[100];      
      CCOURSE *Uppointer;            
      CCOURSE *Leftpointer;
      CCOURSE *Rightpointer;
public:
      CCOURSE ( );
      ~CCOURSE ( );       
      void setData(int newNumber, int newHours, char *newName);
      void printCourse ( );
};



CCOURSE::CCOURSE (void)
{
      Uppointer=Leftpointer=Rightpointer=0;
}



CCOURSE::~CCOURSE
//??Multiple declaration for 'CCOURSE::~CCOURSE'
{
        if ( Leftpointer ) delete Leftpointer;
        if ( Rightpointer ) delete Rightpointer;
}



void CCOURSE::printCourse()
{
      if ( Leftpointer ) Leftpointer->printCourse ( );
            cout<<"name: "<<cName<<"\n"
            <<"of "<<iHours<<" hours\n";
      if ( Rightpointer ) Rightpointer->printCourse ( );
}



void CCOURSE::setData(int newNumber, int newHours, char *newName)
{
      iNumber=newNumber; iHours=newHours; strcpy(cName,newName);
}



class CPERSONAL
{
      CPERSONAL *nextStudent;
      char name[ 100 ];
      char street[ 100 ];
      char zipcode[ 100 ];
      char city[ 100 ];
      CCOURSE* root;

      public:
      CPERSONAL ( );
      ~CPERSONAL ( );
      void setData( /*char* Name, char* Street, char* ZipCode, char*
      City*/  /*what you need*/);
      void printCourse ( );
};



CPERSONAL::CPERSONAL ()
//?? Type qualifier 'CPERSONAL' must be a struct or class name
//?? Declaration terminated incorrectly
{
        Root=NULL;
}



CPERSONAL::~CPERSONAL
{
      if ( Root ) delete Root;
}



void CPERSONAL::printCourse ( )
{
      cout << "Name:  " << CPERSONAL.name << endl;
      cout << "Street:  " << CPERSONAL.street << endl;
      cout << "Zipcode:  " << CPERSONAL. zipcode << endl;
      cout << "City:  " << CPERSONAL.city << endl;

      cout<<"Courses";
      if ( Root )
      {
            cout<<":\n"; Root->printCourse();
      }
      else cout<<" haven't defined yet!\n";
}



void CPERSONAL::setData ( )
{
      cout << " INPUT OF PERSONAL DATA " << endl;
      cout << " Please enter name " << endl;
      cin >> Name >> endl;
      cout << " Please enter street " << endl;
      cin >> Street >> endl;
      cout << " Please enter ZipCode " << endl;
      cin >> ZipCode >> endl;
      cout << " Please enter City "
      cin >> City >> endl;
}



void Display_Menu (&iChoice)
{
      do
      {
      cout << "WELCOME TO THE PROGRAM" << endl;
      cout << "Enter your choice" << endl;
      cout <<  "1. Enter a new student" << endl;
      cout <<  "2. Add a course" << endl;
      cout <<  "3. Retrieve all data" << endl;
      cout <<  "4. Quit" << endl;
      cin >> iChoice;
      }
      while ( iChoice4 );
}



void Retrieve_all_data(CPERSONAL *student)
{
      cout << "Here are all data:" << endl;

      if ( student!=NULL ) {
            do
                  {
//?? remove this line?      student->printCourse ( );
                  student=student->next;
                  }
            while ( student!=NULL );
            }
      else
            {
            cout << " You haven't entered a student yet" << endl;
            }
      
      if student->root==NULL
            {
                  cout << " You haven't entered any courses" << endl;
            }
      else
            {
                  cout << " Courses:" << endl;
                  student->printCourse ( );

            }
}



void CCOURSE::addCourse(CCOURSE *newOne)
{
      if ( iNumber > newOne->iNumber )
            {
            if ( LeftDownCourse==NULL )
                  {
                  LeftDownCourse=newOne;
                  newOne->UpCourse=this;
                  }
            else LeftDownCourse->addCourse(newOne);
            }
      else
            {
            if  ( RightDownCourse==NULL)
                  {
                  RightDownCourse=newOne;
                  newOne->UpCourse=this;
                  }
            else RightDownCourse->addCourse(newOne);
            }
}



void CCOURSE::setData ( )
{
      CCOURSE *newCourse;
      newCourse=new CCOURSE;

      cout<< "Give courseNumber" << endl;
      cin>> CCOURSE.iNumber >> endl;
      cout<< "Give # hours" << endl;
      cin>> CCOURSE.iHours >> endl;
      cout<< "give Title" << endl;
      cin>> CCOURSE.sTitle >> endl;
}



void main (void)
{
      CPERSONAL *student=NULL;
      CPERSONAL *newStudent;
      int iChoice;

      do
      {
            Display_Menu (iChoice)
            switch (iChoice)
            {
                  case 1 :
//?? no declaration, OK?
                  newStudent=new CPERSONAL;
                  newStudent->setData ( );
                  newStudent->next=student;
                  student=newStudent;
                  break;

                  case 2 :

                  newCourse->setData ( );
                  student->addCourse(newCourse);
                  break;
                  
                  case 3 :

                  Retrieve_all_data( *student );
                  break;

                  case 4 :

                  cout << "Exiting program" << endl;
                  break;
            }
      }
      while ( iChoice!=4 );


      while ( student!=NULL )
      {
            newStudent=student->next;
            delete student;
            student=newStudent;
      }

}






0
Comment
Question by:student040597
  • 2
3 Comments
 

Author Comment

by:student040597
ID: 1162950
Edited text of question
0
 

Accepted Solution

by:
Apocalypse earned 20 total points
ID: 1162951
I'm not sure if this is an error or a typo, but you don't
you need parans for the destructor definition?? You're missing them from the code you've given.

The second error looks fine, I can't see anything wrong with. Make sure it's in the proper position so that the definition is after the declaration.

The third and fourth question marks look fine, I'd leave the code in....

I hope this helps a little bit

As for the third error
0
 

Author Comment

by:student040597
ID: 1162952
Edited text of question
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question