Archived

This topic is now archived and is closed to further replies.

toupper problem

This topic is 5888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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"?)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try using "SPELL" instead. Remember, toupper() converts to upper case, not lower.

Share this post


Link to post
Share on other sites
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);



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[i]));
}
// dst = "HELLO";
}


peace.


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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
*sigh*

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 SRoom


cout << "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;

Share this post


Link to post
Share on other sites
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"?)

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites