string class
Ok, so I decided to take a look at gametutorials.com. And whaddayaknow - I found a solution to a problem I've had for ages relating to string input in my MUD kernel, string input. But as soon as I had written this,
"#include <string>",
in my "Globals.h" file, and tried to use it like this,
"if (strInput == "male" || "Male")",
after having declared strInput as this,
"string strInput = "";",
I got an error stating that string was an undeclared identifier:( Does anyone know why?
Or use std::string. The STL classes are put into a namespace (thusly 'using namespace std;') so as to avoid naming conflicts with other libraries.
Quote:Original post by Afr0m@nif(strInput == "male" || strInput == "MALE"). There's no operator || defined for char *, or for std::string.
if (strInput == "male" || "Male")
Further, what happens of you get mixed case input, like "Male"? The best solution is to use std::transform along with std::toupper or std::tolower to convert to a single case, and then perform your tests on that.
Actually, I've used mix cased input comparison. But thanks to you, I remembered that I'll have to put in another else clause to remind the user of what to write if he/she writes something different. That's easier:D
Anyway, now I get an error stating this:
"C:\Programs\C++\MUD\NewGame.cpp(10) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conver
sion)".
What does this mean? This is how line 10 looks:
"cin >> strInput;"
They use the cin operator to retrieve user input in the gametutorials.com project too, and that project compiles nicely:'( I've always used cin, so I don't know of any other operator that does the same thing:(
And thanks to everyone so far:D
Anyway, now I get an error stating this:
"C:\Programs\C++\MUD\NewGame.cpp(10) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conver
sion)".
What does this mean? This is how line 10 looks:
"cin >> strInput;"
They use the cin operator to retrieve user input in the gametutorials.com project too, and that project compiles nicely:'( I've always used cin, so I don't know of any other operator that does the same thing:(
And thanks to everyone so far:D
I bet you're including iostream.h. Don't. It's pre-standard, and doesn't work with all the good stuff. Include iostream (no extension) instead.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement