Controlling input?

J

Jeff Rollin

Hi group,

I'd like to solicit some advice on a problem I've been having with one of
the programs in Chapter 3 of Accelerated C++, viz:

"Write a program that will keep track of grades for several students at
once.
The program could keep two vectors in sync: The first should hold the
student's names, and the second the final grades that can be computed as
input is read. For now, you should assume a fixed number of homework
grades...".

The problems I'm having are as follows:

The "final grades" are computed as the sum of the ratio of midterm grades to
final exams grades to coursework grades; the ratio is irrelevant to this
problem and the book explicitly shows you how to do this. Where I'm having
difficulty is that I can't see how to gather (a) the midterms, (b) the
finals
and (c) the homework grades for (numbers of students > 1) without using a
vector for each of (a), (b) and (c) and then computing the grades from each
of the elements and storing the successive grades in another vector. As you
can see, using vectors for names, midterms, finals, homework and grades
brings us to not two vectors, as the book suggests, but five.

Some notes:

1) The attached source code has the problem that you end a line of input (in
the first case, a list of names such as:

Jeff Geoff

with either two EOF's or an LF followed by an EOF. This is as suggested thus
far through the book. However, once you've done that the program
automatically thinks you have passed input to the next variable (cin >>
midterms). The book has not yet tackled the problem of how to clear the
input
stream; I'm guessing that this is a failure of understanding on my part
since
the (rather detailed) list of Errata on the Website of the Book doesn't even
mention it.

2) I'm aware that some of the algorithms (e.g. for error checking) are not
optimal - I'm trying to resolve these problems using the tools as so far
given, w/o looking for solutions in later parts of the book.

Is it possible that the advice to "read the book over once" first and then
go
back and do the exercises is because the exercises from earlier chapters use
techniques defined in later ones?

TIA

Jeff
 
D

David Harmon

On Thu, 07 Jun 2007 16:28:49 +0100 in comp.lang.c++, Jeff Rollin
of the elements and storing the successive grades in another vector. As you
can see, using vectors for names, midterms, finals, homework and grades
brings us to not two vectors, as the book suggests, but five.

The example early section 3.1 computes a grade for a single student
without storing anything in a vector. If you do that for each student,
you only need to store the name and the grade. I think that's what they
are asking for in that exercise.
The book has not yet tackled the problem of how to clear the
input stream;

Which is why that particular exercise says to assume a fixed number of
homework grades. If the input never deviates from expectations, you
won't need to clear.
2) I'm aware that some of the algorithms (e.g. for error checking) are not
optimal -

More like nonexistent, at that point.

Handling input flexibly is often tricky. I don't like the multiple EOF
method, because it generally won't work with reading input from a file.

One method I do like is to read the input a line at a time, using
std:string and getline(), then create an std::istringstream from it and
read from that. In case of any problem, it's easy to skip that whole
line and go on to the next. You might have the student name on one line
and all of his grades on the next line, repeated until the end.
Is it possible that the advice to "read the book over once" first and then
go
back and do the exercises is because the exercises from earlier chapters use
techniques defined in later ones?

That, and it's pretty helpful to have some idea where you're going.
Rarely can an author cover a subject as complex as C++ without any
backtracking.
 
J

Jeff Rollin

Thanks for that; I had feared I was being too clever for me own good. With
good reason, it seems.

Jeff
 
W

waltbrad

I can't see how to gather (a) the midterms, (b) the
finals
and (c) the homework grades for (numbers of students > 1) without using a
vector for each of (a), (b) and (c) and then computing the grades from each
of the elements and storing the successive grades in another vector. As you
can see, using vectors for names, midterms, finals, homework and grades
brings us to not two vectors, as the book suggests, but five.

I gave up on that book. The presentation of the material was a little
too disorderly for me. I will probably pick it up again when I'm
through with Prata's book.

However I did get as far as you have. If I remember correctly all the
grades should be stored in one vector. First the midterm, then the
final, followed by the homework. You simply remember the order that
they were stored into the vector.
 
J

Jeff Rollin

I gave up on that book. The presentation of the material was a little
too disorderly for me. I will probably pick it up again when I'm
through with Prata's book.

Oh well. What's the title of Prata's book, if I may ask?
However I did get as far as you have. If I remember correctly all the
grades should be stored in one vector. First the midterm, then the
final, followed by the homework. You simply remember the order that
they were stored into the vector.

My own solution was to follow David Harmon's suggestions in this thread.
Considering that the book has not yet even discussed arrays, putting the
variables in one by one made for some pretty hairy (in the sense of
extremely amateurish, without such basic things as loops) code.

However, it's certainly not the only book I've seen on programming where the
successful (as in, without using such amateurish code) completion of some
exercises hangs on knowledge of concepts only discussed later in the book.
That sort of thing seems to be particularly common in Lisp books - I'm
currently debating whether to shell out for Paul Graham's ANSI Common Lisp
in the hope that it is more orderly than some of the ones you can get on
the web for free.

Thanks for the info.

Jeff.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top