473,396 Members | 2,129 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,396 software developers and data experts.

An Example from Accelerated C++ with question

There is a student grade example in the book Accelerated C++ chapter
4.
Related code is:

main.cc:
// 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);
}

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;
}

I make a file with two lines:
John 90 80 60 78 95
Smith 93 76 40 80 60

And feed it to the program.
the istream& read(istream& is, Student_info& s)
function will read all the name and grade and put them in a
Student_info structure.

My question is how does the program know I have read all the data from
the 2 lines file.
the
istream& read_hw(istream& in, vector<double>& hw)
function will do in.clear();
so even cin encounter EOF, the eofbit will be cleared.

I think the main function will try to read the non exist third line
and fail.
But actually the program works! How does the code know it has read all
the data??
Nov 18 '07 #1
1 1799
Lambda <st*********@gmail.comwrote:
My question is how does the program know I have read all the data
from the 2 lines file. the istream& read_hw(istream& in,
vector<double>& hw) function will do in.clear(); so even cin
encounter EOF, the eofbit will be cleared.
Not quite...
I think the main function will try to read the non exist third line
and fail. But actually the program works! How does the code know it
has read all the data??
Step through the program by hand, assume that the file is empty. What
will happen?

1: while (read(cin, record)) {
2: (inside read) is >s.name >s.midterm >s.final;
At this point the eof is encounterd.
3: read_hw(is, s.homework);
4: (inside read_hw) if (in) {
'in' returns 'false' in this case so the entire block is passed up.
I.E., in.clear() isn't called.
5: return in;
6: (back inside read) return is;
7: back to (1) the while loop is exited.

Hope the above helps you understand the code, and gives you a tool to
understand other code in the future.

When I started programing (back in the stone ages. :-) I didn't have
access to a mainframe so I had to step through my code by hand to see if
it worked. I still use the method today.
Nov 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Magic1812 | last post by:
Magic Software invites you to join us this coming Tuesday (January 13th, 2004) at 12:00 EDT / 17:00 GMT for a FREE live Webinar: Title: Accelerated Application Integration and Information...
3
by: Joe Laughlin | last post by:
On 6.2.3/115 of "Accelerated C++", there's this function: double average(const vector<double>& v) { return accumulate(v.begin(), v.end(), 0.0) / v.size(); } The author notes that it's...
39
by: TonyJeffs | last post by:
Great book - I like the way that unlike other books, AC++ explains as much as possible about every piece of code discussed, so I'm not left thinking, "well...OK... I get line 12, but I wonder what...
3
by: Frankie Montenegro | last post by:
Hi everyone, I must say that, even though I think that Accelerated C++ by Koenig and Moo is an awesome text, the wording of exercises is very poor. I spend half the time just trying to figure...
9
by: arnuld | last post by:
hai folks, well, it's me again and again, i am asking questions. anyway, with your help i have finalised that i will start learning c++ (as you told me that i do not need to know any OO language...
3
by: xllx.relient.xllx | last post by:
I have a question about an example presented in the book "INFORMIT C++ Reference Guide" by Danny Kalev: <code> string getmagicword() ( return string("Supercalifragilisticexpialidocious"); );
10
by: Xernoth | last post by:
Hi, This is my first post here, so please be gentle. I've been studying c+ + by mostly using the book Accelerated C++ by Andrew Koenig and Barbara E. Moo. So far, I've been picking things up...
5
by: Madmartigan | last post by:
Gidday Question : Write a program that will report the longest and shortest string in its input. I am attempting EX 3.4(page 49) from Accelerated C++ by Koenig and Moo. I keep getting the...
8
by: utab | last post by:
Dear all, In a question in the highly recommended book Accelerated C++, it is asked to change a const function into a plain function. After on the reader is expected to find which function(s)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.