Reputation: 273
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
Upvotes: 0
Views: 529
Reputation: 273
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!
Upvotes: 1