Hi! I'm experiencing some problems in C++...
1. STL
I'm using a map template like:-
map<string, string>table;
to implement a sort of a word->meaning table. And I intend to
- read a string
- recognize the words in the string by comparing the table keys with the strings
- print the meaning of the word.
And here lies the problem:-
The comparison and printing of the definitions is correct, but it's not "reading" from the left of the string to the right. It reads according to the "index" or the way it stores data elements inside it. Debugging the program showed it stores and reads lexically, meaning that if I store "the" first and "as" second, and the string is "the as", it will display as:-
when the output should be
How can i make it so that it "reads from the left to the right" and not from the way it stores data inside itself...?
I've browsed over different sites and I've tried numerous ways to solve this problem, but to no avail... I've tried using an unordered_map and then sorting it using a list, but the result is same as a normal map...
I've even tried using char* instead of strings like:-
map<char*, char*>table;
and still to no avail.
I've also tried using the relational operators, like:-
1 2 3
|
map<char*,char*,std::less<char*>table;
OR
map<string,string,std::greater<string>table;
|
but still no proper output...
Help me on this, please!
2. FILE I/O
My code is something like this (just for a general idea):-
1 2 3 4 5 6 7 8 9 10 11 12 13
|
ifstream file_in[filename];
string str_line;
while(file_in.good())
{
getline(file_in,str_line);
cout << str_line << endl;
}
while(file_in.good())
{
getline(file_in,str_line);
cout << str_line << endl;
}
|
The main idea is to read the same file twice.
The output is blank!
My compiler doesn't even the reach the body of the first loop, it just stops there and shows a blank output!
I've even tried using another ifstream object, like:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
ifstream file_in[filename];
ifstream temp_file_in[filename];
string str_line;
while(temp_file_in.good())
{
getline(temp_file_in,str_line);
cout << str_line << endl;
}
while(file_in.good())
{
getline(file_in,str_line);
cout << str_line << endl;
}
|
But the output is still the same: blank!
The code only works when I remove/comment out the first loop.
What's happening here and what's the solution?
Thanks (in advance).... :)