Ok, I have a small problem. I'm just trying to get my program to read a single line from a file at a time, and put the results in a vector.
The problem is at the end of each vector. It repeats the last element twice and also adds a 0. I'm not sure whats going on. I don't think the 0 is a newline, because my strstream never receives a '\n'. I also don't know why it repeats the last element. I posted my program plus my input and output at the end. Any help or ideas would be appreciated Thank you.
- Daniel
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <vector>
void main() {
// To determine if the the list is smaller or equal to 8, calculate how
// many times youd have to cut the list to reach less than/equal to 8 BEFORE
// you go into the mergesort loop.
ifstream in("in.dat");
vector<float>numbers;
float myfloat;
char ch[256];
while (in) {
in.getline(ch, 256, '\n');
numbers.clear();
strstream myss;
myss << ch;
while(myss) {
myss >> myfloat;
numbers.push_back(myfloat);
}
for (int a = 0; a <= numbers.size(); a++)
cout << numbers[a] << ", ";
cout << endl;
}
}
My in.dat file looks like this:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
My output looks like this:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 0,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 20, 0,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 30, 0,
30, 22,
30, 22,