We help IT Professionals succeed at work.

Glitch using "tolower(getch())"

zircon
zircon asked
on
899 Views
Last Modified: 2008-03-06
Here's a mystery I'd like some help with:

In a program I'm writing that has a menu where a user types a character to select an option displayed in the menu, I ran into a glitch using the statement:

while((c = tolower(getch())) != 'q')

During execution, this statement ignores the first character but reads the second character from stdin.  Any ideas why?  

I have the following include statements in the source code:

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

I tried taking the while statement apart to see what would happen in different scenarios.

If I use the following code, the first character is read from stdin, as I would expect:

c = getch();
while(tolower(c) != 'q')

But if I use the code below, the first character is ignored and the second character is read from stdin, just like the first version I listed above.

c = tolower(getch());
while(c != 'q')

My compiler is Symantec C++ v. 7.21.  The project is written and compiled as a DOS "release" program.  No warnings or errors occur at compile time or during linking.  I have not gotten any runtime errors so far.

This program is for an extra credit assignment in a C programming course I'm taking, so it's not a big deal if I don't get a solution.  I'm just really curious about why this glitch is happening.  The rest of the program works just fine.

Thanks in advance for your thoughts on this mystery.
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Nexial and imladris,

Thanks for the prompt responses.  The combination of your two responses explains the problem fully and accurately.  A a result, I'm giving Nexial an A for the answer on this question, and I'll post a question for imladris only to award an A worth 50 points for the comment.

Thanks again.

Author

Commented:
Good news! The statement

#undef tolower

did the trick.  My program now works perfectly.  

Two definitions of tolower were present in the ctype.h file:

#if !defined(_WIN32)
#define tolower(c)      (isupper(c) ? (c)+('a'-'A') : (c))

and

#ifndef __STDC__
/* additional code here */
#define _toupper(c)      ((c)-('a'-'A'))

So I'm still not sure what's causing the problem, but I think imladris is on the right track.

Thanks again.

Author

Commented:
Oops!  The second definition is:

#define _tolower(c)      ((c)+('a'-'A'))

Guess I wasn't paying close enough attention when I was copy/pasting.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.