Thread: Moving to the next structure array

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

    Unhappy Moving to the next structure array

    Hello All,
    I need some help here. I have my attached program below. The problem I am having is, I seem to keep writing over the same structure array. I need to increment to the next array some how and after all text is gathered, pass to the structure pointer and then the function. I should be able to print out mulitiple lines of text, but it seems the last array is all that is passed to function.

    Help?

    //Code//


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

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



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

    void createPage( struct data *);

    int main(void) {


    int response;
    char buff[BUFSIZ];
    struct data myStructArray[20];
    do
    {
    fflush(stdin);
    printf( "Please enter a line of text: " );
    fgets( buff, BUFSIZ, stdin );

    myStructArray[0].text = malloc( 100 ); // allocate space
    strcpy( myStructArray[0].text, buff );

    while ( 1 ) {
    // Process text?
    printf( "\nDo you want text to be bold?[1/0] " );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );

    if ( response == YES ) {
    //* Assign value of text to bold address *//
    myStructArray[0].bold = YES;
    puts( "Text will be in Bold" );
    break;
    } else
    break;
    fflush(stdin);
    }
    while ( 1 ) {
    // Process text?
    printf( "\nDo you want text to be in Italics?[1/0] " );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );

    if ( response == YES ) {
    //* Assign value of text to italics address *//
    myStructArray[0].italic= YES;
    puts( "Text will be in Italics" );
    break;
    } else
    break;
    }
    fflush(stdin);

    while ( 1 ) {
    // Process text?
    printf( "\nDo you want text to be Underlined?[1/0] " );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );

    if ( response == YES ) {
    //* Assign value of text to underlined address *//
    myStructArray[0].underline= YES;
    puts( "Text will be in Italics" );
    break;
    } else
    break;
    }

    createPage( myStructArray );


    printf("Would you like to enter another line of text?[1/0] ");
    scanf("%d", &response);

    } while (response != 0);

    // createPage( myStructArray );
    return 0;
    }

    void createPage( struct data *a) {
    /* Create the output file */
    FILE* outFile;
    outFile = fopen( "phaseTwo.html","w" );

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


    if ( a->bold == YES ) {
    fprintf( outFile,"<b>" );
    }

    if ( a->italic == YES ) {
    fprintf( outFile,"<i>" );
    }

    if ( a->underline == YES ) {
    fprintf( outFile,"<u>" );
    }


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

    /* Write to the file HTML representing a line of bold text. */
    if ( a->bold == YES ) {
    fprintf( outFile,"</b>\n" );
    }

    /* Write to the file HTML representing a line of Italics text. */
    if ( a->italic == YES ) {
    fprintf( outFile,"</i>\n" );
    }

    /* Write to the file HTML representing a line of underlined text. */
    if ( a->underline == YES ) {
    fprintf( outFile,"</u>\n" );
    }
    /* All HTML files must end with this tag. */
    fprintf( outFile,"</html>\n" );

    /* Close the output file */
    fclose( outFile );
    }
    /// end Code//


    Thanks...!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Man, what's with all those while(1) loops?
    And you still haven't figured out this code posting have you?

    Code:
    #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;
    };
    
    void createPage ( struct data *array, int num );
    
    int main ( void ) {
        int response, i;
        char buff[BUFSIZ];
        struct data myStructArray[20];
    
        // this loop in a function would be a good idea 
        for ( i = 0 ; i < 20 ; i++ ) {
            printf( "Please enter a line of text (blank to exit): " );
            fflush( stdout );
            fgets( buff, BUFSIZ, stdin );
            if ( strlen(buff) == 1 ) break;
    
            myStructArray[i].text = malloc( strlen(buff)+1 ); // allocate space
            strcpy( myStructArray[i].text, buff );
    
            printf( "\nDo you want text to be bold?[1/0] " );
            fflush( stdout );
            fgets( buff, BUFSIZ, stdin );
            sscanf( buff, "%d", &response );
            myStructArray[i].bold = response;
    
            printf( "\nDo you want text to be Italics?[1/0] " );
            fflush( stdout );
            fgets( buff, BUFSIZ, stdin );
            sscanf( buff, "%d", &response );
            myStructArray[i].italic = response;
    
            printf( "\nDo you want text to be Underlined?[1/0] " );
            fflush( stdout );
            fgets( buff, BUFSIZ, stdin );
            sscanf( buff, "%d", &response );
            myStructArray[i].underline = response;
        }
    
        createPage( myStructArray, i );
        return 0;
    }
    
    void createPage( struct data *a, int num ) {
        int i;
        FILE* outFile;
        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>" );
            }
    
            /* 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,"</html>\n" );
        fclose( outFile );
    }

  3. #3
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Thanks,...he he....and no have not figured it out yet ....
    FYI-Getting C for dummies tongiht

    Matt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. structure in array
    By nevrax in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:35 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM