Teaching myself C, quick inquiry

This is a discussion on Teaching myself C, quick inquiry within the C Programming forums, part of the General Programming Boards category; Before heading into computer science next year, I felt it would be advantageous to get a head start on C ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Teaching myself C, quick inquiry

    Before heading into computer science next year, I felt it would be advantageous to get a head start on C programming. So I have been using this sites tutorial, and while reading through I just had a quick question.

    This is copied from Tutorial #9 (please ignore comments)
    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;
    }
    I see that the program was written with string variables of 50, which clearly covers any name. However...I was wondering how you would modify it so that if string only contained, lets say 10 characters, and if the user imputed more than 10, and error would come up saying "The name is too long"? (just the code that would be needed)

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by Iconate View Post
    However...I was wondering how you would modify it so that if string only contained, lets say 10 characters, and if the user imputed more than 10, and error would come up saying "The name is too long"? (just the code that would be needed)
    Well it seems that a little more needs to be done for that. Firstly, when the user enters a too long name, then there is no newline at the end of the buffer. So i modified strip_newline to provide a return value that indicates whether it actually did any work.
    Code:
    int 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 0;   /* There was a newline, success */
            }
        }
        /* if we get all the way to here, there must not have been a newline! */
        return 1;  /* There was no newline, we failed to do anything */
    }
    Then the part where the name is read, needs to be tranformed to a loop that will not stop unless the user gives a good input.
    By storing the result of the strip_newline in an integer variable named 'namebad' in my code, we can test for correct input, and at the same time control the loop. We should also discard the extra characters the user might have typed so it is necessary to do an fflush on the stdin input stream.
    Code:
        int namebad;
        do {
            fflush(stdin);
            fgets( name, 50, stdin );
            namebad = strip_newline( name, 50 );
            if( namebad )
                printf("You have entered too long name.\nPlease enter name:");
        } while( namebad );
    In the place where the last name is also read the above code should be duplicated.
    When for any reason code needs to be duplicated in a program it is a good hint that the code should be rearranged to a function.
    Last edited by xuftugulus; 02-24-2008 at 03:03 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Awesome thankyou very much Ill have to test this out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:33 AM
  2. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  3. teaching "C" to beginning programmers (Grade 11's)
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2002, 07:06 AM
  4. Replies: 14
    Last Post: 06-11-2002, 07:36 PM
  5. Replies: 0
    Last Post: 04-30-2002, 08:24 PM

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