| jonestsai (6) | |
|
I am trying to read a text file that looks something like this: 0 5 9 0 4 15 1 3 5 2 15 4 11 3 2 15 15 16 end99 The total number of integers before end99 are unknown. But when it gets to end99, the program puts all the individual integers into an array before end99 and then terminates. In the end, the program produces {0,5,9,0,4,15,1,3,5,2,15,4,11,3,2,15,15,16} I don't know if I should use int, char, or getline to read each value before end99. If I use int, I won't be able to know when I get to end99. If I use char, there will be a problem reading the integers. If I use getline, I don't know how to put the individual integers into an array. Any ideas would be greatly appreciated. If you are confused about my question, I would be more than happy to clarify. | |
|
Last edited on
|
|
| LoLFactor (76) | |||
I'm no expert but you could use a vector class and read ints in a loop. Something like:
| |||
|
Last edited on
|
|||
| jonestsai (6) | |
| I can read the numbers from the text using that method, but it doesn't terminate even after reaching end99. | |
|
|
|
| Bazzy (6275) | |||
use in.peek() so that:
peek() reads the next character of a stream leaving it there | |||
|
Last edited on
|
|||
| jonestsai (6) | |||
|
Thanks Bazzy. Your code makes sense. But there's a problem that I can't seem to figure out. For example, I have a text file that looks like this: 2 10 5 3 9 1 end99
The output I get is 2 and then keeps running without terminating. | |||
|
Last edited on
|
|||
| Bazzy (6275) | |||
that could be for space characters, add a condition for them:
| |||
|
Last edited on
|
|||
| modulo51 (7) | |
|
| |
|
Last edited on
|
|