Hello enrikm84,
As I work through the program I find the instructions a bit confusing. They say
neither can I use multiple string variables in the main body |
yet you have a function that requires two "std::strings", so you need to define two variables in main. I would probably use the firt two and delete the rest.
If you think about it there is no restrictions on how many "std::string" variables you can define in the functions.
Once you have created and opened the "ifstream" you need to check that the file did open and that the stream is good. Something like:
1 2 3 4 5 6
|
if (!inFile) // <--- Sorry this way of writing the variable is just habit for me.
{
std::cout << "\n File \'Data\" did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
exit(1); // <--- Because there is no reason to continue any farther.
}
|
The comment on the while statement should read "// Reading data file" not opening this was done earlier.
The line
process(line, combine);
is a conundrum because you are passing "combine" by reference yet the function returns "combine" that the function call never uses, but any change the function makes to "combine" is reflected back in main, so there is no need for the return variable.Although passing a "std::string" by reference is generally less overhead for the computer than making a copy first when passed by value.
It looks to me that the "process" function should change any letters to lower case and remove any punctuation and spaces leaving you with a string of just letters.
In the "process function" the line that defines could be written as
int c = word.length();
, but this is not even needed because in your for loop it should be
for (std::size_t i = 0; i < word.length(); i++)
. Keep in mind that "word.length()" returns an "unsigned int", so "i" should be defined the same. "std::size_t" is just another name for "unsigned int".
In thee for loop the first line is OK and the if statement I believe is OK. The line following the if statement can be shortened to
combine += word[i]
. It does the same thing and I am not saying that you have to change it, but it would make your code look better and might make you as a programmer look better. Just saying.
Lastly you are returning "combine" back to main this is what the function needs to do except for the conundrum of the fact that "combine" being passed by reference the changes to this variable have already been made in main, so why return something that has already been changed.
The last two functions as with the whole program I have not had a chance to actually test yet. That is my next task.
I thought you might want to know this first.
Hope that helps,
Andy