toupper problem
can someone tell me what is worng with this code and how to solve that problem?
struct SRoom
{
string sRoom1;
// other stuff
};
SRoom Room; // tag for SRoom
cout << "What will you do?\n";
cin >> Room.sRoom1;
if (toupper(Room.sRoom1) == "spell") {FSpell();}
Could I be any dumber?
(What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
October 29, 2001 08:00 AM
If you toupper the string... you have to check it against..
"SPELL".. and not "spell". If you use "spell" then do a tolower, not a to upper... you are upper casing the string, then comparing it to a LOWER case string.. it will ALWAYS be false. Anyways, I don''t think you can do a direct == on strings (unless it''s been overloaded in something you included).
Billy
"SPELL".. and not "spell". If you use "spell" then do a tolower, not a to upper... you are upper casing the string, then comparing it to a LOWER case string.. it will ALWAYS be false. Anyways, I don''t think you can do a direct == on strings (unless it''s been overloaded in something you included).
Billy
October 29, 2001 08:01 AM
Try using "SPELL" instead. Remember, toupper() converts to upper case, not lower.
quote: Original post by Anonymous Poster
Anyways, I don''t think you can do a direct == on strings (unless it''s been overloaded in something you included).
You can. Look up the string class in the Standard C++ Library (<string>).
if you''re using c/c++ then you might want to read up on what "toupper" actually takes as input and returns. "toupper" does NOT operate on strings, it operates on characters. unless you overloaded your own version of "toupper".
// definition for "toupper". int toupper(int);
// definition for "toupper". int toupper(int);
char src[32];char dst[32];void main(void) { int i; strcpy(src, "hello"); // convert source string to upper case. for (i = 0; i <= strlen(src); i++) { dst = (char)toupper((int)src));<br> }<br> // dst = "HELLO";<br>}<br> </pre> <br><br> peace. </i> <br><br>To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion… To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Hi pal!
If your didn''t overloaded the operator ==, like Billy said, you''ll have to use strcmp() to compare the strings.
strcmp() accepts 2 strings as parameters, will return a value:
<0 if string1 < string 2
0 if equal
>0 if string1 > string 2
or something like that.
Anyway, try the if like this:
if(!strcmp(string1, string2))
//The strings were the same here
You can use strcmpi() also if you don''t want it to be case sensitive.
Hope it helps!
See ya!
SKeTch
If your didn''t overloaded the operator ==, like Billy said, you''ll have to use strcmp() to compare the strings.
strcmp() accepts 2 strings as parameters, will return a value:
<0 if string1 < string 2
0 if equal
>0 if string1 > string 2
or something like that.
Anyway, try the if like this:
if(!strcmp(string1, string2))
//The strings were the same here
You can use strcmpi() also if you don''t want it to be case sensitive.
Hope it helps!
See ya!
SKeTch
SKeTch
*sigh*
Use the Standard C++ Library functions:
toupper is defined in <cctype> as follows:
Use the Standard C++ Library functions:
#include <cctype> // the C++ toupper is declared here#include <string>using namespace std;struct SRoom{ string sRoom1;// other stuff};SRoom Room; // tag for SRoomcout << "What will you do?\n";cin >> Room.sRoom1;if (toupper(Room.sRoom1) == "spell") {FSpell();}
toupper is defined in <cctype> as follows:
template E toupper(E c, const locale& loc) const;
Oluseyi:
That doesn''t work!
When I try that, I get an error declaring that
''toupper'' : cannot convert parameter 1 from ''class std::basic_string,class std::allocator >'' to ''int''
Could I be any dumber?
(What do you mean, "No"?)
That doesn''t work!
When I try that, I get an error declaring that
''toupper'' : cannot convert parameter 1 from ''class std::basic_string,class std::allocator >'' to ''int''
Could I be any dumber?
(What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
My bad, the C++ toupper() is a method so it needs an object to "hang off" of:
#include <cctype>#include <iostream>#include <string>using namespace std;string str;ctype<char> ty;int main(int argc, char *argv[]){ cout << "Enter random-case text: "; cin >> str; ty.toupper(str.begin(), str.end()); cout << "The text you entered, in uppercase, is:" << endl << str << endl; return 0;}
oops. didn''t know you were talking about C++ STL. my bad.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement