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

Controlling input?

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
Jun 7 '07 #1
4 1481
On Thu, 07 Jun 2007 16:28:49 +0100 in comp.lang.c++, Jeff Rollin
<je************@gmail.comwrote,
>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.

Jun 7 '07 #2
Thanks for that; I had feared I was being too clever for me own good. With
good reason, it seems.

Jeff

Jun 7 '07 #3
On Jun 7, 11:28 am, Jeff Rollin <jeffrey.rol...@gmail.comwrote:
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.
Jun 7 '07 #4
In the last episode, on Thursday 21 Sivan 5767 20:03, waltbrad wrote:
On Jun 7, 11:28 am, Jeff Rollin <jeffrey.rol...@gmail.comwrote:
>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.
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.
Jun 7 '07 #5

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

Similar topics

0
by: Gregory Lielens | last post by:
Hello all, I am not sure this is the correct list to ask as my problem is really related to python low level implementation, but as I do not have found a python.dev list, here it is :-) We...
0
by: Lorenzo Bolognini | last post by:
Hi all, I have a problem letting this small script I wrote work correctly. In particular i can't manage to execute commands that require user input/confirmation. How should i handle that? If...
22
by: Mark Reginald James | last post by:
Hi, I'm trying to use a keyboard event function to change what element gets the focus after tab is pressed in a particular element. Simple HTML that demonstrates the problem is below. When...
1
by: Mario Figueiredo | last post by:
Hello everyone, I'm having trouble controlling the cursor position when I make two consecutive calls to the get family of functions. This problem does not happen if there is an output in...
3
by: Herb | last post by:
I've found how to use javascript to embed a Windows Media Player in a web page. How do I go about controlling the player in response to user input? There should be calls to start, stop and also...
6
by: Eric_Dexter | last post by:
I am having trouble contolling vim with subprocess on a windows machine. It appears that vim comes up on the machine all right and it sometimes looks like it is doing the searchs what I am asking...
1
by: bbcline | last post by:
Hi all, I'm a newbie to the forum and a Javascript beginner, I'd appreciate some help with a drop down box issue. Is it possble to limit the options/attributes in a drop down box based on how the...
1
by: Brit | last post by:
I have an ASP file that retrieves names from an Access database for 4 different categories of membership, which the visitor to the page selects (corporate, institutional, regular, or student). The...
1
by: Cainnech | last post by:
Hello all, I have a big challenge ahead of me but I'm gonna need a lot of help from experts on this one. I have to create a system that can receive input from external devices. So when users...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.