Can some one please explain this error? I read about it but did not understand it enough to solve it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
string filename;
cout << "Enter Your Target File.. " << endl;
getline(cin, filename);
string tag = "BODY";
bool stripOtherTags = true;
string text = File(filename);
string all = ExtractBody(text, tag);
for (string& s : all) //a reference of type "std::string" (not const-qualified) cannot be initialized with a value of type "char"
{
if (stripOtherTags)
{
ExcludeTags(s);
cout << s << '\n';
}
}
The problem is that you're trying to process each character in your string "all" into another string. Perhaps you should be using either a char reference or just use auto instead.
The range-based for loop is accessing the individual characters in the string. The individual characters in the string are not std::strings, they are chars.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
int main()
{
std::string str { "A string" };
for (constchar& itr : str)
{
std::cout << itr << ' ';
}
std::cout << '\n';
}
Using auto as the type in the for loop makes things easier, the compiler knows what type is needed:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
int main()
{
std::string str { "A string" };
for (constauto& itr : str)
{
std::cout << itr << ' ';
}
std::cout << '\n';
}
The error is that a foreach loop works on each character of a std::string.
It isn't clear what you are trying to accomplish, and you aren't explaining it.
Is 'all' supposed to be a single string, or a collection of strings? If the latter, then ExtractBody needs to return a vector<string> or another type of container of strings.
@Ganado it was a vector<string> but I am trying not to use vector so I put it as all[] (undefined string array)
The foreach loop is supposed to read every character and see if it contains the specified tags.
You don't seem to have produced a program/class design before starting to code and seem to be 'coding on the hoof' as you go along.
This isn't the way to produce a good, working program. Before you write one word of a program, you should first produce a program design. This is tried with sample data to ensure it's correct. When you're happy with the design, then you code the program from the design. Then you test/debug your program. If there's a coding error from the design, then the code is fixed as needed. If there is a design/algorithm issue then you go back to the design, modify it, then re-code the program from the updated design. This is repeated until you have a working correct program. When producing a program, the last thing you do is actually to write the code!
@seeplus The thing is, I'm trying to simplify it as much as possible for my comfort. As I said before I am not experienced in programming, need a lot of teaching. So this is not an extreme coding project it's fairly simple, but I have to know how to work around the 3rd party libraries to get the same result WITHOUT using them, I'm trying multiple things right now! Appreciate the advise.
Kareem, if your instructor doesn't allow you to use them, then fine, but they are in no way "third-party". They are just as legitimate as string or iostream.
You can't return a C-style array from a function. You can pass an existing one and modify it.
@Ganado I understand, by 3rd party libraries I don't mean vector specifically, I mean Boost and things like that. But thanks for the information anyways :D.
@jib >What type of variable is ExcludeTags() expecting? string.
1 2 3 4 5 6 7 8
for (string& s : all)
{
if (stripOtherTags)
{
ExcludeTags(s);
cout << s << '\n';
}
}
Okay, then do you realize that "s" is defined as a string but you're trying to stuff a single character into this string? Which gives this error in the online compiler:
81:19: error: invalid initialization of reference of type 'std::string& {aka std::basic_string<char>&}' from expression of type 'char'
Which is telling you that you can't initialize a std::string reference with a single character.
The foreach loop is supposed to read every character and see if it contains the specified tags.
Do you know the difference between a string and a character? What information are you trying to hold in your "tags".
Also look at this snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
string File(string fileName)
{
string body;
char ch;
ifstream FILE(fileName);
if (!FILE)
{
cerr << fileName << " not found!" << endl;
exit(1);
}
while (FILE.get(ch))
{
body = body + ch;
FILE.close();
return body;
}
}
Do you realize that your string body will only contain one character in the code provided?
I doubt that this is the desired behavior, so what exactly does your input file contain?
What exactly are you trying to return when reading that input?
@jib I realized that after looking deeper into the code, and I fixed it. Also, it is kind of like an xml parser but simpler, called ".sgm' file has this input:
<REUTERS ... >
<DATE>26-FEB-1987 15:01:01.79</DATE><TOPICS><D>cocoa</D></TOPICS>
<PLACES><D>el-salvador</D><D>usa</D><D>uruguay</D></PLACES><PEOPLE></PEOPLE>
<ORGS></ORGS><EXCHANGES></EXCHANGES><COMPANIES></COMPANIES><UNKNOWN> ... </UNKNOWN><TEXT> ...
<TITLE>BAHIA COCOA REVIEW</TITLE>
<DATELINE> SALVADOR, Feb 26 - </DATELINE>
<BODY>
Showers continued throughout the week in the Bahia cocoa zone, alleviating the drought since
...
...
Brazilian Cocoa Trade Commission after carnival which ends midday on February 27.
Reuter

</BODY></TEXT>
</REUTERS>
It's an article distributed into 21 .sgm files. And all should be read together to extract top 10 frequent words without using the std::vector, it is a school project and I usually do it with a team now I'm doing it alone so I'm doing a lot of things alone. Had a really bad time this year so I apologize if I seem like I don't understand anything.