simple return back to main

This is a discussion on simple return back to main within the C Programming forums, part of the General Programming Boards category; what i want to do is return from the function back to main, if the person hits q to quit ...

  1. #1
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32

    simple return back to main

    what i want to do is return from the function back to main, if the person hits q to quit .

    Code:
    void loadData ( struct schoolData * FM ) {
         
         char temp[BUFSIZ];
         int i;
      
    printf( "hit enter after each name ( q to enter ages of family ) \n");
         for ( i = 0; i < MAX; ++i) {
               if ( temp[0] == 'q' ) {
               printf("we will now be quiting");
               return 0;  }
    
    printf( "\n &#37;s  - please enter the first name.\n" , name );
         fgets ( temp , sizeof ( temp ), stdin );
         FM->fname = ( char * ) malloc ( strlen ( temp ) + 1 );
         strcpy ( FM->fname , temp );
    
            if ( temp[0] == 'q' ) {
            printf("we will now be quiting");
            return 0;
    }
            printf( "%s  - now please enter the last name.\n" , name );
            fgets ( temp , sizeof ( temp ), stdin );
            FM->lname = ( char * ) malloc ( strlen ( temp ) + 1 );
            strcpy ( FM->lname , temp ); }
    }
    sorry for the identation!
    what would be the right way to do this
    Last edited by tat; 11-11-2007 at 07:06 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    return;

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    if ( temp[0] == 'q' )
    Additionally, you never initialize temp, so this if() statement could be either true or false. Either initialize temp, or prompt the user for a value that you'll store in temp, or both.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Quote Originally Posted by Cactus_Hugger View Post
    Code:
    if ( temp[0] == 'q' )
    Additionally, you never initialize temp, so this if() statement could be either true or false. Either initialize temp, or prompt the user for a value that you'll store in temp, or both.
    forgive me for being a N00b here

    whats this then?
    Code:
    char temp[BUFSIZ];
    the return;
    only gives me an infinite loop
    and dont return

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by tat View Post
    whats this then?
    Code:
    char temp[BUFSIZ];
    not initialized. you want this:
    Code:
    char temp[BUFSIZ] = {0};
    the return;
    only gives me an infinite loop
    and dont return
    what is the compilable code?

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    whats this then
    That declares a character array of BUFSIZ chars. But what is actually stored within that array is unknown, until you set it. So your first call to temp[0] could return anything.

    As for the infinite loop - is whatever that calls this function doing so in a loop?
    Also, indent your code, move the braces to some logical position so that readers won't have to hunt for the closing brace to that for() loop.

    (And what if a person's name starts with q?)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Code:
    void load_family ( struct family * FM ) 
    {
      char temp[BUFSIZ] = {0};
      int i;
         
      printf( "hit enter after each name ( q to enter ages of family ) \n");
         for ( i = 0; i < MAX; ++i) 
         {
            if ( temp[0] == 'q' ) 
               {
            printf("we will now be quiting");
            return;
         }
            
            printf( "\n &#37;s  - please enter the first name.\n" , name );
            fgets ( temp , sizeof ( temp ), stdin );
            FM->fname = ( char * ) malloc ( strlen ( temp ) + 1 );
            strcpy ( FM->fname , temp );
            
            if ( temp[0] == 'q' ) 
            {
            printf("we will now be quiting\n");
            return;
         }
            printf( "%s  - now please enter the last name.\n" , name );
            fgets ( temp , sizeof ( temp ), stdin );
            FM->lname = ( char * ) malloc ( strlen ( temp ) + 1 );
            strcpy ( FM->lname , temp );
         }
        
      }
    when it starts the function it displays the 3 printf right away (hit enter after each name ( q to enter ages of family ), first name, last name)

    i dont get an infinite loop anymore
    thanks to
    Code:
    char temp[BUFSIZ] = {0};
    but it returns back to the beginning on the function and prints this again
    Code:
     printf( "hit enter after each name ( q to enter ages of family ) \n");
    (And what if a person's name starts with q?)
    i guess i will have to change it to a yes or no
    thanks for the reply
    Last edited by tat; 11-11-2007 at 08:04 PM.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Another thing is, your function is declared to return nothing (void), but your return statements all return integers (return 0). You should either change the functions return type to int, or just do return; without a value.

  9. #9
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    im just going to have to redo this whole function. it just isnt right

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It doesn't look that bad.

    However, you may want to consider checking if the user wants to quit before you allocate and set the name in the structure - saves a bit of memory leaky behaviour. You may also want to check that the ENTIRE string is "q" - say someone is named "Quentin" [and the user enters it with lower-case, or you fix your code to allow "q" and "Q" to quit - it is considered user-friendly to not care if it's lower or upper case].

    You probably also don't need to check for "quit" twice in that function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 06:03 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 11:14 PM

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