Not Understanding all Conventions

Hello Forum,

I’m very much struggling to understand all of the tools we’re using and how they interact at this stage. I was wondering if someone out there could kindly hold my hand through the following chunk of code and explain to me in simple terms what it’s doing and how it’s doing it.

I don’t understand what a “handle” does exactly (ie “(FString Word)”). Is it just a way for another function to retrieve information from this function? If that’s the case then isn’t that what the return type, “bool” and “return true” line is for? Wouldn’t one just use the name of function to retrieve this information rather than a handle?

“TMap<char, bool> LetterSeen;” is essentially creating a boolean named “LetterSeen”, yes? Because it comes from a map, this boolean will change which is why we’ll be looping through it?

I still don’t really understand what “auto” does or what its purpose is. How high of a level is “char”? Is it a type of variable similar in broadness to a bool or a void? Where else might one use “auto”?

I don’t understand how the line “for (auto Letter : Word)” translates to “For all letters of the word” as Ben says. I think I really need this explained to me.

Next, where does “tolower” come from? We didn’t define it anywhere did we? So is it some sort of method that’s defined in something we’ve imported? Is it similar to how we sometimes use “.length” to measure strings? What does “letter” in parentheses mean in this line? Sometimes I don’t grasp what it means when you have a word in parentheses like back in “Main.cpp” where early on it says “while(bPlayAgain);” I find it difficult to translate that into English.

The square brackets in this example are getting a value called “Letter” from the map we’ve created, right? What’s it doing with that value?

The more someone could break this down for me, the more I think I’ll understand it. A couple lessons ago I felt like I had a strong grasp on all of the concepts, but now I feel lost.

Thank you so much for your advice and patience!

The (FString Word) is a parameter that is passed into this function from the calling function. In this case it is going to be the player’s guess. The toLower function is defined in the string.h header file, I believe, and is part of most C++ compilers.

I haven’t gotten to this part yet, but I will try to remember from my past experience. It has been a while since I used C++, but here goes. The map is all the letters that have been looked at during each iteration of the for loop. The loop will look at each letter in the word passed to it, compare it to what has already been placed in the map, and when it finds a new letter it adds it to your map. The first time it comes to a letter that is already in the map it exits the function returning false. If it gets through the word without finding a match it will return true.

Thank you for your answers, George. That’s good to know about “toLower”. I was confused as to where it was coming from.

I still have a lot of unanswered questions as I brought up in my post above. I hope someone can chime in one these. In the meantime I’ll keep going through the course material trying to figure it out.

As George said, that’s a parameter. It allows you to pass things into the function. For example

int square(int i)
{
    return i*i;
}
int main()
{
    int a = square(4); //a == 16
}

4 gets passed into the function, get’s multiplied by itself and the result returned to the caller.

No, this is creating a std::map (through the #define TMap std::map) which is an associative container, it maps unique keys to values and is sorted. So when you declare it using the angle brackets <> you are supplying the types of the keys and values respectively. For example std::map<char,int> would create a std::map with char’s as the key and int as the value.

Next is the [] operator for std::map. When you access a std::map via [] it will look for a matching key and return the associated value, however if no key is found it will insert it and value-initialise the paired value. For example

std::map<char,int> charCounter;
int count = charCounter['a'];
charCounter['b'] = 2;

charCounter is initially empty, however the second line tries to find ‘a’ and can’t find it so it inserts it into the map, value-initialises the paired value and returns that value. So count should be 0, and my charCounter will have 1 element which is {'a', 0 }. On the second line, I insert ‘b’ as a key and set it’s associated value to 2. So now the charCounter map has two elements {'a', 0 } and { 'b', 2 } and if I were to access the map with ‘a’ or ‘b’ I would get their paired value returned back.

Not quite sure what you mean by “how high leve is ‘char’” however auto just deduces the type from it’s initialiser.

auto i = 3; //int
auto d = 3.14 //double
auto c = 'c' //char

That is a range-based for loop. Where the left side is declaring a variable to use in the loop, and the right side is the range you want to loop over, in this case Word is being the range we want to loop over and Letter is going to be the char element within that range. (std::string being made up of char’s)

tolower is a function defined in <cctype> (which I assume <string> #includes). It’s a function that take’s a char and gives us the lowercase version char lowercase = tolower('A'); //lowercase = 'a' so the line Letter = tolower(Letter) just makes Letter lowercase.

That will probably have to come with experience. Though maybe a quick annotated example might help

int doubleNumber(int i) //making a function which takes an int
{
    return i * 2;
}
int main() //function that doesn't take anything
{
    int number = doubleNumber(5) //call the function doubleNumber with 5 as the argument
    return 0;
}
2 Likes

Hi Dan,

I just wanted to thank you for your time again. I read your response many times and it really helped me to understand the concepts discussed.

Cheers.

1 Like

Privacy & Terms