Answered Object Oriented Design

  • Tuesday, May 08, 2012 9:08 AM
     
      Has Code

    Hi,

    I need to write a course class & student class and run them using a driver.

    I've written my codes, and the program runs fine, however, i want to enrol 3 students into my course but it doesn't seem to work. The program only manages to check the 1st student, but not the 2nd and 3rd student. I have no idea what is wrong with my program.

    I hope someone could help me with this problem.

    heres my codes

    Course.cpp
    
    #include <iostream>
    #include <vector>
    #include <string>
    #include "Course.h"
    using namespace std;
    //Constructor
    Course::Course()
    {
    }
    Course::Course(string a)
    {
        CourseName=a;
    }
    //destructor
    Course::~Course()
    {
    }
    //checks student enrolled with ID
    bool Course::isEnrolled(string StudentID)
    {
        for (int i=0; i<students.size(); i++)
            {
                    if (students[i].getStudentID() == StudentID)
                    {
                            return true;
                    }
                    else
                    {
                            return false;
                    }
            }
    }
    //stores student in vector
    void Course::enrolStudents(Student student)
    {
        students.push_back(student);
    }
    
    
    
    Course.h
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include "Student.h"
    #ifndef COURSE_H
    #define COURSE_H
    class Course
    {
        public:
        // constructor
        Course();
        Course(std::string a);
        // destructor
        ~Course();
        //declaration of private methods
        bool isEnrolled(std::string StudentID);
        void enrolStudents(Student student);
        private:
        std::string CourseName;     //Course Name
        std::vector<Student> students;   //vector to store students
    }; 
    #endif  // end of class definition
    
    
    
    Student.cpp
    
    #include <iostream>
    #include <string>
    #include "Student.h"
    using namespace std;
    // constructor for your class
    Student::Student()
    {
    }
    Student::Student(string newID, string newName)
    {
            ID = newID;
            Name = newName;
    }
    // destructor for your class
    Student::~Student()
    {
    }
    //Accessor Functions
    string Student::getStudentID()
    {
            return ID;
    }
    string Student::getStudentName()
    {
            return Name;
    }
    //Sets the student name attribute data/Mutator
    void Student::setStudentName(string newName)
    {
            Name = newName;
    }
    //Sets the student ID attribute data/Mutator
    void Student::setStudentID(string newID)
    {
            ID = newID;
    }
    //displays the output
    void Student::display()
    {
            cout << "Name: "<< Name << endl;
            cout << "ID: " << ID << endl;
    }
    
    
    
    Student.h
    
    #include <iostream>
    #include <string>
    using namespace std;
    #ifndef STUDENT_H
    #define STUDENT_H
    class Student
    {
        public:
        // constructor
        Student();
        Student(string, string);
        // destructor
        ~Student();
        //Accessors
        string getStudentName();
        string getStudentID();
        //declaration of private methods
        void setStudentName(string newName);
        void setStudentID(string newID);
        void display();
        private:
        string ID;  //Student ID
        string Name;    //Student Name
    }; 
    #endif
    
    
    
    driver.cpp
    
    #include "Student.h"
    #include "Course.h"
    int main()
    {
            Student student;
            student.setStudentID("123");
            student.setStudentName("abc");
            student.display();
            Course course;
            Student a("Tom" , "1");
            course.enrolStudents(a);
            cout<<course.isEnrolled("Tom");
            Student b("Jas" , "2");
            course.enrolStudents(b);
            cout<<course.isEnrolled("Jas");
            course.enrolStudents(Student("Kio" , "3"));
            cout<<course.isEnrolled("Kio");
            cout << "check" << endl;
            string check;
            cin >> check;
            if(course.isEnrolled(check))
            cout << "Students Enrolled" << endl;
            Else
            cout << "Students not Enrolled" << endl;
            return 0;
    }


    • Edited by ashboi3 Tuesday, May 08, 2012 9:08 AM
    •  

All Replies

  • Tuesday, May 08, 2012 9:20 AM
     
     Proposed Answer Has Code

    You return immediatly:

    bool Course::isEnrolled(string StudentID)
    {
        for (int i=0; i<students.size(); i++)
            {
                    if (students[i].getStudentID() == StudentID)
                    {
                            return true;
                    }
                    else
                    {
                            return false;
                    }
            }
    }

    This should be:

    bool Course::isEnrolled(string StudentID)
    {
        for (int i=0; i<students.size(); i++)
            {
                    if (students[i].getStudentID() == StudentID)
                    {
                            return true;
                    }
            }
    
        return false;
    }

    • Proposed As Answer by Helen ZhaoModerator Wednesday, May 09, 2012 5:40 AM
    • Marked As Answer by ashboi3 Wednesday, May 09, 2012 10:58 AM
    • Unmarked As Answer by ashboi3 Wednesday, May 09, 2012 11:19 AM
    •  
  • Wednesday, May 09, 2012 5:59 AM
     
     Answered Has Code

    I also remark an issue with your include guards. Please your header includes in your include guards, eg:

    #ifndef _NAMESPACE_FILE_H_
    #define _NAMESPACE_FILE_H_
    #pragma once //for the compilers that support it (VS does)
    
    #include <blah>
    ...
    
    #endif //_NAMESPACE_FILE_H_

  • Wednesday, May 09, 2012 11:01 AM
     
      Has Code

    thanks, that solved my problem.

    however, i'm trying to create a driver that read an input.txt file instead of manually typing each student into the driver.

    I'm unable to get the driver to read off any data from the input.txt, i hope some1 could help me out with this

    heres my input.txt and my driver.cpp

    Tom 123
    Jas 456
    Kio 789

    #include<iostream>
    #include<fstream>
    #include<string>
    #include "Course.h"
    #include "Student.h"
    
    using namespace std;
    
    int main(){
    
    Course course;
    string Name,temp; string ID; 
    	ifstream myfilestream;
    	myfilestream.open("input.txt");
    	if (myfilestream.fail())
    	{
    		cout<<"filestream open failed\n";
    		return -1;
    	}
    			
    	do
    	{
    		myfilestream>>temp;
    		myfilestream>>Name;
    		Name+=temp;
    		myfilestream>>ID;
    		course.enrolStudents(Student(Name,ID));
    				
    	}
    	while(myfilestream.good());
    	myfilestream.close();
    	cout<<"Enter to check if student is enrolled\n";
    	string check;
    	cin>>check;
    	if(course.isEnrolled(check))
    		cout<<"Student " << check << "is currently enrolled" << endl;
    	else
    		cout<<"Student not enrolled" << endl;
    
    	return 0;
    }

  • Thursday, May 10, 2012 6:47 AM
     
     

    You should read these references:
    http://www.cplusplus.com/reference/iostream/ifstream/
    http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

    This is standard ISO C++, and there is a learning curve to it...

  • Thursday, May 10, 2012 7:56 AM
     
     

    I've read those references, however i still do not understand why my driver.cpp doesn't read the input.txt file to enrol the students into the course

    could you further elobrate more on it?

  • Thursday, May 10, 2012 10:47 AM
     
     Proposed Answer
    The first thing to note is that you're not testing
    for failure of the input stream in the right place.
    The fundamental rule is: Always test after every I/O
    request is attempted to see if it failed, and do so
    *before* trying to use any data that may have been
    retrieved from an input operation.

    You are doing stream extractions (>>) and *assuming*
    that they will always succeed by trying to use the
    data before checking to see if a failure occurred.

    You also are not checking at the end to see whether
    file reading ended due to eof having been reached
    or because of a read error.

    You have two fields per student in the file,
    but you are extracting three strings from the
    file each iteration.

    Tom 123
    Jas 456
    Kio 789

    do {
       myfilestream>>temp; // What is this for?
       myfilestream>>Name;
       Name+=temp;
       myfilestream>>ID;
    ...

    - Wayne