473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

In file included from grade.h

Hello

I have attempted the source code from Chapter 4 in Accelerated C++ page 70 but keep getting the above error. I'm not sure whether it is in the way I have saved the header files or because my structure is wrong all together. I have included the code.

I have created 3 header files, 3 extra source files and the main source file. I have attached all of them. Please let me know how and where I should save these files to get the programme to function.

Many Thanks

Lannon


main.cpp
Expand|Select|Wrap|Line Numbers
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <ios>
  4. #include <vector>
  5. #include <string>
  6. #include <stdexcept>
  7. #include <iostream>
  8. #include "grade.h"
  9. #include "Student_info.h"
  10.  
  11. using std::cin;          using std::cout;       using std::endl;
  12. using std::setprecision; using std::sort;       using std::vector;
  13. using std::max;          using std::domain_error;      using std::string;
  14. using std::streamsize;
  15.  
  16. int main()
  17. {
  18.     vector<Student_info> students;
  19.     Student_info record;
  20.     string::size_type maxlen = 0;           // LENGTH OF THE LONGEST NAME
  21.  
  22.     // read and store all the students data
  23.     // Invariant: students contains all the students records read so far
  24.     //            maxlen contains the length of the longest name in students
  25.     while (read(cin, record))  {
  26.           // find the length of the longest name
  27.           maxlen = max(maxlen, record.name.size());
  28.           students.push_back(record);
  29.           }
  30.  
  31.     // alphabetize the student records
  32.     sort(students.begin(), students.end(), compare);
  33.  
  34.     // write the names and grades
  35.     for (vector<Student_info>::size_type i = 0; i!= students.size(); ++i) {
  36.  
  37.         // write the name padded to the right by maxlen + 1 characters
  38.         cout << students[i].name
  39.              << string(maxlen + 1 - students[i].name.size(), ' ')
  40.  
  41.         // compute and write the grade
  42.         try {
  43.             double final_grade = grade(students[i]);
  44.             streamsize prec = cout.precision();
  45.             cout << setprecision(3) << final_grade
  46.                  << steprecision(prec);
  47.                  } catch (domain_error e) {
  48.                          cout << e.what();
  49.                          }
  50.                          cout << endl;
  51.             }
  52.    return 0;
  53. }
// grade.h header file
Expand|Select|Wrap|Line Numbers
  1. #ifndef GUARD_grade_h
  2. #define GUARD_grade_h
  3.  
  4. // grade.h
  5. #include <vector>
  6. #include "Student_info.h"
  7.  
  8. double grade(double, double, double);
  9. double grade(double, double, const std::vector<double>&);
  10. double grade(const Student_info&);
  11.  
  12. #endif
  13.  
// median.h header file
Expand|Select|Wrap|Line Numbers
  1. #define GUARD_median_h
  2.  
  3. // median.h
  4. #include <vector>
  5. double median(std::vector<double>);
  6.  
  7. #endif
  8.  
// Student_info header file
Expand|Select|Wrap|Line Numbers
  1. #ifndef GUARD_Student_info
  2. #define GUARD_Student_info
  3.  
  4. // Student_info.h header file
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9.  
  10. struct Student_info {
  11.     std::string name;
  12.     double midterm, final;
  13.     std::vector<double> homework;
  14.     };
  15.  
  16.     bool compare(const Student_info&; const Student_info&);
  17.     std::istream& read(std::istream&, Student_info&);
  18.     std::istream& read_hw(std::istream&, std::vector<double>&);
  19.  
  20. #endif    
// definition of functions for headers
// median.cpp
Expand|Select|Wrap|Line Numbers
  1. // source file for the median function
  2. #include <stdexcept>    // to get the declaration of domain_error
  3. #include <vector>       // to get the declaration of vector
  4. #include <algorithm>    // to get the declaration of sort
  5.  
  6. using std::domain_error;    using std::vector;      using std::sort;
  7.  
  8. // compute the median of a vector<double>
  9. double median(vector<double> vec)
  10. {
  11.     typedef vector<double>::size_type vec_sz;
  12.  
  13.     vec_sz size = vec.size();
  14.     if (size == 0)
  15.             throw domain_error("median of an empty vector");
  16.  
  17.     sort(vec.begin(), vec.end());
  18.  
  19.     vec_sz mid = size/2;
  20.  
  21.     return size % 2 == 0 ? (vec[mid] + vec[mid -1] / 2 : vec[mid];
  22. }
//definition for grade.cpp
Expand|Select|Wrap|Line Numbers
  1. #include <stdexcept>
  2. #include <vector>
  3. #include "grade.h"
  4. #include "median.h"
  5. #include "Student_info.h"
  6.  
  7. using std::domain_error;    using std::vector;
  8.  
  9. // source file for the grade function
  10. double grade(double midterm, double final, double homework)
  11. {
  12.     return 0.2 * midterm + 0.4 * final + 0.4 * homework;
  13. }
  14.  
  15. // compute a students grade from midterm and final grades
  16. // and vectorof homework grades
  17. // this function does not copy its argument, becuase median does so for us
  18. double grade(double midterm, double final, const vector<double>& hw)
  19. {
  20.     if (hw.size() == 0)
  21.             throw domain_error("student has done no homework");
  22.     return grade(midterm, final, median(hw));
  23. }
  24.  
  25. double grade(const Student_info& s)
  26. {
  27.     return grade(s.midterm, s.final, s.homework);
  28. }
//definition for Student_info
Expand|Select|Wrap|Line Numbers
  1. //source file for Student_info related functions
  2. #include "Student_info.h"
  3.  
  4. using std::istream;     using std::vector;
  5.  
  6. bool compare(const Student_info& x, const Student_info& y)
  7. {
  8.     return x.name < y.name;
  9. }
  10.  
  11. istream& read(istream& is, Student_info& s)
  12. {
  13.     // read and store the students name, midterm and final exam grades
  14.     is >> s.name >> s.midterm >> s.final;
  15.  
  16.     read_hw(is, s.homework); // read and store all the students homework grades
  17.     return is;
  18. }
  19.  
  20. // read homewrok grades from an input stream into a vector<double>
  21. istream& read_hw(istream& in, vector<double>& hw)
  22. {
  23.     if (in) {
  24.         // get rid of prvious contents
  25.         hw.clear();
  26.  
  27.         // read homewrok grades
  28.         double x;
  29.                 hw.push_back(x); 
  30.  
  31.         // clear the stream so that the input will work for the next student
  32.         in.clear();
  33.     }
  34.     return in;
  35. }
Sep 11 '07 #1
4 7371
mac11
256 100+
Thats a bunch of code... what exactly is the error you get?
("In file included from grade.h" isn't an error)
Sep 11 '07 #2
sorry mate, it is rather. The error relates to my grade.h header file and I cant figure out what it means. If I compile it stops on the declaration and throws this error.
Sep 12 '07 #3
RRick
463 Expert 256MB
And the error is...............
Sep 13 '07 #4
mac11
256 100+
I snagged your code and tried to compile. The compiler messages are nasty here but if you take the time to really look at them your errors will be revealed...

1. in main.cpp you need a ';' at the end of the following statement:
Expand|Select|Wrap|Line Numbers
  1. cout << students[i].name
  2.             << string(maxlen + 1 - students[i].name.size(), ' ');
2. main.cpp again, change 'steprecision' to 'setprecision' in the following statement:
Expand|Select|Wrap|Line Numbers
  1. cout << setprecision(3) << final_grade
  2.                     << setprecision(prec);
3. medain.h - at the top of the file add
Expand|Select|Wrap|Line Numbers
  1. #ifndef GUARD_median_h
4. Student_info.h - the prototype for compare() has a ';' where there should be a ','
Expand|Select|Wrap|Line Numbers
  1. bool compare(const Student_info&, const Student_info&);
5. median.cpp - the return statement for median() needs a matching ')' like this:
Expand|Select|Wrap|Line Numbers
  1. return size % 2 == 0 ? (vec[mid] + vec[mid -1] / 2) : vec[mid];
Sep 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
35
by: Henry | last post by:
I was doing this program for an exercise in a book. The point was to create a program that would take a numerical grade from a user and convert it to a letter grade (yeah really easy). I tried to...
2
by: dc15 | last post by:
I am trying to write an application, taking input from various text boxes and appending it to a text file.... it was working, then I had to create various sub procedures to reflect limits set in...
0
by: lavicool | last post by:
Hi all, I just joined the post. Iam new to C programming. I have a question regarding a problem for my assignment. The problem is i have a input file with student ID, 5 program scores, midterm and...
1
by: gator6688 | last post by:
I need to be able to save my additional data to my .dat file. The additional data is not part of the vector though. It is data I made after the vector. I have to save the finalGrade and letter grade...
6
by: Jasper | last post by:
Hi, Maybe this is off-topic, but perhaps you can help. I'm looking for ideas on how to parse a data file. I dont know XML but I know it parses data in text format. I have a structured data...
1
by: shalit.dror | last post by:
I have an XML files that contains grades in different subjects from year 2005  2007 . In the HTML file I have several drop down list that contain all the names of the subjects. If I select for...
1
by: payalp | last post by:
(1) Reads from the file “hw4.txt” in the local directory the SID (which is a string) and the scores (which are integers) of each student in a class. Although the number of students and the number of...
2
by: makita18v | last post by:
So my program works all right, but I can't read my information from a file into my vector, what am I doing wrong? vector<char> grades; grades.push_back ('A'); grades.push_back ('B');...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.