multiple returns, failed input, and gcc 
Author Message
 multiple returns, failed input, and gcc

hi there

i wrote a "guessing game"; the computer picks a number and the user has
8 tries to guess the number.  i have 2 questions; here is the code (with
the irrelevant stuff cut out):

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
        int i, utime, m, guess;
        long ltime;

        ltime=time(NULL);
        utime= (unsigned int) ltime/2;
        srand(utime);  
        m = rand()%301;

        for (i=1; i<9; i++) {
                printf("Guess #%d: ",i);
           scanf("%d", &guess);
           if (guess < m) printf("WRONG:  too low.\n" );
                else if (guess > m) printf("WRONG:  too high.\n" );
           else {
                        printf("You managed to get the right number!\n");
        return(0);
                }
        }

        printf("Too bad!  Now you'll never know the number!\n");

        return(0);

Quote:
}

question 1: is it bad programming style to have 2 returns?  i could
easily have the program exit at one point, but it would take a couple
more lines of code, including a flag variable for got-it/didn't-get-it
status.

question 2: if the user enters a letter at the scanf, the scanf read
fails; it puts the letter back into input and tries reading it again,
then the cycle repeats.  how can i get around this?  i'd like to be able
to do it only using either scanf and getchar if it's possible.  if not,
i'm not opposed to learning something new!  :-)

gcc questions:

question 3: is it better for someone learning C to always use the -ansi
and -pedantic options?  my thought is -pedantic would help write better
code by giving me warnings about what isn't good.

question 4: do the -O options (-O1, -O2, etc) optimize the speed of
the executable code or the length of the executable code?

thanks!

pete

--
############################################################################

   I BOYCOTT ANY COMPANY THAT USES MASS ADVERTISING TACTICS ON THE INTERNET



Fri, 02 Jan 1998 03:00:00 GMT  
 multiple returns, failed input, and gcc

(sliver)says...

Quote:

>hi there

>i wrote a "guessing game"; the computer picks a number and the user has
>8 tries to guess the number.  i have 2 questions; here is the code (with
>the irrelevant stuff cut out):

>#include <stdio.h>
>#include <stdlib.h>

>int main(void)
>{
>        int i, utime, m, guess;
>        long ltime;

>        ltime=time(NULL);
>        utime= (unsigned int) ltime/2;
>        srand(utime);  
>        m = rand()%301;

>        for (i=1; i<9; i++) {
>                printf("Guess #%d: ",i);
>           scanf("%d", &guess);
>           if (guess < m) printf("WRONG:  too low.\n" );
>                else if (guess > m) printf("WRONG:  too high.\n" );
>           else {
>                        printf("You managed to get the right number!\n");
>        return(0);
>                }
>        }

>        printf("Too bad!  Now you'll never know the number!\n");

>        return(0);
>}

>question 1: is it bad programming style to have 2 returns?  i could
>easily have the program exit at one point, but it would take a couple
>more lines of code, including a flag variable for got-it/didn't-get-it
>status.

I do not think that it is a bad programming style, but I would not do it. I
beleive in Single Exit point functions. It isolates the exit point where you can
do the cleanup and the most important think is that I can debug it with much
better control.

Quote:
>question 2: if the user enters a letter at the scanf, the scanf read
>fails; it puts the letter back into input and tries reading it again,
>then the cycle repeats.  how can i get around this?  i'd like to be able
>to do it only using either scanf and getchar if it's possible.  if not,
>i'm not opposed to learning something new!  :-)

Why not try fgets() ? and then convert the string to integer. If it is out of
range, disard it right away and let the user choose another.

About GCC......
I do not use GCC so I do not know anything about it.
--
MAG
/* ********************************************************
To understand recursion one must first understand recursion
******************************************************** */



Fri, 02 Jan 1998 03:00:00 GMT  
 multiple returns, failed input, and gcc

Quote:
>question 1: is it bad programming style to have 2 returns?  i could

It depends on whom you ask :-)  The Pascal bigots will answer YES,
the C bigots will answer NO :-)

My answer: it's primarily a matter of taste.  But unnecessarily
complicating your code simply to avoid an alternate return is bad taste.

Quote:
>question 2: if the user enters a letter at the scanf, the scanf read
>fails; it puts the letter back into input and tries reading it again,
>then the cycle repeats.  how can i get around this?  i'd like to be able
>to do it only using either scanf and getchar if it's possible.  if not,
>i'm not opposed to learning something new!  :-)

It is possible, but it's not worth the effort: after any scanf, use getchar
to eat anything left on the line, including the newline character.

The normal approach is to read a full line of input with fgets and use
sscanf to parse it.

Quote:

>gcc questions:

>question 3: is it better for someone learning C to always use the -ansi
>and -pedantic options?  my thought is -pedantic would help write better
>code by giving me warnings about what isn't good.

The right options for someone learning C with gcc are -Wall -pedantic.
-ansi isn't likely to make much difference (unless you use trigraphs :-)

Quote:

>question 4: do the -O options (-O1, -O2, etc) optimize the speed of
>the executable code or the length of the executable code?

They optimize for speed.  In the days of multi-gigabyte disks, nobody
seems to care for size optimizations (except for those in the embedded
control business).

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Fri, 02 Jan 1998 03:00:00 GMT  
 multiple returns, failed input, and gcc

Quote:

>hi there

>i wrote a "guessing game"; the computer picks a number and the user has
>8 tries to guess the number.  i have 2 questions; here is the code (with
>the irrelevant stuff cut out):

>#include <stdio.h>
>#include <stdlib.h>

>int main(void)
>{
>    int i, utime, m, guess;
>    long ltime;

>    ltime=time(NULL);
>    utime= (unsigned int) ltime/2;
>    srand(utime);
>    m = rand()%301;

>    for (i=1; i<9; i++) {
>            printf("Guess #%d: ",i);
>       scanf("%d", &guess);
>       if (guess < m) printf("WRONG:  too low.\n" );
>            else if (guess > m) printf("WRONG:  too high.\n" );
>       else {
>                    printf("You managed to get the right number!\n");
>            return(0);
>            }
>    }

>    printf("Too bad!  Now you'll never know the number!\n");

>    return(0);
>}

>question 1: is it bad programming style to have 2 returns?  i could
>easily have the program exit at one point, but it would take a couple
>more lines of code, including a flag variable for got-it/didn't-get-it
>status.

>question 2: if the user enters a letter at the scanf, the scanf read
>fails; it puts the letter back into input and tries reading it again,
>then the cycle repeats.  how can i get around this?  i'd like to be able
>to do it only using either scanf and getchar if it's possible.  if not,
>i'm not opposed to learning something new!  :-)

>gcc questions:

>question 3: is it better for someone learning C to always use the -ansi
>and -pedantic options?  my thought is -pedantic would help write better
>code by giving me warnings about what isn't good.

>question 4: do the -O options (-O1, -O2, etc) optimize the speed of
>the executable code or the length of the executable code?

>thanks!

>pete

Hello,
   I can answer your two first questions:
1)Just replace the return(0); with exit(0); & include <process.h> ,
<stdlib.h>!!!
2)Read a character at a time if it is fine enter it into a string when
'\n' is entered put a '\0' at the end of the string and convert it with
the apropriate function( i.e. atoi() );

          Hope I helped!!!
Michael



Sat, 03 Jan 1998 03:00:00 GMT  
 multiple returns, failed input, and gcc

Quote:

>hi there

>i wrote a "guessing game"; the computer picks a number and the user has
>8 tries to guess the number.  i have 2 questions; here is the code (with
>the irrelevant stuff cut out):

>#include <stdio.h>
>#include <stdlib.h>

>int main(void)
>{
>    int i, utime, m, guess;
>    long ltime;

>    ltime=time(NULL);
>    utime= (unsigned int) ltime/2;
>    srand(utime);
>    m = rand()%301;

>    for (i=1; i<9; i++) {
>            printf("Guess #%d: ",i);
>       scanf("%d", &guess);
>       if (guess < m) printf("WRONG:  too low.\n" );
>            else if (guess > m) printf("WRONG:  too high.\n" );
>       else {
>                    printf("You managed to get the right number!\n");
>            return(0);
>            }
>    }

>    printf("Too bad!  Now you'll never know the number!\n");

>    return(0);
>}

>question 1: is it bad programming style to have 2 returns?  i could
>easily have the program exit at one point, but it would take a couple
>more lines of code, including a flag variable for got-it/didn't-get-it
>status.

>question 2: if the user enters a letter at the scanf, the scanf read
>fails; it puts the letter back into input and tries reading it again,
>then the cycle repeats.  how can i get around this?  i'd like to be able
>to do it only using either scanf and getchar if it's possible.  if not,
>i'm not opposed to learning something new!  :-)

>gcc questions:

>question 3: is it better for someone learning C to always use the -ansi
>and -pedantic options?  my thought is -pedantic would help write better
>code by giving me warnings about what isn't good.

>question 4: do the -O options (-O1, -O2, etc) optimize the speed of
>the executable code or the length of the executable code?

>thanks!

>pete

Hello,
   I can answer your two first questions:
1)Just replace the return(0); with exit(0); & include <process.h> ,
<stdlib.h>!!!
2)Read a character at a time if it is fine enter it into a string when
'\n' is entered put a '\0' at the end of the string and convert it with
the apropriate function( i.e. atoi() );

          Hope I helped!!!
Michael



Sat, 03 Jan 1998 03:00:00 GMT  
 multiple returns, failed input, and gcc

Quote:
>1)Just replace the return(0); with exit(0); & include <process.h> ,
><stdlib.h>!!!

  What's the difference?  The function still has two exit points...  Changing the
  return to an exit doesn't solve the underlying 'moral' dilemma.  You just changed
  they way the function (and in this case the program) will end.  I have always found
  functions that have only one exit point to be more understandable, and, except in
  a few cases, have been able to code to this end.

  Chris



Sat, 03 Jan 1998 03:00:00 GMT  
 multiple returns, failed input, and gcc

: 1)Just replace the return(0); with exit(0); & include <process.h> ,
: <stdlib.h>!!!

Why should (the function) exit be used instead of (the keyword)
return?

Why should (the nonstandard) <process.h> be #included (for the snipped
program)?

--
Bob Farrell



Sat, 03 Jan 1998 03:00:00 GMT  
 multiple returns, failed input, and gcc

Quote:
> question 1: is it bad programming style to have 2 returns?  i could
> easily have the program exit at one point, but it would take a couple

    It depends. In this specific example some people would say that it
    is indeed suboptimal style because the code flows naturally to a
    single return anyway, eg...

            if (guess < m) ...
            else if (guess > m) ...
            else break;             /* or make guess != m part of the loop termination test */
        }
        if (i < 9) printf("You managed to get the right number!\n");
        else printf("Too bad!  ...

    The two sets of comparisons test for slightly different things.
    The first tests for accuracy, the second tests for successful
    completion. For code this small it's pretty irrelevant though.

Quote:
> question 2: if the user enters a letter at the scanf, the scanf read

    Replace the call to scanf() with a call to fgets(...,stdin). Then
    you can use atoi() to extract the guess value.

Quote:
> pete

--

***     "If pigs had wings, bacon'd be more expensive" - Jonas Glim     ***


Sun, 04 Jan 1998 03:00:00 GMT  
 multiple returns, failed input, and gcc

< snip >

Quote:
> question 1: is it bad programming style to have 2 returns?  i could
> easily have the program exit at one point, but it would take a couple
> more lines of code, including a flag variable for got-it/didn't-get-it
> status.

IMHO it's a case-by-case judgement call.  Other things being equal, I'd
rather have a single return, because it is more readable.  It is also
easier to debug: you have a single point at which to examine the state of
relevant variables just before returning, either with printf() or with
breakpoints.

However, other things are not always equal.  It order to retain a single
return statement you may have to resort to a lot of flag testing, deeply
nested ifs, or other clumsy contrivances.

The bad effects of an extra return statement are reduced if:

1. It occurs near the top of the function, disposing of some simple
   cases immediately without having somehow to bypass the guts of the
   function.

2. It occurs near the bottom of the function, so that if you look at
   the last return, you'll probably see the extra one too.  To make it
   more visible, try to make the two returns occur in different branches
   of an if/else.

3. It occurs in a very short function.  This case is a combination of the    
   previous two.  In general, brevity mitigates a lot of stylistic
   offenses.

4. It bails out of the function only for a rare and unexpected fatal
   error.  One has to code for such things, but one doesn't want to
   contort the code to accommodate them.

If your boss has religious objections to multiple returns, then respect
his or her wishes.  It won't be so very bad.  You may just have to think
a little more carefully about how your code is structured.  (Your boss
is probably reacting to previous abuses of the return statement.)

Scott McKellar
Southwestern Bell Telephone
St. Louis, MO



Fri, 09 Jan 1998 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. One return vs multiple returns

2. HELP!! GCC and FLEX input problem

3. C declaration (structure alignment tricks) fails under VC++ but work under gcc

4. Newbie Question:Ctrl-Z causes gcc to fail

5. gcc -Wall fails to catch wrong use of calloc

6. multiple getchar()'s in gcc (weird)

7. gcc and multiple typedefs -- ignore how?

8. Input loop that Fails after first pass

9. Input loop that Fails aft

10. Simulated Keyboard input and Accelerator Fails

11. Simulated Keyboard input and Accelerator Fails

12. strstr returns integer in gcc 2.5

 

 
Powered by phpBB® Forum Software