# Structure problem

• 11-29-2001
mattz
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!
• 11-29-2001
mattz
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...
• 11-29-2001
quzah
Something like...
Code:

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

Quzah.
• 11-29-2001
mattz
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 :)
• 11-29-2001
mattz
Quzah Are you going to be around tomorrow ? Would like to know if you could help me through this problem?

Thanks.
• 11-29-2001
quzah
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.
• 11-30-2001
Unregistered
Thanks, will poke around with this today during my work breaks!
• 11-30-2001
Unregistered
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");

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?
• 11-30-2001
Salem
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 ); }```
• 11-30-2001
mattz
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-30-2001
mattz
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" );

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