• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
End-Of-File question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

End-Of-File question


  • Subject: End-Of-File question
  • From: Richard Rehl <email@hidden>
  • Date: Fri, 21 Sep 2007 15:52:01 -0400

Newbie here with a question regarding a C++ project from "Accelerated C++" by Koening/Moo. This is a program that's supposed to read a name and a set of grades, terminated by entering end-of-file. It turns out I need to enter it twice before the program actually does what it's supposed to do - calculate and display the final grade. I'm assuming the problem is somewhere in the read_hw function defined in Student_info.cpp. Here's a sample output:

Please enter your first name, followed by your midterm and final grades: richard
90
100
Please enter your homework grades, followed by end-of-file: 90
90
90
∂
Please enter your first name, followed by your midterm and final grades: ∂
Please enter your homework grades, followed by end-of-file: richard 94


and here are the various files:

------------------------------------------------------------------------ ------
main.cpp


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <ios>
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "Student_info.h"

using std::cin;
using std::cout;
using std::endl;
using std::domain_error;
using std::max;
using std::setprecision;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;

int main ()
{
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
//read and store the students' data
//Invariant: students contains all the student records read so far
// maxlen contains the length of the longest name in students
while (read(cin, record)) {
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}
//alphabetize the student records
sort(students.begin(), students.end(), compare);


    //write the names and grades
    for (vector<Student_info>::size_type i = 0;
         i != students.size(); ++i) {
        //write the name, padded on the right
        cout << students[i].name
             << string(maxlen + 1 - students[i].name.size(), ' ');

        //compute and write the grade
        try {
            double final_grade = grade(students[i]);
            streamsize prec = cout.precision();
            cout << setprecision(3)
                 << final_grade << setprecision(prec);
        } catch (domain_error e) {
            cout << e.what();
        }
        cout << endl;
    }
    return 0;
}

------------------------------------------------------------------------ ----
Student_info.h


#ifndef GUARD_Student_info
#define GUARD_Student_info

#include <iostream>
#include <string>
#include <vector>

struct Student_info {
    std::string name;
    double midterm, final;
    std::vector<double> homework;
};

bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
#endif

------------------------------------------------------------------------ ----

Student_info.cpp


#include "Student_info.h"

using std::istream;
using std::vector;
using std::cout;

bool compare(const Student_info& x, const Student_info& y)
{
    return x.name < y.name;
}

istream& read(istream& is, Student_info& s)
{
cout << "Please enter your first name, followed by your midterm and final grades: ";
is >> s.name >> s.midterm >> s.final;
cout << "Please enter your homework grades, followed by end-of- file: ";
read_hw(is, s.homework);
return is;
}


istream& read_hw(istream& in, vector<double>& hw)
{
    if(in)
    {
        hw.clear();

        double x;
        while(in >> x)
            hw.push_back(x);
        in.clear();
    }
    return in;
}

------------------------------------------------------------------------ ----------------------

grade.h

#ifndef GUARD_grade_h
#define GUARD_grade_h

#include <vector>
#include "Student_info.h"

double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);

#endif

------------------------------------------------------------------------ ------------------------

grade.cpp

#include "grade.h"
#include <stdexcept>
#include <vector>
#include "median.h"
#include "Student_info.h"

using std::domain_error;
using std::vector;

double grade(const Student_info& s)
{
    if (s.homework.size() == 0)
        throw domain_error("Student has done no homework");
    return 0.2 * s.midterm + 0.4 * s.final + 0.4 * median(s.homework);
}



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: End-Of-File question
      • From: Laurence Harris <email@hidden>
  • Prev by Date: Re: Objective C++ and ObjC exceptions
  • Next by Date: Shared lib load/unload functions
  • Previous by thread: Re: App crashes when debugging, not in normal execution
  • Next by thread: Re: End-Of-File question
  • Index(es):
    • Date
    • Thread