Solved

Glitch using "tolower(getch())"

Posted on 1998-03-16
4
629 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.
0
Comment
Question by:zircon
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
Nexial earned 50 total points
Comment Utility
tolower is an unsafe macro unless you undefine the macro.

As a macro it evaluates the getch() twice:   this is perfectly legal but not what you want.

Your solution is either:
   1) #undef tolower   /* you already include ctype.h */

this will force the function to be used.

or preassign the results of getch() to a variable and do the
tolower on the variable.  i.e something like:

int
   c;
  c = getch();
  c = tolower(c);

0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
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.
0
 
LVL 1

Author Comment

by:zircon
Comment Utility
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.
0
 
LVL 1

Author Comment

by:zircon
Comment Utility
Oops!  The second definition is:

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

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now