Structure problem

This is a discussion on Structure problem within the C Programming forums, part of the General Programming Boards category; Hi, Maybe someone can help me out a bit here. I need to create a structure and program that asks ...

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

    Unhappy Structure problem

    Hi,
    Maybe someone can help me out a bit here.
    I need to create a structure and program that asks the user to enters a line of text .
    It the prompts them 1 for yes 0 for no, if they would like it to be displayed as bold then ask if they want it displayed as italics, and then underlined. I need to Keep track of the user's responses for a single line of text within a single structure. And use an array of structures to hold all the information for all lines of text entered.

    Not sure what my structure should look like here?


    struct rec
    {
    int txt[100][100]; /* Text variable */
    int bold[100][100]; /* Bold variable */
    int uline[100][100]; /* Underline Variable */
    int italics[100][100]; /* Italics Variable */
    };
    ?

    pretty much need some where to start.

    Thanks!

  2. #2
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Forgot ...this array of structures gets past to an already created function..I just can't figure out how to define this structure to accept multiple lines of text...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Something like...
    Code:
    struct data {
        char* text;
        int bold,italic,underline;
    };
    ...should do the job. Read to a buffer, allocate text space, adjust your pointer, set your values for b/t/u.

    Quzah.
    Hope is the first step on the road to disappointment.

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

    Red face

    Okay thanks. ...will give it a try. Guess I need to re-read *sigh* the tutorial again for structures and allocating text space.
    This is a tough elective

  5. #5
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Quzah Are you going to be around tomorrow ? Would like to know if you could help me through this problem?

    Thanks.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    malloc()

    If you have an array of structures, you can go through the array, reading the string from the user, into an existing buffer, then copy that into a dynamicly allocated block of text for each array:

    struct data myStructArray[20]; /** 20 structures **/

    Here's how you allocate for our example:

    myStructArray[x].text = malloc( somesize );

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Unregistered
    Guest
    Thanks, will poke around with this today during my work breaks!

  8. #8
    Unregistered
    Guest

    Unhappy

    Update:

    I have tried to progress a bit and perhaps show some more of my work here. I know this is wrong but having trouble understanding how I can take my text that the user enters and assign it to the int value "BOLD" and pass ..just that to the function.

    Anyway this is the error which I keep getting because im trying to assign the char type to an int.

    '=' : cannot convert from 'int' to 'struct data *'

    #include <stdio.h>

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


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

    void createPage( struct data *);

    int main (void)

    {


    struct data myStructArray[20];
    struct data *st_ptr;
    st_ptr = &myStructArray[20];
    int response;


    printf("Please enter a line of text: ");
    scanf("%s", &myStructArray->text);

    fflush(stdin);

    while(1){
    // Process text?
    printf("\nDo you want text to be bold?[1/0] ");
    scanf("%d",&myStructArray->bold);

    if(response == YES){
    //* Assign value of text to bold address *//

    st_ptr = myStructArray->bold;
    puts("Text will be in Bold");
    break;
    }
    else
    break;
    }

    createPage(st_ptr);

    return 0;


    }

    void createPage( struct data *a)
    {

    /* Create the output file */
    FILE* outFile;
    outFile = fopen("phaseOne.html","w");

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

    /* 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. */
    fprintf(outFile,"<b>");
    fprintf(outFile,"%s",a->bold);
    fprintf(outFile,"</b\n");

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

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

    Ideas?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Voila!!!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define YES 1
    #define NO 0 // not used
    
    
    struct data {
        char* text;
        int bold, italic, underline;
    };
    
    void createPage( struct data *);
    
    int main(void) {
        char buff[BUFSIZ];
        struct data myStructArray[20];
    //    struct data *st_ptr;
    //    st_ptr = &myStructArray[20]; // points off the end of the array
        int response;
    
        printf( "Please enter a line of text: " );
        fgets( buff, BUFSIZ, stdin );
    
        // you might want to remove the \n which fgets writes into buff
        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;
        }
    
        createPage( myStructArray );
        return 0;
    }
    
    void createPage( struct data *a) {
        /* Create the output file */
        FILE* outFile;
        outFile = fopen( "phaseOne.html","w" );
    
        /* All HTML files must start with this tag. */
        fprintf( outFile,"<html>\n" );
    
        if ( a->bold == YES ) {
            fprintf( outFile,"<b>" );
        }
    
        /* 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" );
        }
    
        /* All HTML files must end with this tag. */
        fprintf( outFile,"</html>\n" );
    
        /* Close the output file */
        fclose( outFile );
    }

  10. #10
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    First of all THANK YOU very much. It works perfectly. I need to make a few adjustments and I NEED to read up on some of the statements you used and understand them . Are you going to be online this weekend anytime?

    Matt

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

    Cool Looping

    Okay,
    I have my program almost working. However, trying to do a loop and choose a do while loop. However, on the second pass through it has 2 major issues. One it does not allow text to be entered again and two it erases the first text and thus nothing passes through to the function. Please find attached 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 =0;
    char buff[BUFSIZ];
    struct data myStructArray[20];


    do
    {
    printf( "Please enter a line of text: " );
    fgets( buff, BUFSIZ, stdin );

    // you might want to remove the \n which fgets writes into buff
    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 );
    fflush(stdin);

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

    } while (response != 0);

    return response;
    createPage( myStructArray );
    return 0;
    }

    void createPage( struct data *a) {
    /* Create the output file */
    FILE* outFile;
    outFile = fopen( "phaseOne.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 );
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 10:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 03:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 10:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 11:52 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM

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