Thread: Calculating grades

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Calculating grades

    I compiled this program from Accelarated C++ chapter 4 that calculates grades of students using functions and structures.

    Its 7 files so i didnt post it all it would take up too much place here.
    The program's missing output when entering datas so i took it that we had to integrate it ourselves thus so did i.
    main.cpp
    Code:
    #include <algorithm>
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    #include <vector>
    #include "grade.h"
    #include "Student_info.h"
    #include "median.h"
    
    using std::cin;                     using std::setprecision;
    using std::cout;                    using std::sort;
    using std::domain_error;            using std::streamsize;
    using std::endl;                    using std::string;
    using std::max;                     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;
        }
        system ("pause");
        return 0;
    }
    Student_info.cpp
    Code:
    // source file for Student_info-related functions
    #include "Student_info.h"
    
    using std::istream;  using std::vector; 
    using std::cout;     using std::endl;  
    
    bool compare(const Student_info& x, const Student_info& y)
    {
        return x.name < y.name;
    }
    
    istream& read(istream& is, Student_info& s)
    {   //here's where i added output info
        // read and store the student's name and midterm and final exam grades
        cout<<"Enter name: ";
        is >> s.name;
        cout<<endl;
        cout<<"Enter midterm grade: "<<endl;
        is >> s.midterm;
        cout<<endl;
        cout<<"Enter final grade: "<<endl;
        is >> s.final;
        cout<<endl;
        
        read_hw(is, s.homework);  // read and store all the student's homework grades
        return is;
    }
    
    // read homework grades from an input stream into a `vector'
    istream& read_hw(istream& in, vector<double>& hw)
    {
        if (in) {
            // get rid of previous contents
            hw.clear();
    
            // read homework grades
            double x;
            cout << "Enter homework grades (end with eof): ";
            while (in >> x)
                hw.push_back(x);
    
            // clear the stream so that input will work for the next student
            in.clear();
        }
        return in;
    }
    My problem is when we're in the first loop of Enter name... etc it works well
    but for the second time it just outputs "Enter name" and didnt wait for input
    it outputs "Enter midterm grade" right away.
    I can post the rest of the files if needed. Thanks.
    Last edited by Ducky; 03-14-2008 at 05:53 AM.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a lot C programming help PLEASE!
    By copelli39 in forum C Programming
    Replies: 11
    Last Post: 04-21-2009, 09:37 PM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Grades program has me lost
    By adrea in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2002, 08:35 PM
  5. Replies: 13
    Last Post: 08-15-2002, 09:20 AM