Hey I just got this book Accelerated c++ and it seems pretty good, but I'm having one problem in chapter 4. When I compile my grading program in chapter 4. I can't get the final version to work. I believe I'm supposed to enter a students name followed by grades, but I don't know how to create a condition that makes the first while loop stop, so I can show I'm done entering names and grades. I included the files that I'm working with. If you have the book they're the exact same.

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

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

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 grade(const Student_info& s)
{
       return grade(s.midterm, s.final, s.homework);
}
#ifndef GUARD_grade_h
#define GUARD_grade_h

// 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
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"

using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
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;  // the length of the longest name
    
    // read and store all 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)) {
        // find length of longest name
        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 to maxlen +1 characters
         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;
}
// source file for the median function
#include <algorithm>    // to get the decleration of sort
#include <stdexcept>    // to get the decleration of domain_error
#include <vector>       // to get the decleration of vector

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

// compute the median of a vector<double>
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];
}
#ifndef GUARD_median_h
#define GUARD_median_h

// median.h
#include <vector>
double median(std::vector<double>);

#endif
// source file for Student_info-related functions
#include "Student_info.h"

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

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

istream& read(istream& is, Student_info& s)
{
         // read and store the student's name and midterm and final exam grades
         is >> s.name >> s.midterm >> s.final;
         
         read_hw(is, s.homework);  // read and store all the student's homework grades
         return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
         if (in) {
                 // get rid of previous content
                 hw.clear();
                 
                 // read homework grades
                 double x;
                 while (in >> x)
                     hw.push_back(x);
                 
                 // clear the stream so that input will work for the next student
                 in.clear();
         }
         return in;
}
#ifndef GUARD_Student_info
#define GUARD_Student_info

// Student_info.h header file
#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
int main()
{
    vector<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;  // the length of the longest name
    
    // read and store all 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)) {
        // find length of longest name
        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 to maxlen +1 characters
         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;

Here are the functions for the read(cin, record)

istream& read(istream& is, Student_info& s)
{
         // read and store the student's name and midterm and final exam grades
         is >> s.name >> s.midterm >> s.final;
         
         read_hw(is, s.homework);  // read and store all the student's homework grades
         return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
         if (in) {
                 // get rid of previous content
                 hw.clear();
                 
                 // read homework grades
                 double x;
                 while (in >> x)
                     hw.push_back(x);
                 
                 // clear the stream so that input will work for the next student
                 in.clear();
         }
         return in;
}

If you need anything else please let me know.

The read() function returns the stream, so you should be able to signal EOF to stop the first loop. On Linux it's a Ctrl+D key combination and on Windows it's Ctrl+Z.

Comments
Quick help

How about using a sentinnel/flag? A little bit of a hassle to enter an additional keystroke, but it is completely portable, offers complete control without using magical combinations, etc.

bool moreInput = true;
char temp;
while(moreInput)
{
   //get input
   cout << "enter y to keep going, anything else to stop" << '\n';
   cin >> temp;
   if(temp == 'y')
     moreInput = false;
}

Alright when I push ctrl+z it displays this ^Z. Then I hit enter a few times and decided to just type in done or something random. Then it displayed the names and grades. Any ideas?

oh nvm I see I had too end two loops. Alright whew that works out. Thank you all for all your help.

bool moreInput = true;
char temp;
while(moreInput)
{
   //get input
   cout << "enter y to keep going, anything else to stop" << '\n';
   cin >> temp;
   if(temp == 'y')
     moreInput = false;
}

I always would write the following line: if(temp == 'y') as if( [B]tolower(temp)[/B] == 'y') because it also allows the user to enter an uppercase 'Y' :)

This question has already been answered. Start a new discussion instead.