0

I have code that simply computes the final grade of students. When I run it in CodeBlocks I have no issue. However when I copied it in Visual Studio (wanted to try the IDE), I get the error vector subscript out of range.

in main.cpp:

#include <iostream>
#include <vector>
#include <stdexcept>
#include "info.h"
#include "grade.h"

int main()
{
    std::cout << "enter name and grades" << std::endl;
    std::vector<info> students;
    info record;
    while (read(std::cin, record))
    {
        students.push_back(record);
    }

    for (std::vector<info>::size_type i = 0; i < students.size(); i++)
    {
        std::cout << students[i].name << " ";
        try
        {
            double finalgrade = grade(students[i]); // ERROR
            std::cout << finalgrade;
        }
        catch (std::domain_error e)
        {
            std::cout << e.what();
        }
        std::cout << std::endl;
    }
    return 0;
}

In info.h:

#ifndef GUARD_info
#define GUARD_info

#include <string>
#include <vector>


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

std::istream& read_hw(std::istream&, std::vector<double>&);
std::istream& read(std::istream&, info&);

#endif

In info.cpp:

#include <iostream>
#include <vector>
#include "info.h"

std::istream& read_hw(std::istream& in, std::vector<double>& hw)
{
    if (in)
    {
        hw.clear();
        double x;
        while (in >> x)
        {
            hw.push_back(x);
        }
        in.clear();
    }
    return in;
}

std::istream& read(std::istream& in, info& stu)
{
    in >> stu.name >> stu.midterm >> stu.final;
    read_hw(in, stu.homework);
    return in;
}

I have median.cpp, median.h, grade.cpp and grade.h files that contain the functions calculating median and final grades (keeping them out as post is getting long).

I think the issue isn't the code itself (as it works fine on CodeBlocks) but its compatibility with different compilers. I haven't found a satisfactory answer yet and would be glad to get input on what might be causing the bug (visual studio builds it without error, but fails when displaying the final grades). Thank you.

EDIT: here are the grade.cpp, grade.h, median.cpp and median.h files:

In grade.cpp:

#include <vector>
#include <stdexcept>
#include "median.h"
#include "info.h"

double grade(double midterm, double final, double hw)
{
    return 0.2*midterm + 0.4*final + 0.4*hw;
}

double grade(double midterm, double final, const std::vector<double>& hw)
{
    if (hw.size() == 0)
        throw std::domain_error("Student has done no homework.");
    return grade(midterm, final, median(hw));
}

double grade(const info& stu)
{
    return grade(stu.midterm, stu.final, stu.homework);
}

In grade.h:

#ifndef GRADE_H_INCLUDED
#define GRADE_H_INCLUDED

#include <vector>
#include "info.h"
#include <stdexcept>

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

#endif

In median.cpp:

#include <vector>
#include <stdexcept>

double median(std::vector<double> vec)
{
    if (vec.size() == 0)
        throw std::domain_error("Median of an empty vector.");

    typedef std::vector<double>::size_type vec_sz;
    vec_sz size = vec.size();
    vec_sz mid = size / 2;

    return size % 2 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
}

In median.h:

#ifndef GUARD_median_h
#define GUARD_median_h

#include <vector>

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

#endif
12
  • 1
    What value do you see if you step through with the debugger? Commented Feb 5, 2015 at 6:37
  • VS makes lots more checks than codeworks to help you to find nasty bugs. What's the definition of grade() ? Commented Feb 5, 2015 at 6:44
  • 1
    Yes, you should step through the grade() function and make sure the homework vector is being indexed correctly. Commented Feb 5, 2015 at 7:04
  • @Christophe thanks, I've added the definition of grade() as an edit. Commented Feb 5, 2015 at 22:29
  • @ben I get a "debug assertion failed" pop up window right after the first student's name is printed. The windows indicates "vector subscript out of range", however when I change the call to grade() for a call to students[i].midterm, it prints the midterm grade with no issue (so the vector subscript doesn't seem to be out of range). I've added the definition of grade(), thanks for your input ben. Commented Feb 5, 2015 at 22:34

1 Answer 1

1

I mistakenly wrote in my median() function

return size % 2 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];

when I should have written

return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];

Thanks MarkU!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.