Solved

Debug my program

Posted on 1997-04-24
3
221 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
Comment Utility
Edited text of question
0
 

Accepted Solution

by:
Apocalypse earned 20 total points
Comment Utility
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
Comment Utility
Edited text of question
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now