But you are better not to use a string for this purpose. It's not worth trying to cope with all the variations of case a user might potentially input. So just stick with a single char and convert it to upper or lower case then test that. There is an example here.
@TheIdeasMan
Ok I will look into enums and I will try to figure it out. I thank you for your help and if i need any more assistance I will let you know
#include <iostream>
#include <string>
#include <locale> // needed for std::tolower()
int main()
{
std::locale loc; // needed for tolower()
std::cout << "Welcome to a world of imagination. You are in Area 1.\n"
<< "(Go North to Area 2.) : ";
std::string direction;
std::getline(std::cin, direction); // why getline? So you get ALL the characters entered.
std::cout << "\n";
// take the first element in the string, direction[0]
// use tolower() to convert the character to lower case
// compare if that equals a single-byte character
if (std::tolower(direction[0], loc) == 'n')
{
std::cout << "Good, you made it to Area 2.\n";
}
else
{
std::cout << "No, I said, \"Go North!\"\n";
}
}
Welcome to a world of imagination. You are in Area 1.
(Go North to Area 2.) : w
No, I said, "Go North!"
Welcome to a world of imagination. You are in Area 1.
(Go North to Area 2.) : N
Good, you made it to Area 2.
@FurryGuy
Thank you for the time you put into making the code (even though for you it probably took like 2 minutes lol). As stated before im freshly learning and everything everyone is posting is really helpful. Thank you all.
Edited:
Currently I am reading through the Tutorials (http://www.cplusplus.com/doc/) which so far i find better than the C++ for dummies (6ed) I have been using and my next step is to create a loop for when the user does not input the correct direction (without the infinite loop dilemma) so the user can attempt to reenter the correct input.
Reworking the code too longer than two minutes, really. :)
Any time I used was worth it because it helped you to learn some new C++ concepts that will help you in the future if you decide to pursue a programming career.
You did most of the work by posting your code, so congratulate yourself. :)
TheIdeasMan had good suggestions, using enums. I used another method (std::string/tolower) to arrive at the same place, code that works the way you want.
So I think it is not really advisable to use the standard function tolower() that is normally supposed to be defined in the header file #include <cctype> ? Right?
The function tolower() is in #include <cctype> , not in the header #include <locale>
BUZZ! Wrong!
My usage of std::tolower() IS in <locale>, if you would ever bother to look:
http://www.cplusplus.com/reference/locale/tolower/
A less robust version is in the C Library <cctype>, but I chose to use the C++ version.
<locale> std::tolower works with templated char types. <cctype> tolower only works with int.
<locale> std::tolower overloads the C <cctype> function. Notice "overloads." So the <locale> function template can use more than a single type of input.
@FurryGuy
> So the <locale> function template can use more than a single type of input.
What do you mean by "more than a single input"? Since it is a template function, can it do the following :
my next step is to create a loop for when the user does not input the correct direction (without the infinite loop dilemma) so the user can attempt to reenter the correct input.
Mash some code, then. While studying.
And when you have problems and errors, not if, post the code so others can help you. :)
There are books, good books far better than dummy books, that would be a help for learning C++. Here's one list:
https://isocpp.org/wiki/faq/how-to-learn-cpp#buy-several-books
CPlusPlus has a good tutorial section: http://www.cplusplus.com/doc/
Another C++ tutorial site: http://www.learncpp.com/
Yes without anything other than what I have shown. I chose char[] to avoid hidden linkage via <string>. I noticed it the other day on my machine, saw the comments here, et voila you can try it yourself.