Accelerated C++ istream problem

//This is from <<Accelerated C++ >> Chapter 4 or 5
//This is a program reading and calculating Student's grade

//Here is the original code reading Student's info .which contains istream (I think it is a structure !)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 #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 .

Last edited on
//Here is the version istream removed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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 ?
The istream class is used so that you can use any input stream, not just the cin stream.
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.
}
@Peter87
Thanks ,That's the answer I want ,It makes sense
Topic archived. No new replies allowed.