C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-26-2009, 11:14 PM   #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();
		
}
xforevertink is offline   Reply With Quote
Old 07-26-2009, 11:16 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Presumably every course should contain a Grade, and every Student should contain several Courses?
tabstop is offline   Reply With Quote
Old 07-26-2009, 11:17 PM   #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!
xforevertink is offline   Reply With Quote
Old 07-26-2009, 11:19 PM   #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?
xforevertink is offline   Reply With Quote
Old 07-26-2009, 11:23 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-27-2009, 12:25 AM   #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.
xforevertink is offline   Reply With Quote
Old 07-27-2009, 12:37 AM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-27-2009, 12:40 AM   #8
Webhead
 
Spidey's Avatar
 
Join Date: Jul 2009
Posts: 278
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.
Spidey is offline   Reply With Quote
Old 07-27-2009, 12:46 AM   #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.
xforevertink is offline   Reply With Quote
Old 07-27-2009, 02:27 AM   #10
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Try answering the questions other ask you, and you'll get better answers. Try it. It really works.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-27-2009, 02:46 AM   #11
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Reply

Tags
access, c++, class, vectors

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 10:46 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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