read from file problems
im trying to read data from a file
the file appears like
how can i read these in ?
Male 4 4.7 5
0 0 0 0 0
Female 3 4.0 2
1 0 0 0 0
what happens is it reads in the Dog entry fine, but when it gets to the Cat entry, it doesnt seem to be reading in the Cat ID which is 3..
c >> gender;
c >> age;
c >> weight;
c >> total_visits;
while( c >>points[x] )
I believe your problem is here:
You keep reading in the points until nothing is read in, when what you should be doing is reading in only <total_visits> number of points (I'm assuming total_visits is the number of points directly below).
while( c >>points[x] )
So just change your code to match that methodology and see if that fixes it.
that while loop is trying to read in these 5 numbers
1 0 0 0 0
if those 5 numbers are always five numbers then you can do this:
will read in all data until end of file or an error occurs. You only want to read in 5 data points, not the rest of the file, potentially. In your case, when the program has read the five ints, it will attempt to read the three, which it does successfully, but now it tries to put the 3 in points, and depending on the size of points it may overwrite the end of the array, and it may crash the program. If the program doesn't crash there, it will then try to read the C in cat, which isn't an int, and that may well cause the program to crash, if points is an array of ints as opposed to an array of char.
x = 0;
while(x < 5)
cin >> points[x++];
because you know the number of points to read in. As jverkoey said
while(cin >> points[x])
In addition, you have the classic problem that happens when mixing >> and getline() in the same program. It turns out that data doesn't go directly from the keyboard/file to variables. It goes to an intermediate repository affectionately known as the input buffer, where it waits for the input method/operator to retrieve it. The >> operator will leave the terminating char in the input buffer, and ignore it when it goes back next time for another piece of data. getline() removes the terminating char from the input buffer. The default terminating char for getline() is the newline char. In the Dog data, getline() comes before >>. However, after the 5 numbers at the end of Dog are read in by >>, the terminating char for the 5th number is probably a new line char, which will be left in the input buffer. You then go back to the top of a loop, I'm sure, and call getline() to start reading data for Cat. However, the first thing getline() sees is a newline char, so it doesn't put anything in id. It removes the newline char, and then reads in the next line, which is Cat, and everything is okay again, except nothing got put into id.
To fix this problem, it's not a bad idea to always clear the input buffer before any call to getline(). There are a number of ways to do that. They all involve ignore() method. A reasonably effective way is like this:
again the second parameter, which is the terminating char, defaults to newline so you could do this if that's what you want.
There are more sophisticated ways to do the same thing, but this syntax works the vast majority of the time.
i've tried using the cin.ignore
but i get this error
error: `ignore' undeclared (first use this function)
i've included the header file <iostream>
do i need to include any otheres?
Errr... posting the code would help.
Did you type cin.ignore, or just ignore?