Some additional commentary:
You don't need line 3.
Line 7: Please do only one thing per line.
Line 9: The
list default constructs to an empty state. You don't need to
clear() it first.
Line 11: I would have named my variable '
word', as this better explains its purpose.
Line 13: Don't test on
eof() in loops. Test against
good(). Should anything go wrong while reading that file, you will get an infinite loop with the former (EOF will never become true) but proper termination with the latter (even if the entire file is not read).
Line 14: Please do only one thing per line.
Line 15: This is a variation of the last. That closing brace should be on a line by itself, not hidden there at the end of the last command.
Line 17: You should be using an
unsigned variable here.. just to keep compilers happy. (The
size() function returns an unsigned value.) Also, use standard variable names -- it helps other programmers follow what you are doing. An
x or
n or
cntr or
i or the like is better understood than the random
t (which is more commonly used for 'time' and the like).
Lines 19-21: Please watch your indentation.
Here it is again with a little cleanup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
#include <keepr.h>
int main() {
keepr dontstop;
vector<string> list;
string word;
ifstream wordfile("dic.dat");
while (infile >> word) {
list.push_back(word);
}
for (unsigned n=0; n<list.size(); ++n) {
cout << list.at( n ) << " ";
}
return 0;
}
|
I removed the addition of the space after the word -- since the space is not part of a word. See line 18 for where the space went.
If you wanted to make sure that the file was read in its entirety, you can add a check after line 15:
1 2 3 4
|
if (!wordfile.eof()) {
cerr << "Hey there! Something went wrong when reading the list of words!\n";
return 1;
}
|
Hope this helps.