vector of strings

ok, i'm using a vector of strings to store a word list loaded from a file. The program works perfectly but it cuts all the words off to 8 characters. Is this due to some inherent limitation with vectors? Can I remove this limitation?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<vector>
#include<cstring>
#include<fstream>
#include<keepr.h>
using namespace std;
int main(){keepr dontstop;
    vector<string> list;
    list.clear();
    
    string temp;
    ifstream infile("dic.dat");
    while(!(infile.eof())){
                           infile>>temp;temp+=" ";
                           list.push_back(temp);}
    
    for(int t=0;t<list.size();++t){
            cout<<list.at(t);
                 }
                 return 0;
                 }
                 

FYI:keepr is a class a use with the screen pausing function located in the destructor
Doesn't happen when I run it.
Last edited on
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.
Last edited on
Thank you Duoas for all the commentary. As I beginner it's helpful to get feedback on conventions to make my code more readable. I found your loop for reading the file most helpful. I've actually had problems with programs not quite reaching the end of the file before I need them to stop. One more question. You moved #include<keepr.h> to under the using namespace std; line. Is this another convention to show I am using a non-standard header? Thanks again for the help.

BTW: Original question was kind of stupid. I finally realized the word list I downloaded was truncated to 8 letters, don't ask me what kind of use it was supposed to be. Thanks for the help still.
Last edited on
I tend to do that, but it doesn't make any difference.

Sometimes it does, though.
Yeah, I have noticed that sometimes I have to mess with the order of the include statements especially when using pre-compiled headers.
Generally though use of angle brackets in #includes denotes a "system" header and use
of double quotes denotes a "user" header. Any headers you write should be included
using double quotes.

Behind the scenes, the compiler does treat the two differently -- <> causes the compiler to
search the system include paths first, and then the user include paths. This would only
make a difference if you created a header file with the exact same name as a system
header, which you shouldn't.

Topic archived. No new replies allowed.