How to link 2 structures

This is a discussion on How to link 2 structures within the C++ Programming forums, part of the General Programming Boards category; HI guys.. i need some help in linking two structures.. basically it means There's a course in a college where ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    33

    How to link 2 structures

    HI guys..

    i need some help in linking two structures..

    basically it means

    There's a course in a college where there are several course sections in it. Students can enroll in the course sections.. but they are limited to 4 course sections per course.

    i have done the structures and have assigned pointers for them, but i just don't know how to link them, so i can add students to the course sections and the course sections to the course..

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct Course
    {
           char courseNumber[6];
           char title[20];
           int creditHour;
           Course *CourseNxt;
    };
    
    struct CourseSection
    {
           char sectionNumber [20];
           int startTime;
           int startDate;
           int roomNumber;
           CourseSection *CourseSectionNxt;
    };
    
    struct Student
    {
           char name [30];
           char studentId [30];
           char major [30];
           Student *StudentNxt;
    };
    
    struct Node
    {
           Student stud;
           CourseSection cs;
           Node *NodeNxt;
    };
    
    Course *course_ptr = NULL;
    Course *course_current;
    
    CourseSection *courseSection_ptr = NULL;
    CourseSection *courseSection_current;
    
    Student *stu_ptr;
    Student *stu_current;
    
    Node *node_ptr;
    Node *node_current;
    
    void addNewCourse();
    void displayCourse();
    void addNewCourseSection();
    int main()
    {
        int input;
     menu:
     cout<<"[1] ADD a Course";
     cout<<"\n[2] ADD a Course Section";
     cout<<"\n[3] DELETE a record";
     cout<<"\n[4] SEARCH by student number";
     cout<<"\n[5] DISPLAY a Course";
     cout<<"\n[6] SORT by name";
     cout<<endl;
     cout<<"what's your choice?";
     cin>>input;
     
     switch(input)
     {
                  case 1:
                       addNewCourse();
                       goto menu;
                       break;
                  case 2:
                       addNewCourseSection();
                       goto menu;
                       break;
                  case 5:
                       displayCourse();
                       goto menu;
                       break;     
                       }
        system("pause");
        return 0;
    }
    
    void addNewCourse()
    {
         Course *temp, *temp2;
         temp = new Course;
         
         cout<<"Course Number:"<<flush;
         cin>>temp->courseNumber;
         cout<<"Title:"<<flush;
         cin>>temp->title;
         cout<<"Credit Hours:"<<flush;
         cin>>temp->creditHour;
         temp->CourseNxt = NULL;
          if (course_ptr == NULL)
           { course_ptr = temp;
    	 course_current = course_ptr;
           }
         else
           { temp2 = course_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->CourseNxt != NULL)
               {  temp2 = temp2->CourseNxt;
                  // Move to next link in chain
               }
             temp2->CourseNxt = temp;
           }
         
         
    }
    
    void displayCourse()
      {  Course *temp;
         temp = course_ptr;
         cout << endl;
         if (temp == NULL)
           cout << "The list is empty!" << endl;
         else
           { while (temp != NULL)
    	   {  // Display details for what temp points to
                  cout<< "Course Number: "<<temp->courseNumber<<endl;;;
                  cout<< "Course Title: "<<temp->title<<endl;;
                  cout<< "Credit Hours:"<<temp->creditHour<<endl;;
    	        
    	      if (temp == course_current)
    		cout << " <-- Current node";
                  cout << endl;
    	      temp = temp->CourseNxt;
    
    	   }
    	 cout << "End of list!" << endl<<endl;
           }
           
                
    }
    
    void addNewCourseSection()
    {
         CourseSection *temp, *temp2;
         temp = new CourseSection;
         
         cout<<"Course Section Number:"<<flush;
         cin>>temp->sectionNumber;
         cout<<"startTime:"<<flush;
         cin>>temp->startTime;
         cout<<"startDate:"<<flush;
         cin>>temp->startDate;
         cout<<"Room Number:"<<flush;
         cin>>temp->roomNumber;
         temp->CourseSectionNxt = NULL;
          if (courseSection_ptr == NULL)
           { courseSection_ptr = temp;
    	 courseSection_current = courseSection_ptr;
           }
         else
           { temp2 = courseSection_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->CourseSectionNxt != NULL)
               {  temp2 = temp2->CourseSectionNxt;
                  // Move to next link in chain
               }
             temp2->CourseSectionNxt = temp;
           }
         
         
    }
    help pls..

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're not going to link anything of the sort, not with those structures. If a student can enroll in four courses, then your student structure is going to need room for four courses (or at least four pointers to sections of courses). If you want to have sections of a course, then your course structure will need to have room for an array of sections (or at least an array of pointers to sections of courses). Et cetera.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    thanks..

    but can't quite understand what you're trying to say.. (a bit beginner at this)..

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    you mean like this??

    Code:
    struct Student
    {
           char name [30];
           char studentId [30];
           char major [30];
           Student *firstCourse, *secondCourse, *thirdCourse, *fourthCourse;
    };

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, no. You probably still need the (one) pointer to the next student, on the assumption you intend to keep your linked list of students (and why wouldn't you). But you will also need, as part of the data that goes with the student (along with name, studentID, and major) four courses.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    Code:
    struct Student
    {
           char name [30];
           char studentId [30];
           char major [30];
           Student *StudentNxt;
           Student *firstCourse, *secondCourse, *thirdCourse, *fourthCourse
    };
    is this correct??

    how can i link the students to the courseSection

    like this

    void addStudent (CourseSection cs, Student stud)
    {
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sivapc View Post
    Code:
    struct Student
    {
           char name [30];
           char studentId [30];
           char major [30];
           Student *StudentNxt;
           Student *firstCourse, *secondCourse, *thirdCourse, *fourthCourse
    };
    is this correct??
    Of course not. Is "firstCourse" supposed to point to a Student?

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    hmm.. i thought the next pointer is to point to the student and course pointers to point to the course sections that the students are taking..

    just dumbfounded, atm.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sivapc View Post
    hmm.. i thought the next pointer is to point to the student and course pointers to point to the course sections that the students are taking..

    just dumbfounded, atm.
    Of course that's what you want. So type that in.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    so what was wrong in

    Student *studentNxt
    Student *firstCourse, *secondCourse etc??

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sivapc View Post
    so what was wrong in

    Student *studentNxt
    Student *firstCourse, *secondCourse etc??
    You don't think a Course is the same thing as a Student, do you?

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    oops.. ok..

    courseSection *firstCourse, *secondCourse, etc??

    like that?

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    er,, do you know how to add students to the course??

    i tried this..

    Code:
    void addStudent (CourseSection cs)
    {
       CourseSection *temp;
       temp = new CourseSection;
        Student *temp1;
        temp1 = new Student;
        
        
     
         
         
         
    }
    but it gives me errors..

    pls lend some help

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Presumably you need to have the student already, and not make up a new one.

    But you would store a pointer to the course in your Student structure (and if you were on the ball you might be able to store a pointer to the student in your CourseSection structure).

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    Code:
    struct CourseSection
    {
           char sectionNumber [20];
           int startTime;
           int startDate;
           int roomNumber;
           Student *studentPointer;
           CourseSection *CourseSectionNxt;
           
    };
    like that??

    still gives me error (ISO C++ forbids declaration of 'Student' with no type)

    :cry:

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 06:23 AM
  2. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 09:13 PM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 06:45 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21