Visual C++ istream (Accelerated C++)

This is a discussion on Visual C++ istream (Accelerated C++) within the C++ Programming forums, part of the General Programming Boards category; I am going through Accelerated C++ using Visual C++ express 2010. In chapter 4 it makes a struct for student ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    21

    Visual C++ istream (Accelerated C++)

    I am going through Accelerated C++ using Visual C++ express 2010. In chapter 4 it makes a struct for student info and supposedly reads names and marks in from a file, and this is where I am stumped. For those who don't have the book, the code is

    Code:
    #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
    Code:
    #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)
    {
        is >> s.name >> s.midterm >> s.final;
    
        read_hw(is,s.homework);     //read and store all the students' homework grades
        return is;
    }
    
    istream& read_hw(istream& in, vector<double>& hw)
    {
        if(in)
        {
            //get rid of previous contents
            hw.clear();
    
            //read homework grades
            double x;
            while(in>>x)
             hw.push_back(x);
    
            //clear the stream so that the input would work for the next student
            in.clear();
        }
    
        return in;
    }
    Code:
    #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
    Code:
    #include <stdexcept>
    #include <vector>
    #include "grade.h"
    #include "median.h"
    #include "Student_info.h"
    
    using namespace std;
    
    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);
    }
    Code:
    #ifndef GUARD_median_h
    #define GUARD_median_h
    
    #include <vector>
    double median(std::vector<double>);
    
    #endif
    Code:
    #include "median.h"
    #include <vector>
    #include <algorithm>
    
    using std::istream;
    using std::vector;
    using std::sort;
    using std::domain_error;
    
    double median(std::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];
    }
    and the main class
    Code:
    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    #include <ios>
    #include <stdexcept>
    #include <string>
    #include <vector>
    #include "grade.h"
    #include "Student_info.h"
    
    using std::cout;
    using std::cin;
    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;
    
    	while (read(cin, record)) {
    		maxlen = max(maxlen, record.name.size());
    		students.push_back(record);
    		cout << "entered";
    		cout << students[0].name << string(maxlen+1 - students[0].name.size(), ' ');
    	}
    	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(), ' ');
    
    			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;
    	}
    	cin.ignore();
    	cin.get();
    	return 0;
    }
    Initially I get a blank screen, I can input some values but no way of printing results out.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Some prompts would be nice.
    It seems you have to enter something like
    bob 69 74
    to enter the name, midterm and final.
    Then you enter a bunch of other numbers:
    56 76 66 84
    Then you have to enter a ctrl-Z (on windows; I believe it's ctrl-D on unix) on a line by itself and hit Enter to end the input of the "homework" numbers.
    Then you start again with a name and two numbers, a few other numbers, ctrl-Z and Enter.
    Then to end the input for the whole thing you hit another ctrl-Z and Enter.
    At THAT point it prints the names and final grade.

    This part
    Code:
    		cout << "entered";
    		cout << students[0].name << string(maxlen+1 - students[0].name.size(), ' ');
    will always print out the first person's name, though. So you probably want an index variable that you increment each time through the loop.
    Last edited by oogabooga; 02-02-2012 at 06:11 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Accelerated Questions Chapter 1.
    By Kitt3n in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2010, 06:23 PM
  2. help with a exercise of Accelerated C++
    By roelof in forum C++ Programming
    Replies: 14
    Last Post: 06-11-2010, 09:45 AM
  3. Exercise 5.10 (ACCELERATED C++)
    By pantera in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2010, 08:46 AM
  4. Help with Accelerated C++ exercise
    By s_siouris in forum C++ Programming
    Replies: 5
    Last Post: 01-24-2010, 02:10 PM
  5. reading Accelerated C++
    By Akkernight in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2008, 10:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21