whats wrong with this

This is a discussion on whats wrong with this within the C Programming forums, part of the General Programming Boards category; in the tutorials they dont really work... Code: #include <stdio.h> /* stdin, printf, and fgets */ #include <string.h> /* for ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    34

    whats wrong with this

    in the tutorials they dont really work...

    Code:
    #include <stdio.h>    /* stdin, printf, and fgets */
    #include <string.h>   /* for all the new-fangled string functions */
    
    /* this function is designed to remove the newline from the end of a string
    entered using fgets.  Note that since we make this into its own function, we
    could easily choose a better technique for removing the newline.  Aren't
    functions great? */
    void strip_newline( char *str, int size )
    {
        int i;
    
        /* remove the null terminator */
        for (  i = 0; i < size; ++i )
        {
            if ( str[i] == '\n' )
            {
                str[i] = '\0';
    
                /* we're done, so just exit the function by returning */
                return;   
            }
        }
        /* if we get all the way to here, there must not have been a newline! */
    }
    
    int main()
    {
        char name[50];
        char lastname[50];
        char fullname[100]; /* Big enough to hold both name and lastname */
    
        printf( "Please enter your name: " );
        fgets( name, 50, stdin );
    
        /* see definition above */
        strip_newline( name, 50 );
    
        /* strcmp returns zero when the two strings are equal */
        if ( strcmp ( name, "Alex" ) == 0 ) 
        {
            printf( "That's my name too.\n" );
        }
        else                                     
        {
            printf( "That's not my name.\n" );
        }
        // Find the length of your name
        printf( "Your name is %d letters long", strlen ( name ) );
        printf( "Enter your last name: " );
        fgets( lastname, 50, stdin );
        strip_newline( lastname, 50 );
        fullname[0] = '\0';            
        /* strcat will look for the \0 and add the second string starting at
           that location */
        strcat( fullname, name );     /* Copy name into full name */
        strcat( fullname, " " );      /* Separate the names by a space */
        strcat( fullname, lastname ); /* Copy lastname onto the end of fullname */
        printf( "Your full name is %s\n",fullname );
    
        getchar();
    
        return 0;
    }
    When i compile. it compiles fine but when i run, nothing happens, any ideas how to fix it?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    strip_newline() is declared as a void, however your using return.

    Try declaring the function as an int, and see where you get from there.
    Last edited by jamie85; 11-30-2005 at 05:47 PM.

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Works for me.

    The only thing that is likely to make "nothing happen" is that you should fflush(stdout) before each fgets to make sure everything is sent out before you read in the input. Some terminals are line buffered, and won't display anything until there is a newline. Your prompts don't have a newline.


    Quote Originally Posted by jamie85
    strip_newline() is declared as a void, however you use return.

    Try declaring the function as an int, and see where you get from there.
    There is nothing wrong with using "return" in a void function, because the return statement doesn't have a value.
    Last edited by cwr; 11-30-2005 at 05:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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