I have a file:
P 0.5 0.6 0.3
30 300
80 150
160 400
200 150
250 300
T
r 45 0 0
s 0.5 1.5 0 0
t 200 –150
.
.
.
When I read in 'P' I know that 3 floats will follow. That will be followed by a finite number of X and Y coordinates. The number will vary until a 'T' is reached which I have to recognize. Then there could be an 'r', 's' or 't' followed by some values.
Anyways I know how to recognize 'P' and then take in the 2 floats but then I know I have to have a while loop for the X and Y coordinates which will stop when I get to a 'T'. I do not know enough about C++ to make the loop stop and recognize the 'T' and then do something else.
An example to explain would be appreciated. Thanks in advance!
user to read text file line by line and then run string word;
I think you can use standard streams
to check "P" and "T"
use get(char &ch);
and putback(ch) to push it back to stream
and
yourstream >> x >> y >> endl;
http://www.cplusplus.com/reference/iostream/istream/putback/
I'll show you what I think it's the proper C++ way of doing this. First define a class for representing your first line and for doing its IO:
I've added some basic error checking with assert, you'll probably want something more robust in your final program.
Now a class for middle lines:
When we reach the end of the section where the middle lines are we are supposed to encounter a "T". In that case we raise the fail bit of the stream, which will tell client that there are no more middle lines to read.
Finally a class for the last lines:
Last lines are more complicated becase we don't know how many values are in each, so we just read as many as we can.
That was the tricky part. Now our main function will simply read one first line, then an unknown number of middle lines, and finally an unknown number of last lines:
This is the output you'll get::
I've used a string as the source of my data but you'll probably want to read from a file.
And that's all, you can see that I didn't write a single loop.
Here's the code in codepad.