>But for now, I am frustrated with it.
The frustration doesn't go away, it just gives way to new frustrations as you learn more and become more confident. Though very few professions are as stimulating and rewarding.
>I'm trying to figure out if all the "get" statements can be linked into a struct.
The two options are to read each field individually as a line and assign them to the record, or read an entire formatted record and then parse it. Here is the former:
#include <stdio.h>
struct person {
char name[50];
char hometown[50];
char homestate[4];
char occupation[50];
};
int main ( void )
{
struct person temp;
while ( 1 ) {
/* Read a record */
if ( fgets ( temp.name, sizeof temp.name, stdin ) == NULL )
break;
fgets ( temp.hometown, sizeof temp.hometown, stdin );
fgets ( temp.homestate, sizeof temp.homestate, stdin );
fgets ( temp.occupation, sizeof temp.occupation, stdin );
/* Print the record */
printf ( "Name: %s", temp.name );
printf ( "Hometown: %s", temp.hometown );
printf ( "Homestate: %s", temp.homestate );
printf ( "Occupation: %s\n", temp.occupation );
}
return 0;
} And the latter:
#include <stdio.h>
#include <string.h>
struct person {
char name[50];
char hometown[50];
char homestate[3];
char occupation[50];
};
int main ( void )
{
struct person temp;
char buffer[BUFSIZ];
while ( 1 ) {
/* Read a line */
if ( fgets ( buffer, sizeof buffer, stdin ) == NULL )
break;
/* Parse the line */
strcpy ( temp.name, strtok ( buffer, ":" ) );
strcpy ( temp.hometown, strtok ( NULL, ":" ) );
strcpy ( temp.homestate, strtok ( NULL, ":" ) );
strcpy ( temp.occupation, strtok ( NULL, ":" ) );
/* Print the record */
printf ( "Name: %s\n", temp.name );
printf ( "Hometown: %s\n", temp.hometown );
printf ( "Homestate: %s\n", temp.homestate );
printf ( "Occupation: %s\n", temp.occupation );
}
return 0;
} Please take note that the code I just gave is very unsafe in production code, and should be viewed as an example of concept, not a complete implementation.