#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using std::string ;
using std::vector ;
using std::istream ;
using std::cin ;
using std::cout ;
using std::endl ;
struct Student_info
{
string name ;
double midterm ;
double final ;
vector<double> homework;
};
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;
}
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;
}
void print(Student_info& s)
{
cout << "The student's name is :" << s.name << endl;
cout << "The student's midterm grade is :" << s.midterm << endl;
cout << "The student's final grade is :" << s.final << endl;
double sum = 0 ;
for(vector<double>::size_type i =0 ; i < s.homework.size() ;i++)
sum += s.homework[i] ;
cout << "The student's average homework grade is :" << sum /s.homework.size() << endl;
}
int main()
{
Student_info S;
read(cin, S);
print(S);
}
How to use :
1.Input a string for student's name , then a double for midterm grade , then another double for final grade , then input arbitrary number of homework grades .
2.Press Ctrl + Z to finish inputting .
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using std::string ;
using std::vector ;
using std::istream ;
using std::cin ;
using std::cout ;
using std::endl ;
struct Student_info
{
string name ;
double midterm ;
double final ;
vector<double> homework;
};
void read_hw(vector<double>& hw) //istream& removed !!!
{
if (cin)
{
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (cin >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
cin.clear();
}
//istream& removed !!!,no return !
}
void read( Student_info& s) //istream& removed !!!
{
// read and store the student's name and midterm and final exam grades
cin >> s.name >> s.midterm >> s.final;
read_hw( s.homework); // read and store all the student's homework grades
}
void print(Student_info& s)
{
cout << "The student's name is :" << s.name << endl;
cout << "The student's midterm grade is :" << s.midterm << endl;
cout << "The student's final grade is :" << s.final << endl;
double sum = 0 ;
for(vector<double>::size_type i =0 ; i < s.homework.size() ;i++)
sum += s.homework[i] ;
cout << "The student's average homework grade is :" << sum /s.homework.size() << endl;
}
int main()
{
Student_info S;
read (S);
print(S);
}
//Both programs work exactly the same to user .
Here are my questions:
1.Why bother introducing istream structure to the read() and read_hw() functions ?
2. If there is a need to use istream , why bother returning the istream object in each function ?
If there is a need to use istream , why bother returning the istream object in each function ?
It's so that you can check if the function was successful or not.
1 2 3 4 5 6 7 8 9
Student_info studentInfo;
if (read(studentInfo))
{
// The student info was read successfully.
}
else
{
// Something when wrong while reading the student info.
}