![]() |
|
#1
|
||||
|
||||
there has to be a better wayHowdy I've got two functions that are almost exactly the same. The only difference is that one checks for uppercase letters the other does lowercase. I was just wondering if anyone could think of a more productive method of doing this?
Here's my code: CPP / C++ / C Code:
and the other function: CPP / C++ / C Code:
|
||||
|
#2
|
|||
|
|||
|
upper case characters are 65 - 90
lower case characters are 97 - 120 to convert from upper case to lower case: add 32 to convert form lower case to upper case: subract 32 CPP / C++ / C Code:
__________________
spasms!!! |
|
#3
|
||||
|
||||
|
I always use the functions tolower or toupper when I want to convert case. They are standard ansi C and they do checking to make sure that your can be converted to requested case. They are defined as:
CPP / C++ / C Code:
So that if have something like: CPP / C++ / C Code:
That should print a capital A in both cases. Cheers, d |
|
#4
|
||||
|
||||
|
Remove the switch block and add another parameter:
CPP / C++ / C Code:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#5
|
||||
|
||||
|
Tabs also don't port well to other IDEs. I agree with dsmith, and that's what I would've said. Just convert the characters to the proper case.
__________________
-Aaron |
|
#6
|
||||
|
||||
|
thanks everyone for your help. I like the ideal of using a booleen but it really just puts both functions into one, not really improving the code any. I also think that there is a little missunderstanding as to what I'm trying to do. I've making a chess game. The board (which is q.board[][] in these functions) is made of charaters. White pieces are uppercase (R, N, B, K, Q, P) and black pieces are lowercase (r, n, b, k, q, p). What the first function - CheckPiece- does is make sure that player one is selecting to move his own piece, i.e. the uppercase letters and vice verse with player 2. The second function - CheckMove- checks to see if the player is moving his piece onto his own piece. The reason for the first switch statement is to turn the user input row into the actual matrix index. Let me explain
Here's what the board looks like when printed: 1 2 3 4 5 6 7 8 A R N B K Q B N R B P P P P P P P P C D E F G p p p p p p p p H r n b k q b n r The user is then asked to enter which piece he would like to move. A1 would be a white rook, H4 would be a black king, and so on. In the matrix A1 is actually 0,0 so the first switch takes the A and converts it to zero. That switch has to be there. |
|
#7
|
||||
|
||||
|
Here is what I would do,find the ASCII numbers for each char and do some math on them..:
CPP / C++ / C Code:
you can do the same on other functions.. __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#8
|
||||
|
||||
|
Quote:
If chr has 'C', doesn't chr - 'A' equal 2? If chr has 'G', doesn't chr - 'A' equal 6? So what does the switch accomplish that chr - 'A' doesn't other than taking extra time and code space? __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#9
|
||||
|
||||
|
Quote:
my apologie's for misunderstanding your code. That is a much better way of doing it. Many, Many, Many thanks for all of your help Last edited by Garth Farley : 17-May-2004 at 11:41.
Reason: Now now, behave!
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The