help cleaning up code(newbie stuff)

I

isaac2004

hi i was assigned to write a program for my class that brought in data
from a file and sorted and formatted it into to outputs. it consists
of three classes called courses, quarters and students. i was
wondering if i can get some help making my code more efficient. it
works i just want to get good style and problem solving techniques.
here is my code

////---------Area for Includes////////////////////////////////////
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

////////Area for Variables///////////////////////////////////////

/* All these variables are for is to set up the formatting of the
output file
All are positions on a line of output
*/


const int Name_Length = 19; //max length of name string
const int Quarter_ID_Length = 4; // max length of an ID for a
Quarter
const int Course_ID_Length = 8; // max length for an ID for a Course
const int Credits_Length = 1; // max length of a string of credits
const int Grades_Length = 2; // max length of a grade string
const int Start_Name = 0; // starting position of the name on the
output line
const int Start_Quarter_ID = 19; // starting position of the Quarter
ID on the output line
const int Start_course_ID = 24; // starting position of the Course
ID on the output line
const int Start_Credits = 34; // starting position of the Credits
on the output line
const int Start_Grade = 39; // starting position of the Grade on
the output line

/////////////////////////////////////////////////////////////////////

string intToString (int x) {
ostringstream output_string;
output_string << x;
return output_string.str();
}
/* Changes a Integer to a string
Pre - int x is brought in
Post - returns the integer converted to a string
*/

int stringToInt (string x) {
istringstream input_string(x);
int i;
input_string >> i;
return i;
}
/* Changes a string to an integer
Pre - string x is brought in
Post - returns the string converted to an integer
*/

//////////Class for the Course Portion

class Course {
public:
Course (string id, int crds, string grd);
/* Constructor Function
Pre - id is a string, crds is an int, and grd is a string as
well
Does not return but creates a Course Object
*/
string toString() const;
/* Formats a string properly for Output
Pre - string is broght in
Post - returns the the string in proper format
*/
string getId() const;
/* Grabs the ID of a Course
Pre - string is brought in
Post - returns the the ID in the form of a string
*/
bool operator< (const Course& right) const;
/*
Pre - Course Object is brought in
Post - returns a boolean if an id is less than the id of the
course object next to it
*/
private:
string course_id;
int credits;
string grade;
};

//--------------Implementation of Class

// constructor
Course::Course (string id, int crds, string grd)
:course_id(id), credits(crds), grade(grd){
}

bool Course::eek:perator< (const Course& right) const {
return course_id < right.course_id; // check if course_id is less
than course.course_id
}

string Course::toString() const {
return " " + course_id + " " + intToString (credits) + " "
+ grade + "\n"; // string is fixed now
}

string Course::getId() const{
return course_id; // return quarter id
}

////////////Class for Quarter Portion

class Quarter {
public:
Quarter (string id);
/* Constructor of a Quarter
Pre - string is brought in
Post - Returns a partial string with a value
*/
void addCourse (string course_id, int credits, string grade);
/* Mutator makes adds a course object with a id, credit amount
and a grade to the quarter
Pre - string called course_id is brought in, as well as an int
for credits and another string for a grade
Does not return just changes the object
*/
string getId() const;
/* Grabs the ID of a Quarter
Pre - string is brought in
Post - returns the the Quarter in the form of a string
*/
string toString() const;
/* Function to clean up the format of a Course string for output
Pre - Course string is brought in
Post - returns a string that has been cleaned up for proper
output
*/
void sort();
/* Crappy algorithm i found online for bubble sort
Pre - Brings in a vector of type quarters
Post - returns vector sorted by course name
*/
bool operator< (const Quarter& right) const;
/* Pre - Quarter Object is brought in
Post - returns a boolean if an id is less than the id of the
quarter object next to it
*/
private:
string quarter_id_Input_format() const;
string quarter_id;
vector<Course> courses;
};

//----------Implementation of Quarter Class


// constructor
Quarter::Quarter (string id) : quarter_id(id){
}

string Quarter::quarter_id_Input_format() const {
string s;

if (quarter_id[0] == 'W' || quarter_id[0] == 'w')
s = "1";
else if (quarter_id[0] == 'S' || quarter_id[0] == 's')
s = "2";
else
s = "3";
// conditional to give value of quarters via time of taken
return quarter_id.substr (1, 3) + s; // returns partial string of
quarter_id concatenated with the value of the id
}

bool Quarter::eek:perator< (const Quarter& right) const {
return quarter_id_Input_format() < right.quarter_id_Input_format();
}

void Quarter::addCourse (string course_id, int credits, string grade)
{
courses.push_back (Course (course_id, credits, grade)); // adds the
courses vector to each quarter object
} // adds the records to the course object

string Quarter::getId() const {
return quarter_id;
}
// function just returns the id of the quarter

string Quarter::toString() const {
string s = " " + quarter_id + "\n"; // formats the way the
quarter will look
for (int x = 0; x < (int)(courses.size()); x++) //loop through
courses vector intil done
s = s + courses[x].toString(); // loops through formatting s
return s;
}
// naive bubble sort from the internet
// brings in vector and sorts via course name
void Quarter::sort() {
for (int x = 0; x < (int)(courses.size() - 1); x++){
for (int y = x + 1; y < (int)(courses.size()); y++) {
if (courses[y] < courses[x]) { // if one string is less than
the other
Course temp = courses[x]; // next few lines move the too
or swap
courses[x] = courses[y];
courses[y] = temp;
}
}
}
}

////////////Class for Student Portion

class Student {
public:
Student (string name);
/* Constructor of a Student
Pre - string is brought in
does not return just creates object
*/
string getName() const;
/* Constructor of a Quarter
Pre - string is brought in
Post - Returns a string for the name
*/
void addCourse (string quarter_id, string course_id, int credits,
string grade);
/* Mutator makes adds a course object with a id, credit amount and
a grade to the quarter
Pre - string called course_id is brought in, as well as an int
for credits and another string for a grade
Does not return just changes the object
*/
string toString() const;
/* Function to clean up the format of a student string for output
Pre - student string is brought in
Post - returns a string that has been cleaned up for proper
output
*/
void sort();
/* Crappy algorithm i found online for bubble sort
Pre - Brings in a vector of type quarters
Post - returns vector sorted by quarter
*/
bool operator< (const Student& right) const;
/* Pre - Student Object is brought in
Post - returns a boolean if a name is less than the name of the
student object next to it
*/
private:
int getQuarterIndex (string quarter_id);
string name;
vector<Quarter> quarters;
};

//---------Implementation of Student Class

// constructor
Student::Student (string name) : name(name) {
}

bool Student::eek:perator< (const Student& right) const {
return name < right.name; // returns a boolean representing if name
is less than the object next to it
}

void Student::addCourse (string quarter_id, string course_id, int
credits, string grade) {
int x = getQuarterIndex (quarter_id);
quarters[x].addCourse (course_id, credits, grade); // adds quarters
on the end of that
}

string Student::getName() const {
return name;// returns name of the object
}

int Student::getQuarterIndex (string quarter_id) {
for (int x = 0; x < (int)(quarters.size()); x++) {
if (quarter_id == quarters[x].getId())// if quarter_id variable
is equal to the id of array part
return x; // then return index of
array
}
quarters.push_back (Quarter (quarter_id)); // adds the quarter to
the student record
return (int)(quarters.size() - 1);
}

string Student::toString() const {
string s = name + "\n";
for (int x = 0; x < (int)(quarters.size()); x++)
s = s + quarters[x].toString(); // makes a call to itself with a
concatentation of the string
return s;
}
// sort again this time it sorts by strength of a quarter
void Student::sort() {
for (int x = 0; x < (int)(quarters.size()); x++){
for (int y = x + 1; y < (int)(quarters.size()); y++) {
if (quarters[y] < quarters[x]) { // if one value is lower
than the other
Quarter temp = quarters[x]; // next lines are for
swapping
quarters[x] = quarters[y];
quarters[y] = temp;
}
}
}
}

//////////helper functions for main

int getStudentIndex (string name, vector<Student>& students) {
/* brings in a string from input and compares to the vector
Pre - string brought in to compare
Post - int created that represented index of vector
*/
for (int x = 0; x < (int)(students.size()); x++) {
if (name == students[x].getName())
return x;
}
students.push_back (Student (name)); // continue process
return (int)(students.size() - 1);
}

bool readLine (ifstream& input, vector<Student>& students) {
string name, quarter_id, course_id, grade;
int credits;
string column;
/* reads in a stream from an input file
Pre - Input file is needed
Post - Puts and orders all data into the structure that is built
and returns true
*/
if (!getline (input, column)) return false; // if end of stream
name = column.substr (Start_Name, Name_Length); // grabs range of
values and puts them in name
quarter_id = column.substr (Start_Quarter_ID,
Quarter_ID_Length); // grabs range of values and puts them in
quarter_id
course_id = column.substr (Start_course_ID, Course_ID_Length);//
grabs range of values and puts them in course_id
credits = stringToInt (column.substr(Start_Credits,
Credits_Length));// grabs range of values and puts them in credits
if (column.length() == Start_Grade + 1)// if length of column
position is equal to max length + 1 then
column += " "; // extend column
grade = column.substr (Start_Grade, Grades_Length);// grabs range
of values and puts them in grade
int i = getStudentIndex (name, students); // calls function and
puts returned int into i
students.addCourse (quarter_id, course_id, credits, grade);//
finally adds courses into student object

return true;
}

string toString (const vector<Student>& students) {
/* turns the vector into a string
Pre - brings in the vector
Post - converts the vector to a string
*/
string s = "";
for (int i = 0; i < (int)(students.size()); i++)
s = s + students.toString();
return s;
}
//same sort from earlier, just changed to use Students
void sort (vector<Student>& students) {
/* sorts a vector
Pre - brings in a vector
Post - sorts the vector and does not return anything
*/
for (int i = 0; i < (int)(students.size()); i++){
for (int j = i + 1; j < (int)(students.size()); j++) {
if (students[j] < students) {
Student temp = students;
students = students[j];
students[j] = temp;
}
}
}
}

/////////////////////////////////////////////////////////////////////

int main() {
ifstream input;
input.open ("grades.dat");
vector<Student> students;

while (readLine (input, students));
// loops through and reads in the file and adds it to a vector

sort (students);// sorts the vector
cout << toString(students); // outputs vector

return 0;
}

here is an example of a dat file brought in


Smith, Sue S'04 CSCI 204 4 B+
Anderson, Adam S'03 EDAD 450 2 D-
Madison, Mitch W'04 PHYS 123 5 B
Anderson, Adam F'03 ENG 310 3 C-
Anderson, Adam W'02 AMST 204 3 A-
Anderson, Adam F'03 ESCI 154 4 B
Anderson, Adam F'03 ART 101 3 B+
Madison, Mitch F'03 GEOL 350 5 F

the output should be all sorted from students, quarters and classes.
thanks for the help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top