Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 323469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:02:02+00:00 2026-05-12T09:02:02+00:00

I was writing a sample example involving multiple files. The detailed code is as

  • 0

I was writing a sample example involving multiple files. The detailed code is as follows.

main.cpp

#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 student's 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 the 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 to 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;
}

Student_info.{h,cpp}

#ifndef GUARD_Student_Info
#define GUARD_Student_Info

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

using std::iostream;
using std::string;
using std::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
#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;
}

grade.{h,cpp}

#ifndef GRADE_H
#define 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 <stdexcept>
#include <vector>

#include "median.h"
#include "grade.h"


double grade(const Student_Info& s)
{
    return grade(s.midterm,s.final,s.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(double midterm,double final,double homework)
{
    return 0.2*midterm + 0.4*final + 0.4*homework;
}

median.{h,cpp}

#ifndef MEDIAN_H
#define MEDIAN_H

#include <vector>           
#include <stdexcept>        //to get the declaration of domain_error
#include <algorithm>        //to get the declaration of sortt

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

double median(std::vector<double>);

#endif
#include "median.h"

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

The problem is when I compile it using g++ on linux and run ./a.out nothing happens. This is strange. I have gone over the code but couldn’t find anything untoward. Hopefully someone can find the glitch.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:02:02+00:00Added an answer on May 12, 2026 at 9:02 am

    By “nothing happens”, do you mean that it’s waiting for input and doesn’t exit?

    while(read(cin,record)) sure looks like that to me. Pass something via standard input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 176k
  • Answers 176k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Having unit tests that are slow is always bad. Having… May 12, 2026 at 3:16 pm
  • Editorial Team
    Editorial Team added an answer \s is a whitespace character, such as a tab or… May 12, 2026 at 3:16 pm
  • Editorial Team
    Editorial Team added an answer A cell can be highlighted (on touchDown) or selected (on… May 12, 2026 at 3:16 pm

Related Questions

Looking for some direction here as I'm running into some migration problems. We have
I'm writing a 7 card poker hand evaluator as one of my pet projects.
First post, so here goes. I'm writing a script that does intelligent search and
I am writing a program in Objective-C and I need to make web requests

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.