Hi Vince
Your code has to be tuned, try these two things:
- each line of the file "test.dat" consists of 7 integers, I suppose you
use the first integer of the row as a marker and the other 6 as data;
instead of reading "pclass" + 5 integers try "pclass" + 6 integers.
- the line that prints the output should have "count" somewhere, probably
you typed "cout" instead of "count".
Note: you are learning C++ from a book that is a bit old ( I can tell that
from the fact that you say "#include <iostream.h>", instead of "#include
<iostream>"): I suggest you find one that tells about ANSI C++. I do not
know if there is one available on line, probably if you take a look at the
link page of the site: http://www.noicys.d2g.com you can find some sites
that among other books have one on ANSI C++.
At 10:58 10/01/02 -0500, you wrote:
>Preamble
>
>Hello I'm learning C++ and following examples in a text I found a good
>book on the web. This is a Chapter 9 exercise (of 30 + chapters) and up to
>now I have had no problems I could not solve.
>
>I'm not yet familiar with the debugger (perhaps I should be working on that).
>
>Problem:
>
>I'm trying to read in a file of data using the following code and data
>file (below):
>
>#include <iostream.h>
>#include <stdlib.h>
>#include <fstream.h>
>
>int main()
>{
> ifstream infile("test.dat", ios::in | ios::nocreate);
> int count =0;
>
> if (!infile.good()) {
> cout << "Sorry ! Couldn't open the file." << endl;
> exit(1);
> }
>
> int pclass;
>
> infile >> pclass;
>
> while(pclass != -1) {
> count++;
> int a1, a2, a3, a4, a5;
> infile >> a1 >> a2 >> a3 >> a4 >> a5;
> infile >> pclass;
> }
>
> cout << "I think there were " << cout
> << "records in that file." <<endl;
>
> return 0;
>}
>
>test.dat:
>
>1 0 0 0 0 0 0
>1 1 0 0 0 0 0
>1 0 1 0 0 0 0
>1 0 0 1 0 0 0
>1 0 0 0 1 0 0
>1 0 0 0 0 1 0
>1 0 0 0 0 0 1
>1 1 0 1 0 0 0
>1 0 1 1 0 0 1
>1 0 1 1 1 0 0
>1 0 1 1 1 1 0
>1 0 1 0 1 1 0
>0 0 0 0 0 0 0
>0 1 0 0 0 0 0
>0 0 1 0 0 0 0
>0 0 0 1 0 0 0
>0 0 0 0 1 0 0
>0 0 0 0 0 1 0
>0 0 0 0 0 0 1
>0 1 0 1 0 0 0
>-1
>
>The code compiles and link with no errors but does not give me any output
>and does not allow any input; simply a console window with a blinking curser.
>I have the data file in the same directory as the executable.
>
>Oh yes, I'm working under WinNT.
>
>Any assistance/advice towards a solution is greatly appreciated.
>
>Thank you,
>
>Vince.
>
>
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--
gisan
gi...@li...
|