Re: Trying to quit program

From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/23/04


Date: Mon, 23 Feb 2004 03:19:05 GMT

On Sun, 22 Feb 2004 22:45:03 GMT, "Benjamin" <benjamin@innocent.com>
wrote in alt.comp.lang.learn.c-c++:

I have a few suggestions about your program, but first some posting
suggestions:

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">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
> <META content="MSHTML 6.00.2800.1276" name=GENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <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?
>
> //toupper.c
> //02/20/2004
> //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
should add:

  fflush(stdout);

...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;".

> }
>
> Ben

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: ");
   fflush(stdout);
   scanf("%c", &c);

   if (isalpha(c))
   {
      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)
   {
      break;
   }
}

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


Relevant Pages

  • Re: malloc + 4??
    ... >the screen or to a file, there aren addition 4 characters. ... This loop tries to count the size of the file by calling getc ... until getcreturns EOF. ... yet also return the special marker value EOF. ...
    (comp.lang.c)
  • [SUMMARY] Word Loop (#149)
    ... cheater is a wonderful label for a programmer to have. ... the first priority is to find a possible loop. ... puts before+letter+looplets.shift ...
    (comp.lang.ruby)
  • Re: PolAdtEv
    ... I did try posting a question about this on ... entire array instead of just the first 254 characters? ... >This is more a WMI question and not SMS related. ... tried tojsut get the data from the registry into ...
    (microsoft.public.sms.inventory)
  • Re: The Dacrons do Florida! (in flash)
    ... Regarding posting to the end or beginning of posts, I personally *hate* having to scroll and weed through stuff I've already read to get to the new part. ... Regarding Frank's statement about "hitting the return key every 70 characters or so" seems to me to be a complaint for complaint's sake. ... At the bottom of the window choose Plain Text for News Sending Option. ... Go in and reset your wrap character count. ...
    (rec.outdoors.rv-travel)
  • Re: Get the path and namefile in run time
    ... function Get_Path_Only return String; ... -- This returns the first N characters of the program name. ... end loop; ...
    (comp.lang.ada)