breaking out of program

This is a discussion on breaking out of program within the C Programming forums, part of the General Programming Boards category; In regards to the default case, i need the prog to only end if there is no input. If i ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    breaking out of program

    In regards to the default case, i need the prog to only end if there is no input. If i put return 0; the prog ends only after one loop, and if i put break; the prog never ends.

    Can anyone tell me how i fix this?


    Code:
       int main(){
               
       
          while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
             switch ( buff[0] ) {
                case '0':
                   one (buff);
                   break;
                case '1':
                   one (buff);
                   break;
                case '2':
                   one (buff);
                   break;
                case '3':
                   one (buff);
                   break;
                case '4':
                   one (buff);
                   break;
             
                case '5':
                   one (buff);
                   break;
             
                case '6':
                   one (buff);
                   break;
             
                case '7':
                   one (buff);
                   break;
             
                case '8':
                   one (buff);
                   break;
             
                case '9':
                   one (buff);
                   break;
             
                default: 
                   two(buff);
                   return 0;
             }
          }
       }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Code:
       int main(){
          int quit = 0;
               
       
          while ( !quit && fgets( buff, BUFSIZ, stdin ) != NULL ) {
             switch ( buff[0] ) {
                case '0':
                   one (buff);
                   break;
                default: 
                   two(buff);
                   quit = 1;
             }
          }
       }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    15
    I'm still relatively new to C programming, although I completed a university course in programming with C. ^^; Haven't had much practice since then.

    Anyway, my question is, is it unprofessional to use a Go to statement in a case like this?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is it unprofessional to use a Go to statement in a case like this?
    Not as long as you can defend your choice of using goto to every yahoo who reads your code.
    My best code is written with the delete key.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Welcome Back Prelude
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. Need help breaking from a program
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 02-23-2002, 10:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21