reading input into function problem

From: ht (bonm_at_plo.col)
Date: 06/28/04


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;
}



Relevant Pages

  • hlookup function
    ... I have a worksheet with students' midterm grade.The instructor would ... If student misses Exam and has a zero? ... If student has a grade 1-119 points, increase their grade by 25 points ...
    (microsoft.public.excel.worksheet.functions)
  • EZ Q 4 U: How do I change a number to text, based on the number
    ... I have the score results for a midterm. ... professors has assigned grade cutoffs ... LaTonya ...
    (microsoft.public.excel.worksheet.functions)
  • Re: [C] structures
    ... > (each record has the fields: name, id, and grade) for up ... > before token" error pointing to the struc student line. ... the idiom to define a struct in C is ... I suggest you to think about representing id's by strings. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: reading input into function problem
    ... >> output of a studentfinal grade with name, with only storing the ... >> name and final grade of each student in a stucture. ... The Ultimate Truth is that there is no Ultimate Truth ...
    (alt.comp.lang.learn.c-cpp)
  • Re: reading input into function problem
    ... > name and final grade of each student in a stucture. ... > (struct, double, vector etc) as these are only what i have been ... After you exhaust all your effort and still want more explicit help with ...
    (alt.comp.lang.learn.c-cpp)