Array, reading in response etc...

This is a discussion on Array, reading in response etc... within the C Programming forums, part of the General Programming Boards category; Hello, First thanks to all who have helped me get to this point. I have a general question on how ...

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

    Array, reading in response etc...

    Hello,
    First thanks to all who have helped me get to this point.

    I have a general question on how to tackle this section of problem. I need to read in a response from user (from 1 to 7) to determine the size of font to use in the text held in the structure array. Understand how to do a normal "scanf("%d", &fresponse) but not sure how to set it up. See the code below for a better understanding of problem....I need to read in response on size of text, apply it to the particular structure array of text then in the function apply it to the various fonts of 1 to 7:
    #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 );

    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;

    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;

    }

    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>" );
    }
    if(a->font == 1) {
    fprintf( outFile,"<FONT SIZE = 1" );

    }

    //First attempt//

    // fprintf( outFile,"<FONT SIZE = ", &fr );



    /* 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" );
    }
    if(a->font== 1) {
    fprintf( outFile,"</font>\n" );
    }

    }

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

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Something like this -

    Code:
    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 ); 
    }
    zen

  3. #3
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Thanks very much. It works great. Could you help me understand what you have done here:
    //you declare another char array "buff2" with the size [BUFSIZ].

    char buff2[BUFSIZ]


    /// assign the value of a-> font (example 7) to <FONT SIZE=%d>//


    sprintf(buff2,"<FONT SIZE = %d",a->font);
    fprintf( outFile,buff2);

    I did some research on the sprintf - seems to allow you to use
    (char *, const char, char *) correct?


    Just trying to make sure I understand this for future use

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Thanks very much. It works great. Could you help me understand
    what you have done here:

    //you declare another char array "buff2" with the size [BUFSIZ].

    char buff2[BUFSIZ]
    Right. This is common practice. Replace 'BUFSIZ' with whatever size you want your buffer to be.

    /// assign the value of a-> font (example 7) to <FONT SIZE=%d>

    sprintf(buff2,"<FONT SIZE = %d",a->font);
    fprintf( outFile,buff2);

    I did some research on the sprintf - seems to allow you to use
    (char *, const char, char *) correct?
    Close. All of the 'printf' functions, and 'scanf' functions use similar formats:

    printf( "constant string", other arguments );

    sprintf is the same, except it takes:

    sprintf( to_here, "this format", these_arguments );

    What happens in the 'printf' type functions is, the first argument is snipped up into chunks, and these chunks are looked at, to see what the following arguments are supposed to be. Thus, when this:

    "<FONT SIZE=%d>"

    is encountered, it sees the "%d" section, and says, "Ah, the next argument is an integer, because that's what '%d' means!". So then, it replaces the "%d" section, with the value of whatever the next argument is, in integer form. So, if 'a->font' is 7, then the resulting code becomes:

    "<FONT SIZE=7>"

    That help?

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

  5. #5
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Excellent...understand now.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data into a 2D array
    By swgh in forum C Programming
    Replies: 2
    Last Post: 08-17-2007, 04:07 AM
  2. reading into array
    By 98holb in forum C Programming
    Replies: 1
    Last Post: 01-26-2006, 11:35 AM
  3. Reading individual strings from an array.
    By Baaaah! in forum C Programming
    Replies: 2
    Last Post: 12-20-2005, 03:42 PM
  4. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 01:31 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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