From 2c9056a23e1a55fd21a8e314c903d9325bffd62e Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Sat, 6 Sep 2025 09:37:14 +0200 Subject: Move CPP code here Signed-off-by: Carlos Maiolino --- CPP/cpp_book/chap3/student_info.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 CPP/cpp_book/chap3/student_info.cpp (limited to 'CPP/cpp_book/chap3/student_info.cpp') diff --git a/CPP/cpp_book/chap3/student_info.cpp b/CPP/cpp_book/chap3/student_info.cpp new file mode 100644 index 0000000..a55338b --- /dev/null +++ b/CPP/cpp_book/chap3/student_info.cpp @@ -0,0 +1,34 @@ +#include "student_info.h" + +bool compare_students(const StudentInfo& a, const StudentInfo& b) +{ + return a.name < b.name; +} + +std::istream& read_homework(std::istream& in, std::vector& hw) +{ + if (in) { + hw.clear(); // Empty the vector. + + double x; + /* + * Loop invariant: + * - 'homework' contains all the homework grades read so far + */ + while (in >> x) + hw.push_back(x); + + in.clear(); // Clear the stream in case + // it will be used again. + } + return in; +} + +std::istream& read_student(std::istream& is, StudentInfo& s) +{ + /* Read and store student's name, midterm and final */ + is >> s.name >> s.midterm >> s.final; + + read_homework(is, s.homework); + return is; +} -- cgit v1.2.3