Hello everyone, I am new to the boards and new to C++. I am reading Accelerated C++ and working through Ch. 4. The program as it stands compiles fine, but it seems I dont completely understand what is going on when I reference another object. The program seems to read in data fine, but it is not ending up where I hoped. I have strayed a bit from the program covered in the book, I thought I might learn more that way. Thats what I get for thinking :/
I am trying to get the data into the students vector. Any help would be much appreciated.
Bob
Code:
//Grading Program
//By Robert Lohaus
//Chapter 4, Accelerated C++
#include <algorithm>
#include <iomanip>
#include <ios>
#include <stdexcept>
#include <string>
#include <vector>
#include "Student_info.h"
using std::cout; using std::string;
using std::endl; using std::sort;
using std::vector; using std::domain_error;
using std::setprecision;
int main()
{
//presents a welcome message
cout << "Welcome Student Grading Program 0.0.1"
<< endl << endl;
//Global Objects
vector<Student_info> students;
string::size_type maxlen = 0;
//Reads Input into a vector
maxlen = read(students);
//alphabetize students
sort(students.begin(), students.end(), compare);
//write the names and grades
for (int i = 0; i != students.size(); ++i)
{
//write name padded on right to maxlen + 1
cout << students[i].name
<< string(maxlen + 1 - students[i].name.size(), ' ');
}
return 0;
}
#ifndef GAURD_Student_info
#define GAURD_Student_info
//Student_info.h
#include <iostream>
#include <vector>
#include <string>
struct Student_info
{
std::string name;
double midterm, final;
std::vector<double> homework;
};
//Function Definitions
std::string::size_type read(std::vector<Student_info>&);
int read_hw(std::vector<double>&);
bool compare(const Student_info&, const Student_info&);
double grade(const Student_info&);
double grade(double, double, const std::vector<double>&);
double grade(double, double, double);
double median(const std::vector<double>&);
#endif
#ifndef GAURD_Read
#define GAURD_Read
//Source for Read and related functions
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include "Student_info.h"
using std::string; using std::endl;
using std::cout; using std::vector;
using std::cin;
//Functions
//read()
//this function reads name,mid,final then passes
//to read_hw to enter homework scores
string::size_type read(vector<Student_info>& s)
{
Student_info record;
string end_cond;
string::size_type mname = 0;
string::size_type maxlen = 0;
//Reads the students name, midterm, final
cout << "Enter a student record? " << endl
<< "Enter 'Y' or 'N': ";
cin >> end_cond;
while (end_cond == "y")
{
cin.clear();
cout << endl << "Student's Name: ";
cin >> record.name;
cout << endl << "Student's Midterm: ";
cin >> record.midterm;
cout << endl << "Student's Final: ";
cin >> record.final;
read_hw(record.homework);
mname = record.name.size();
mname > maxlen ? maxlen = mname:
s.push_back(record);
//This checks for more records
cout << endl << endl
<< endl << "Enter a student record? "
<< endl << "Enter 'Y' or 'N': ";
cin >> end_cond;
}
//returns the longest name for later printing
return maxlen;
}
//read_hw()
//this function reads homework scores and comes up with an average
int read_hw(vector<double>& hw)
{
string end_cond;
double score;
cout << endl << "Do you wish to enter another homework score? "
<< endl << "Press 'Y' or 'N': ";
cin >> end_cond;
while (end_cond == "y")
{
hw.clear();
cout << endl << "Enter a homework grade: ";
cin >> score;
hw.push_back(score);
cout << endl << "Do you wish to enter another homework score? "
<< endl << "Press 'Y' or 'N': ";
cin >> end_cond;
}
return 0;
}
//compare()
//this is used in the sort function
bool compare(const Student_info& x, const Student_info& y)
{
return x.name < y.name;
}
//grade functions
//grade()
//gets Student_info as refence and calls the next grade function
double grade(const Student_info& s)
{
return grade(s.midterm, s.final, s.homework);
}
//grade()
//throws error if no homework grades were added
//passes to median
double grade(double midterm, double final, const std::vector<double>& hw)
{
int oops = 0;
hw.size() == 0 ? throw std::domain_error("Student has done no homework"):
++oops;
return grade(midterm, final, median(hw));
}
//median()
//calculates median for homework grades
double median(const std::vector<double>& v)
{
typedef std::vector<double>::size_type vec_sz;
std::vector<double> vec;
vec = v;
vec_sz size = vec.size();
size == 0 ? std::domain_error("Median of an empty vector"):
std::sort (vec.begin(), vec.end());
vec_sz mid = size/2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}
//grade()
//computes final grade
double grade(double midterm, double final, double homework)
{
return (0.2 * midterm) + (0.4 * final) + (0.4 * homework);
}
#endif