Sunday, July 12, 2009

C++, I have 4 strings, Hard, Medium, Easy. The fourth is input. Make it see "easy" as "Easy"

My program asks what difficulty they would like their program, without having 3 more strings called easy, medium and hard (no caps) how can I change the input or make it ignore whether the input is in caps or not?

C++, I have 4 strings, Hard, Medium, Easy. The fourth is input. Make it see "easy" as "Easy"
use a upcase function





I don't know C++ (I use delphi) so I don't know C++ syntax but if you use upcase to convert both strings to uppercase then you can compare them that way. I.E





X = upcase(difficulty)


Y = upcase(input)





if X = Y then {Some Code}





or just





if upcase(difficulty) = upcase(input) then {Some Code}





hope this helps :)
Reply:#include %26lt;algorithm%26gt;


#include %26lt;string%26gt;


#inculude %26lt;ctype.h%26gt;





string input;


// fill input, yada yada yada


std::transform(input.being(), input.end(), input.begin(), _tolower);





You can also do it with locales, but I'm not familiar enough with them.
Reply:convert both to lower case and do a strcmp.


No comments:

Post a Comment