reading input into function problem
From: ht (bonm_at_plo.col)
Date: 06/28/04
- Next message: David White: "Re: Why does vector.end() point beyond last element?"
- Previous message: Francis Glassborow: "Re: Why does vector.end() point beyond last element?"
- Next in thread: Ben Measures: "Re: reading input into function problem"
- Reply: Ben Measures: "Re: reading input into function problem"
- Reply: Edo: "Re: reading input into function problem"
- Reply: ht: "Re: reading input into function problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 27 Jun 2004 23:11:47 +0100
Im stuck with this program (win32 console). i am trying to produce an
output of a student(s) final grade with name, with only storing the
name and final grade of each student in a stucture. the computation of
the grade has to take place within the input of the grades.
if anyone can help could you use the terminology i have used here
(struct, double, vector etc) as these are only what i have been
introduced to so far in the book im learning from.
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
using std::max; using std::cin; using std::cout; using
std::domain_error;
using std::endl; using std::istream; using std::ostream;
using std::setprecision; using std::setw; using std::sort;
using std::streamsize; using std::string; using std::vector;
struct Student_info {
string name;
double final_grade;
};
double median(vector<double> vec)
{
typedef vector<double>::size_type vec_sz;
vec_sz size = vec.size();
if (size == 0)
throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end());
vec_sz mid = size/2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
double grade(double midterm, double final, const vector<double>& hw)
{
if (hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));
}
double read(istream& is, Student_info& s, double midterm, double
final, vector<double>& hw)
{
is >> s.name >> midterm >> final;
if (is){
double x;
hw.clear();
while (is >> x)
{
hw.push_back(x);
}
}
is.clear();
double a = grade(midterm, final, hw);
return a;
}
bool compare(const Student_info& x, const Student_info& y)
{
return x.name < y.name;
}
int main()
{
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
double midterm = 0;
double final = 0;
double final_grade = 0;
vector<double> homework;
while (read(cin, record, midterm, final, homework))
{
maxlen = max(maxlen, record.name.size());
students.push_back(record);
record.final_grade = final_grade;
}
sort(students.begin(), students.end(), compare);
for (vector<Student_info>::size_type i = 0;
i != students.size(); ++i)
{
cout << students[i].name
<< string(maxlen + 1 - students[i].name.size(), ' ');
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade
<< setprecision(prec);
cout << endl;
}
return 0;
}
- Next message: David White: "Re: Why does vector.end() point beyond last element?"
- Previous message: Francis Glassborow: "Re: Why does vector.end() point beyond last element?"
- Next in thread: Ben Measures: "Re: reading input into function problem"
- Reply: Ben Measures: "Re: reading input into function problem"
- Reply: Edo: "Re: reading input into function problem"
- Reply: ht: "Re: reading input into function problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|