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 );
}