string class

Started by
5 comments, last by Afr0m@n 19 years, 4 months ago
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?
_______________________Afr0Games
Advertisement
#include <string>
using namespace std;
________________________________pro.gram.mer - an organism that turns caffeine into code
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@n
if (strInput == "male" || "Male")
if(strInput == "male" || strInput == "MALE"). There's no operator || defined for char *, or for std::string.

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
_______________________Afr0Games
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.
Thanks:) That worked:D
_______________________Afr0Games

This topic is closed to new replies.

Advertisement