943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 694
  • C++ RSS
Jan 13th, 2009
0

Distinguish whether the user is inputting a big letter or small

Expand Post »
So basically, I'm creating a switch statement that if the user enters a p or if they enter a P it will still work, any ideas? Thank you
Reputation Points: 41
Solved Threads: 7
Posting Whiz in Training
Phil++ is offline Offline
233 posts
since Jan 2009
Jan 13th, 2009
0

Re: Distinguish whether the user is inputting a big letter or small

C++ Syntax (Toggle Plain Text)
  1. switch (choice)
  2. {
  3. case 'p' || 'P': // do this
  4. break;
  5. case 's' || 'S': // do that
  6. }

won't work
Reputation Points: 41
Solved Threads: 7
Posting Whiz in Training
Phil++ is offline Offline
233 posts
since Jan 2009
Jan 13th, 2009
0

Re: Distinguish whether the user is inputting a big letter or small

Convert the string to evaluate to lower case, then use it.
I prefer if-else statments, of stings for input.

For ASCII
In a loop of string size:
if (is upper case(91< && 64>)) then
    swap_case()

Swap function:
function swap_case(&character)
    character XOR 32
Last edited by MosaicFuneral; Jan 13th, 2009 at 6:55 pm.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 13th, 2009
0

Re: Distinguish whether the user is inputting a big letter or small

Or for a more reliable solution (ei for more than just ASCII) you can use tolower() and toupper() functions found in the cctype header

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 14th, 2009
0

Re: Distinguish whether the user is inputting a big letter or small

Click to Expand / Collapse  Quote originally posted by Phil++ ...
C++ Syntax (Toggle Plain Text)
  1. switch (choice)
  2. {
  3. case 'p' || 'P': // do this
  4. break;
  5. case 's' || 'S': // do that
  6. }

won't work
Indeed. 'p' || 'P' would expand to a bool value of true, as will 's' || 'S'. Compilers will generally refuse to compile the above, as particular cases of a switch statement are not allowed to be duplicated.

Try this instead;
C++ Syntax (Toggle Plain Text)
  1. switch (choice)
  2. {
  3. case 'p':
  4. case 'P': // do this
  5. break;
  6. case 's':
  7. case 'S': // do that
  8. break;
  9. }
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Weird error... Please help
Next Thread in C++ Forum Timeline: Need help writing C++ as OOP (Beginner)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC