Thread: Help!

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    13

    Unhappy Help!

    Assignment: implement a 3 different classes [student, course, grade] and have the code calculate the total number of units the student is taking and calculate the gpa.


    So far, my code is working, I don't know how to initialize/declare/call [I'm not really familiar with the c++ lingo], inside the Course class, the "vector<char>grades" and the "char course_grade" and "vector <double> units" which is in the header file, inside the "double Grade::gpa() const" function in the main file.

    Help?

    Also. Anymore helpful, useful tips for my code would be appreciated. Thanks!




    Here's the header file.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <iomanip>
    
    using namespace std;
    
    class Grade {
    	Grade (char class_grade){
    		(*this).class_grade = class_grade;
    	}
    	double gpa() const;
    private:
    	char class_grade;
    };
    
    
    
    class Course {
    public:
    	Course (string course_name){
    		(*this).course_name = course_name;
    	}
    	char getGrade() const;
    	{
    		return class_grade;
    	}
    	
    	vector<double> units;	
    	vector<char> grades;
    	char course_grade;
    	
    	void read();
    	void print() const;
    	void addCourse (string course_name, double course_unit, char course_grade);
    	
    private:
    	string course_name;
    	double course_unit;
    	vector<string> courses;
    };
    
    
    class Student {
    public:
    	Student (string student_name){
    		(*this).student_name = student_name;
    	}
    	
    	string getName() const {
    		return student_name;
    	}
    	void read();
    	void print() const;
    private:
    	string student_name;
    	
    };
    Here's the main file.
    Code:
    #include "Hwk5.h"
    
    void Student::read(){
    	cout << "Please enter Student name: ";
    	getline (cin, student_name);
    }
    void Student::print() const{
    	cout << student_name << endl;
    	cout << "================================================" << endl;
    	cout << "Course Name \t \tUnits \tGrade" << endl;
    	cout << "------------------------------------------------" << endl;
    }
    
    void Course::read(){
    	cout << "Please enter the course name: ";
    	getline (cin, course_name);
    	cout << "Please enter the number of units: ";
    	cin >> course_unit;
    	cin.ignore();
    	cout << "Please enter the final grade: ";
    	cin >> course_grade;
    	cin.ignore();
    	addCourse (course_name, course_unit, course_grade);
    }
    
    void Course::addCourse(string course_name, double course_unit, char course_grade){
    	courses.push_back(course_name);
    	units.push_back(course_unit);
    	grades.push_back (course_grade);
    }
    
    void Course::print() const {
    	int i = 0;
    	for (i = 0; i < courses.size(); i++)
    	{
    		cout << courses[i] << "\t \t" << units[i] << "\t" << grades[i] << endl;
    	}
    }
    
    double Grade::gpa() const
    {
    
    }
    	
    
    
    int main (){
    	string answer;
    	
    	Student first ("");
    	first.read();
    	
    	Course second ("");
    	
    	bool more = true;
    	while (more)
    	{
    		second.read();
    		
    		cout << "Would you like to enter another class? (y/n) ";
    		cin >> answer;
    		cin.ignore();
    		if (answer == "n" || answer == "N")
    			more = false;
    	}
    	cout << endl << endl;
    	first.print();
    	second.print();
    		
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Presumably every course should contain a Grade, and every Student should contain several Courses?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    Note. I'm not asking you guys to do the assignment for me. I'm just stuck in a wall right now and would appreciate how to get out of it. Thanks!

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    Are you trying to say that I should create a string variable with the course name?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xforevertink View Post
    Are you trying to say that I should create a string variable with the course name?
    That pretty much has no relation whatsoever to what I posted. Try it again:
    Presumably every Course should contain a Grade, and every Student should contain several Courses?

    Notice that I made no mention of strings, or names.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    Why is it in a form of a question? I was asking how I call the vector (in header file) inside the class Course, in the main function.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't ever, under any circumstances, call a vector. It's not a function, it's just a thing.

    If you want a vector inside your class, then put it there.

    The question mark is because if you had read the first sentence of your very own post, you wouldn't have asked your question.

  8. #8
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Your main should be its own source file and your classes should have their own .h and .cpp files.
    After that follow tabstop's advice.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    I don't want to create another vector.. I want to use the one I already created (header file), inside the main file in the function "double Grade::gpa() const".

    Although my code works, obviously with the given assignment, it's not finished. I'm just wondering how I write the code so that I can use the vector in that function.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try answering the questions other ask you, and you'll get better answers. Try it. It really works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xforevertink View Post
    I don't want to create another vector.. I want to use the one I already created (header file), inside the main file in the function "double Grade::gpa() const".

    Although my code works, obviously with the given assignment, it's not finished. I'm just wondering how I write the code so that I can use the vector in that function.
    Since the assignment as you posted it specifically tells you NOT to use any such thing, it is highly doubtful that such an idea is a good one.

Popular pages Recent additions subscribe to a feed

Tags for this Thread