something wrong with getline(), taking the words in correct but still the value of size remains 26.
I tried printing each time it takes in a character and all of them do print so itis taking in strings correctly, but not storing them?
I have attached the code below to refer
Ask me for the whole project if you need to refer what is going wrong if someplace else.
void TldPart::PreloadTLDs() { ifstream in(TLD_TEST_FILE); if(in) { string tld; for(int i =0; !in.eof(); i++) { getline(in,tld); String myString = tld.c_str(); //cout << myString.GetLength() << endl; for(int j=0; j<myString.GetLength();j++) { myString[j]=tolower(myString[j]); } //cout << myString << endl; ValidTLDs.insert(pair<String,int>(myString,i)); //ValidTLDs[myString] = true; //if the map was bool } in.close(); cout << ValidTLDs.size(); //Printing the size //prints 26 } }
Asked
Active
Viewed 230 times
0

Nicol Bolas
- 449,505
- 63
- 781
- 982

Chinmay
- 13
- 1
- 3
-
What type is `ValidTLDs`? – paxdiablo Mar 16 '13 at 21:00
-
It is the map in which I'm storing the String – Chinmay Mar 16 '13 at 21:05
-
Why are you using a map if you're _forcing_ each item to be unique with `i`? How are you going to look up these items in the map given that the pair has an arbitrary `i`? – paxdiablo Mar 16 '13 at 21:07
-
Never mind, scratch that last comment. – paxdiablo Mar 16 '13 at 21:09
-
I'm assuming the strings are unique, yes? – paxdiablo Mar 16 '13 at 21:16
-
Actually, why are you using two different string types? – paxdiablo Mar 16 '13 at 21:29
-
1Is it possible the strings you are inserting to the map are single character strings between a and z? That would explain why you get 26 unique keys. – Eran Mar 16 '13 at 22:12
-
2This has nothing to do with your problem but `in.eof()` doesn't do what you think it does. It doesn't tell you if you've reached the eof. It tells you that you tried to read past the eof, which means that you wind up processing the last line twice. `getline()` is convertible to `bool` and will be false when you reach the eof. Use `for (int j = 0; getline(in, tld); ++j)` instead. – Ferruccio Mar 17 '13 at 11:34
-
@paxdiablo I'm trying to use my own string class while just using the predefined class for getline() – Chinmay Mar 26 '13 at 14:53
-
@Eran That actually might be the problem, but when I'm just printing the strings after getline, it prints everything perfectly and I checked while debugging that it was storing all characters. Not just unique between a to z – Chinmay Mar 26 '13 at 14:54