Re: Trying to quit program
From: Jack Klein (jackklein_at_spamcop.net)
Date: Mon, 23 Feb 2004 03:19:05 GMT
On Sun, 22 Feb 2004 22:45:03 GMT, "Benjamin" <email@example.com>
wrote in alt.comp.lang.learn.c-c++:
I have a few suggestions about your program, but first some posting
Usenet technical discussion groups are a text-only communication
medium. You need to do two things to your Outlook Express newsreader
to post properly.
First, find the setting in the menus that sets posting to newsgroups
to "text only", so it doesn't automatically add HTML content, part of
which I have copied from your original post:
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
> <META content="MSHTML 6.00.2800.1276" name=GENERATOR>
> <BODY bgColor=#ffffff>
HTML is for web pages, and is what some might like in their email, but
it is certainly not for posting to newsgroups like this one.
The second setting you need to make is to tell your software to insert
line breaks every 72 characters or less.
> Could someone tell me what I am doing incorrectly. I want the program to exit when I press the z key. Also I get two Please enter a character and the character you entered... each time it loops. What am I missing here?
> //vers. 0.1a
> //Purpose to convert a lower case input and display an upper case character. Exit when z is pressed
> //Need to include to use printf and scanf
> #include <stdio.h>
> //Need to include to use toupper and tolower
> #include <ctype.h>
> int main(void)
Excellent, I am glad to see that you know main() returns an int!
> char c; //Declare a char var for upper case
Two things about the line above. First, unless you have a C99
conforming compiler, comments starting with "//" are not legal C.
Second you should initialize c here where you define it, like this:
char c = 0;
> char b; //Declare a char var for lower case
> //Keep going until z is entered
> while (c !='z' )
The first time your program enters this loop the way you initially
defined 'c', it has an indeterminate value. If just might happen to
have the value of the character 'z', which would cause your program to
end before it really starts. Also using the value of an indeterminate
value is undefined behavior. If you define c with an initial value of
0 like I suggested, your first test will compare 0 to 'z', the loop
will execute the first time, and you avoid undefined behavior.
> printf("\nPlease enter a character: ");
> scanf("%c", &c);
On some computers where C is used, the prompt will not appear until
after the user presses the return or enter key. If you are going to
end a prompt without a '\n', which forces it to appear right away, you
...before the scanf().
> //Convert to upper case
> c = toupper(c);
> b = tolower(c);
> //Print it out
> printf("\nThe character you entered was %c and in uppercase it is %c\n", b,c);
> return 0;
More points for the proper "return 0;".
The main cause of your problem is the fact that you don't and can't
enter just one character into scanf().
If you enter a lower case letter 'a' the first time, you actually
enter two characters, 'a' and '\n'. The first pass of the loop
processes the 'a', then the second pass retrieves the '\n'. Since
your program is only supposed to operate on letters, you could do
something like this:
for ( ; ; )
printf("\nPlease enter a character: ");
c = toupper(c);
b = tolower(c);
printf("\nThe character you entered was %c "
"and in uppercase it is %c\n", b,c);
if ('z' == c)
Two things about this approach: first you don't test 'c' until after
you have read something into it, so you do not need to initialize it.
Second you will ignore '\n', and numeric digits and punctuation marks
and all other characters for which toupper() and tolower() don't have
any real use. The program will just prompt for another character.
-- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html