Trouble With Grading Program

This grading program is from Accelerated C++ chapter 4. The program files were too difficult for me to put together piece by piece the way they wanted me to. I ended up finding them on the web and copy and pasted them. When I run the program I get these errors. Please can someone tell me how do I fix the errors?

1>
Build started: Project: exercises, Configuration: Debug Win32
1> grade.cpp
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\student_info.h(7): error C2011: 'Student_info' : 'struct' type redefinition
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\student_info.h(7) : see declaration of 'Student_info'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\grade.cpp(28): error C2027: use of undefined type 'Student_info'
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\student_info.h(7) : see declaration of 'Student_info'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\grade.cpp(28): error C2228: left of '.midterm' must have class/struct/union
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\grade.cpp(28): error C2027: use of undefined type 'Student_info'
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\student_info.h(7) : see declaration of 'Student_info'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\grade.cpp(28): error C2228: left of '.final' must have class/struct/union
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\grade.cpp(28): error C2027: use of undefined type 'Student_info'
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\student_info.h(7) : see declaration of 'Student_info'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\grade.cpp(28): error C2228: left of '.homework' must have class/struct/union
1> exercises.cpp
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\student_info.h(7): error C2011: 'Student_info' : 'struct' type redefinition
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\student_info.h(7) : see declaration of 'Student_info'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\exercises.cpp(24): error C2079: 'record' uses undefined struct 'Student_info'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\exercises.cpp(31): error C2664: 'read' : cannot convert parameter 2 from 'int' to 'Student_info &'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\exercises.cpp(31): fatal error C1903: unable to recover from previous error(s); stopping compilation
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#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::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;
	}
	return 0;
}// main.cpp
// source file for `Student_info'-related functions
#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) 
{
	// read and store the student's name and midterm and final exam grades
	is >> s.name >> s.midterm >> s.final;
	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<double>'
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 input will work for the next student
		in.clear();
	}
	return in;
}// Student_info.cpp
// `Student_info.h' header file
#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>&);
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using std::domain_error;  
using std::vector;


// compute a student's overall grade from midterm and final exam grades and homework grade
double grade(double midterm, double final, double homework) 
{
	return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}

// compute a student's overall grade from midterm and final exam grades
// and vector of homework grades.
// this function does not copy its argument, because `median' does so for us.
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);
}// grade.cpp
// `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&);
// source file for the `median' function
#include <algorithm>    // to get the declaration of `sort'
#include <stdexcept>    // to get the declaration of `domain_error'
#include <vector>       // to get the declaration of `vector'
using std::domain_error;   
using std::sort;   
using std::vector;

#include "median.h"

// compute the median of a `vector<double>'
// note that calling this function copies the entire argument `vector'
double median(vector<double> vec)
{
#ifdef _MSC_VER
	typedef std::vector<double>::size_type vec_sz;
#else
	typedef vector<double>::size_type vec_sz;
#endif

	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];
}// median.cpp
// `median.h'--final version
#include <vector>

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

Comments

  • This is a common problem of including the same header file (student_info.h in your case) more than once without using header guards. To prevent that add header guards in student_info.h. You can define any name you wish, I like to define it with the name of the header file, but that's only my convention.
    #ifndef _STUDENT_INFO_H
    #define _STUDENT_INFO_H
    // rest of header file goes here
    
    #endif
    
  • Ok that fixed that, but now all I get is a blank command prompt.
  • It's probably on line 16 of student_info.cpp. Put a prompt just before that line to tell you what to enter.
  • You mean cout as in what the person should enter in on the keyboard. What should I ask of the person entering in grades?
    It does not help that I do not know what the program is suppose to do.
  • read lines 15 and 16, they tell you what to enter.
  • Ok I read it here is what I did. All it did was give me error stuff.
  • That looks exactly like what you posted before, what error(s) did it give you?
  • I added cin if you look closer, so here is the error it gives me.

    1>
    Build started: Project: exercises, Configuration: Debug Win32
    1> Student_info.cpp
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\exercises\exercises\student_info.cpp(18): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1053): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,signed char *)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1060): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,signed char &)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1067): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,unsigned char *)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1074): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,unsigned char &)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1097): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1104): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1111): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char *)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1118): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char &)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(1128): or 'std::basic_istream<_Elem,_Traits> &std::operator >><char,std::char_traits<char>,std::istream>(std::basic_istream<_Elem,_Traits> &&,_Ty &)' [found using argument-dependent lookup]
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Ty=std::istream
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(179): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(185): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(192): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(199): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(218): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(253): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(272): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(298): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(316): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(334): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(354): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__int64 &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(373): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(392): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(411): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(429): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(447): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(466): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> while trying to match the argument list '(std::istream, std::istream)'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  • It is missing a couple include files
    #include <string>
    #include <vector>
    
Sign In or Register to comment.