![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 13
| 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;
};
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 | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Presumably every course should contain a Grade, and every Student should contain several Courses? |
| tabstop is offline | |
| | #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 | |
| | #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 | |
| | #5 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
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 | |
| | #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 | |
| | #7 |
| and the Hat of Guessing 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 | |
| | #8 |
| Webhead 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 | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
| | #11 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
![]() |
| Tags |
| access, c++, class, vectors |
| Thread Tools | |
| Display Modes | |
|