Thread: Function

  1. #1
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Function

    Hello, all
    Been trying to make this a bit cleaner and wanted to take the majority of whats in main and make it a function that is called by Main. I have tried various function formats void, int etc... ideas? Also was trying to check the condition of the user inputs ....to verify its a 1 or o and with font that its withing 1-7 range...did this with do while loops...easier/cleaner way?

    Thanks,
    Matt

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define YES 1
    #define NO 0 // not used

    struct data {
    char* text;
    int bold, italic, underline, font;
    };

    void createPage ( struct data *array, int num);

    int main ( void ) {
    int response, i, fresponse;
    char buff[BUFSIZ];
    struct data myStructArray[80];

    // this loop in a function would be a good idea
    for ( i = 0 ; i < 80 ; i++ ) {
    printf( "Please enter a line of text ('$' to exit): " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );

    //This checks to see if the user wants to end program //

    if ( buff[0] == '$' )
    break;


    myStructArray[i].text = malloc( strlen(buff)+1 ); // allocate space
    strcpy( myStructArray[i].text, buff );

    do
    {
    printf( "\nDo you want text to be bold?[1/0] " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );
    myStructArray[i].bold = response;
    }while (response != YES && response != NO);

    do{
    printf( "\nDo you want text to be Italics?[1/0] " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );
    myStructArray[i].italic = response;
    }while (response != YES && response != NO);

    do{
    printf( "\nDo you want text to be Underlined?[1/0] " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );
    myStructArray[i].underline = response;
    }while (response != YES && response != NO);


    do{
    printf( "\nDo you size font do you wish to use?[1-7] " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &fresponse );
    myStructArray[i].font = fresponse;
    }while (fresponse > 7 || fresponse <= 0);


    }

    createPage( myStructArray, i );
    return 0;
    }

    void createPage( struct data *a, int num ) {
    int i;
    FILE* outFile;
    char buff2[BUFSIZ];
    outFile = fopen( "phaseTwo.html","w" );

    /* All HTML files must start with this tag. */
    fprintf( outFile,"<html>\n" );

    for ( i = 0 ; i < num ; i++, a++ ) {
    if(a->bold == YES) {
    fprintf( outFile,"<b>" );
    }
    if(a->italic == YES) {
    fprintf( outFile,"<i>" );
    }
    if(a->underline == YES) {
    fprintf( outFile,"<u>" );
    }
    sprintf(buff2,"<FONT SIZE = %d",a->font);
    fprintf( outFile,buff2);

    /* Write to the file HTML representing a line of text. */
    fprintf( outFile,"<p>" );
    fprintf( outFile,"%s",a->text );
    fprintf( outFile,"</p>\n" );

    if(a->bold == YES) {
    fprintf( outFile,"</b>\n" );
    }
    if(a->italic == YES) {
    fprintf( outFile,"</i>\n" );
    }
    if(a->underline == YES) {
    fprintf( outFile,"</u>\n" );
    }

    fprintf( outFile,"</font>\n" );


    }

    fprintf( outFile,"</html>\n" );
    fclose( outFile );
    }

  2. #2
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Disregard...will just leave in main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM