![]() |
|
#1
|
|||
|
|||
Input stream questionI'm trying to understand a series of programs in Accellerated C++ which build a grading program in steps. The authors began, in Chapter 3, with a program that includes all the user input and output in main, using a vector<double> to hold homework grades. In chapter 4, the author separates functions out into separate source files, and arranges student records in structs. In chapter 5, he introduces iterators and lists.
The problems I'm having are in the revised function to read homework. First, the author removed any kind of prompt in the revision, so that when the program compiles, there's no indication of what to do.... just a cursor. That I fixed. The remaining problem is what to do when one wants to indicate that one is through entering homework. main calls function read: CPP / C++ / C Code:
And function read is included in the Student_info source file: CPP / C++ / C Code:
The problem, in a nutshell, is that there's no way to gracefully exit the read_hw function in the current version. On my mac, using XCode 2.4.1, entering end-of-file (CTL-D) does nothing. I've also tried entering CTL-Q followed by CTL-D. The only way out of the loop is to enter a non-numeric value which gives an error message: Quote:
I've tried altering the read_hw function to use a flag '-1' to signal end of homework, but that doesn't get me out of the main loop: Quote:
I'm at a loss as to first, signal the end of entering homework, and second, signal the last student entered. Any help would be greatly appreciated. I've not yet been able to get the code to compile on Dev-C++ on my PC. Here are the complete source files if anyone is interested: main.cc CPP / C++ / C Code:
Student_info.h: CPP / C++ / C Code:
Student_info.cc: CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
Last edited by LuciWiz : 25-Sep-2007 at 12:12.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: Input stream questionQuote:
Anyhow... Quote:
For example: CPP / C++ / C Code:
Note my comment: the in.clear() resets any error flags, so that the results of an invalid input (or end-of-file or other stream error) is not fed back to the calling program. Therefore, no matter what happens with the in stream in this function, it always returns whatever state the stream had when the function was called. Regards, Dave |
|
#3
|
|||
|
|||
Re: Input stream questionThanks, Dave.
I assume that the best solution would be to set an end-of-file flag on the input stream if there is no more homework to be read. Unfortunately, I'm still totally at a loss as how to make this happen on my Mac, using Xcode 2.4.1, with this code. I've tried entering CTRL-D, CTRL-Q followed by CTRL-D, and even CTRL-Z for the hell of it.... nothing makes it stop reading. If I say to hell with it and decide to ask the user to enter some nonsense value, like -1, to signal the end of entering homework, would this be a reasonable way to do it? CPP / C++ / C Code:
Last edited by admin : 26-Sep-2007 at 09:26.
Reason: Please insert your C code between [cpp] & [/cpp] tags
|
|
#4
|
|||
|
|||
Re: Input stream questionQuote:
However... Quote:
The statement in.eof(); does not set the eof flag on the input stream. And what if it did? How would that help anything back up the chain of calling functions? In other words, the issue now is in the loop in your main() program that calls the read() function. How will you let that piece of program know that there are no more students to be processed? Regards, Dave |
|
#5
|
|||
|
|||
Re: Input stream questionOh, I see, in.eof() just returns a bool indicating whether the stream has hit end-of-file... so is there no istream member function that lets you actually force the eof flag to be set? I looked at http://www.cppreference.com/io_flags.html#manipulators and didn't see any there.
And yes, I understand that the calling function is going to have to deal with the return from the read function.... but I'm trying to deal with the most pressing problem for me right now, which is, again, how the hell do I cause the eof flag to be set? |
|
#6
|
|||
|
|||
Re: Input stream questionQuote:
I hate to repeat myself, but what if you could set it when leaving your lowest-level read function? It would be set for each and every student for which you had called the function (since each time you call the read_hw() function, it reads homework values until you tell it to quit). How would setting the eof flag every time let any upstream calling function know which one was the last student that you intended to process? Regards, Dave |
|
#7
|
|||
|
|||
Re: Input stream questionQuote:
__________________
Music, programming, endless learning.. |
|
#8
|
|||
|
|||
Re: Input stream questionOK, got it - so eof should only be true if the istream actually reads end-of-file, either from a file or from input (which I still don't know how to make happen.... sigh). That would definitively signal that there is no more homework to be read.
So I should modify the read_hw() and read() functions to also return a bool that indicates whether the current student is finished inputting grades, and also modify either the read() function or the calling function to ask whether there are more students to be read. |
|
#9
|
|||
|
|||
Re: Input stream questionQuote:
You are, of course, correct. A user can set the "eof flag" on an istream object by CPP / C++ / C Code:
Thanks for the tweak. Regards, Dave |
|
#10
|
|||
|
|||
Re: Input stream questionOK, so if I can set the eofbit to true, all I should then need to do in my read() function is something like:
CPP / C++ / C Code:
Last edited by earachefl : 26-Sep-2007 at 10:30.
Reason: wrapping code with [CPP]
|
Recent GIDBlog
Master?s Degree by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| basic input question | prince87x | C Programming Language | 10 | 01-Oct-2006 16:21 |
| scanf / clearing input stream | zone4 | C Programming Language | 8 | 08-May-2004 14:42 |
| IP tables | rogermark100 | C Programming Language | 6 | 18-Apr-2004 07:22 |
| a C input question.. | tmike | C Programming Language | 2 | 19-Sep-2003 02:39 |
| a C input question | tmike | C Programming Language | 1 | 16-Sep-2003 02:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The