I'm trying to create a sort of text-adventure game, but rather than having to check for the input being uppercase or lowercase, I'd rather convert it before checking it in an if statement.
I've created a function, and I know it works because when I print the output, it shows up capitalized, however when I check it with an if statement, it doesn't pass.
which will convert the whole string to uppercase, all in one nice line :) This also introduces you to the <algorithm> header, which is incredibly useful.
toupper will return the uppercase equivalent of its argument.
tolower will do the same but with the lowercase equivalent.
This is out of my textbook on character conversions, hope it helps:
Each function simply returns its argument if the conversion cannot be
made or is unnecessary. For example, if the argument to toupper is not a lowercase letter, then toupper simply returns its argument unchanged. The prototypes of these functions are
int toupper(int ch);
int tolower(int ch);
The fact that these functions return an integer means that a statement such as
cout << toupper('a'); // prints 65
will print the integer that is the ASCII code of 'A' rather than printing the character 'A' itself. To get it to print the character, you can cast the return value to a char, as in
cout << static_cast<char>(toupper('a')); // prints A
or you can assign the return value to a character variable first and then print the character: