[ create a new paste ] login | about

Link: http://codepad.org/XMOK475V    [ raw code | output | fork ]

C, pasted on Dec 24:
#include <stdio.h>
#include <string.h>

int main( void )
{
    char name[20], race[20], *tmp;

    printf( "What is your hero's name? " );
    if( ! fgets( name, sizeof name, stdin ) )
    {
        printf( "An error occurred, sorry.\n" );
        return 1;
    }

    /* Remove trailing newline, if present. */
    if( ( tmp = strrchr( name, '\n' ) ) )
    {
        *tmp = 0;
    }

    printf( "Hello %s!\n", name );
    
    printf( "What is your race? " );
    if( ! fgets( race, sizeof race, stdin ) )
    {
        printf( "An error occurred, sorry.\n" );
        return 1;
    }
    
    if( ( tmp = strrchr( race, '\n' ) ) )
        *tmp = 0;

    printf( "So you are a %s.\n", race );

    printf( "Continue? [yn] " );
    switch( getchar() )
    {
        case 'Y':
        case 'y':
            printf( "Okay! Let's go!\n" );
            break;

        case 'N':
        case 'n':
            printf( "Okay, I can wait...\n" );
            break;

        case EOF:
            printf( "An error occurred, sorry.\n" );
            return 1;

        default:
            printf( "Invalid answer!\n" );
            /* You could put this all in a loop and loop until
             * you get a valid answer. Or you could assume that
             * y or n has been entered.
             */
            return 1;
    }

    return 0;
}


Output:
1
What is your hero's name? An error occurred, sorry.


Create a new paste based on this one


Comments: