Structures

A

alex

I've written this structure and allowed the user to initialise the
members in an array of this structure. I also want the program to
print to the screen whats in this array after its been initialised.
I've written the program below, but it doesn't do the last thing
properly...any ideas?

/*A program to allow user to populate an array structure*/
#include <stdio.h>

/*Declare a four member structure */
struct PayRecord {int number; char name[20]; float rate; int hours;};



main()
{
int i;
/*Define an 1d array of struct*/
struct PayRecord employee[6];

/*loop for user to initialise the members of array*/
for(i=0; i<6; ++i)
{
printf("Please enter the following\n number, name, rate and hours
for an employee\n ");
scanf("%i %char %f %i", &employee.number, &employee.name,
&employee.rate, &employee.hours);
printf("%d %s %f %d\n ", employee.number, employee.name,
employee.rate, employee.hours);
}

return 0;
}
 
R

rsk

alex said:
I've written this structure and allowed the user to initialise the
members in an array of this structure. I also want the program to
print to the screen whats in this array after its been initialised.
I've written the program below, but it doesn't do the last thing
properly...any ideas?

/*A program to allow user to populate an array structure*/
#include <stdio.h>

/*Declare a four member structure */
struct PayRecord {int number; char name[20]; float rate; int hours;};

main()
{
int i;
/*Define an 1d array of struct*/
struct PayRecord employee[6];

/*loop for user to initialise the members of array*/
for(i=0; i<6; ++i)
{
printf("Please enter the following\n number, name, rate and hours
for an employee\n ");
scanf("%i %char %f %i", &employee.number, &employee.name,
&employee.rate, &employee.hours);


I think "%char" could be %s .
printf("%d %s %f %d\n ", employee.number, employee.name,
employee.rate, employee.hours);
}

return 0;
}
 
E

Eric

In scanf, reading a string (%s) eats up all the remaining characters.
So that you have to re-order the input fields to put the name at the end:

/*A program to allow user to populate an array structure*/
#include <stdio.h>

/*Declare a four member structure */
struct PayRecord
{
int number;
char name[20];
float rate;
int hours;
};

int main(void)
{
int i;
struct PayRecord employee[6];

/*loop for user to initialise the members of array*/
for (i = 0; i < 6; ++i)
{
printf("Please enter the following\nnumber, rate and hours for an
employee, name\n");
scanf("%d,%f,%d,%s", &employee.number, &employee.rate,
&employee.hours, &employee.name); // name
printf("%d %s %f %d\n ", employee.number, employee.name,
employee.rate, employee.hours);
}

return (0);
}
 
M

Mark A. Odell

(e-mail address removed) (alex) wrote in

I've written this structure and allowed the user to initialise the
members in an array of this structure. I also want the program to
print to the screen whats in this array after its been initialised.
I've written the program below, but it doesn't do the last thing
properly...any ideas?

/*A program to allow user to populate an array structure*/
#include <stdio.h>

/*Declare a four member structure */
struct PayRecord {int number; char name[20]; float rate; int hours;};



main()
{
int i;
/*Define an 1d array of struct*/
struct PayRecord employee[6];

/*loop for user to initialise the members of array*/
for(i=0; i<6; ++i)
{
printf("Please enter the following\n number, name, rate and
hours
for an employee\n ");
scanf("%i %char %f %i", &employee.number,
&employee.name,
&employee.rate, &employee.hours);
printf("%d %s %f %d\n ", employee.number, employee.name,
employee.rate, employee.hours);
}

return 0;
}


Re-formatted and fixed (I think):

/* A program to allow user to populate an array structure
*/
#include <stdio.h>

/* Declare a four member structure <-- Useless comment
*/
struct PayRecord
{
int number;
char name[20];
float rate;
int hours;
};

#define NUM_OF(x) (sizeof (x) / sizeof (*(x)))

int main(void)
{
int employee;
/*Define an 1d array of struct <-- Useless comment */
struct PayRecord empList[6];

/* Loop for user to initialise the members of array (endpoint will
** now track actual size of empList
*/
for (employee = 0; employee < NUM_OF(empList); ++employee)
{
printf("Please enter the following\n"
"number, name, rate and hours for an employee\n ");

/* Don't use %i, and %char doesn't exist. %c does but you want
** a string which is %s. Get user input.
*/
scanf("%d %s %f %d",
&empList[employee].number,
&empList[employee].name,
&empList[employee].rate,
&empList[employee].hours);

/* Allow user to exit with a negative number if he doesn't
** want to enter all employee slots
*/
if (empList[employee].number < 0) break;

/* Print user input back out.
*/
printf("%d %s %f %d\n ",
empList[employee].number,
empList[employee].name,
empList[employee].rate,
empList[employee].hours);
}

return 0;
}
 
A

Al Bowers

alex said:
I've written this structure and allowed the user to initialise the
members in an array of this structure. I also want the program to
print to the screen whats in this array after its been initialised.
I've written the program below, but it doesn't do the last thing
properly...any ideas?

What is the 'last thing'?
Below are some suggestions.
/*A program to allow user to populate an array structure*/
#include <stdio.h>

/*Declare a four member structure */
struct PayRecord {int number; char name[20]; float rate; int hours;};



main()

int main(void)
{
int i;
/*Define an 1d array of struct*/
struct PayRecord employee[6];

/*loop for user to initialise the members of array*/
for(i=0; i<6; ++i)
{
printf("Please enter the following\n number, name, rate and hours
for an employee\n ");
scanf("%i %char %f %i", &employee.number, &employee.name,


I do not thing you want %char. You can replace it with the %s specifier.
This would require that the name the user input not have whitespace.
For example an input of 'George' would work but 'George Washington"
would fail. Using scanf to get the user input requires the user to
adhere to the strict format. At the least you should research scanf and
its return value. Test the return value of scanf to give you some idea
of the success or failure of the function. If %s is not appropriate
for your requirements you might try the scanset specifier.
&employee.rate, &employee.hours);
printf("%d %s %f %d\n ", employee.number, employee.name,
employee.rate, employee.hours);
}

return 0;
}
 
C

CBFalconer

alex said:
I've written this structure and allowed the user to initialise the
members in an array of this structure. I also want the program to
print to the screen whats in this array after its been initialised.
I've written the program below, but it doesn't do the last thing
properly...any ideas?

.... snip code ...

The following (incomplete) program may help. It separates
discrete actions, such as getting a record, storing a record,
printing a record. To use it you need ggets() - see the
annotation comment. You can now practice getting a number out of
a string, and finding out where it ends, both for floats and
integers. To do this I advise writing further functions getint
and getfloat. Make sure they detect errors. The standard
functions strtol and strtod will do all the hard work. The place
to insert those functions is ahead of getrecord. Note how #define
is used to set critical constants in one place.

If you use gcc the compilation and linking is done by:

gcc -c ldstruct.c
gcc -o ldstruct ldstruct.o ggets.o

assuming you have already compiled ggets (see comments) with:

gcc -c ggets.c

The main thing I am trying to put across is the breakup of
programs into small, easily definable, simple subroutines. You
should be able to see the whole routine in one editor screenful,
and it should be simple enough so you can easily see it does the
job. If it gets too long it is time to split it up into further
simple subroutines. In C, a subroutine is implemented by writing
a function. Using descriptive names helps readability.

------- File ldstruct.c --------
/* demo program to load, verify, print an array of struct */
/* Note how functions do one simple thing each */
/* Public domain, by C.B. Falconer */

#include <stdio.h>
#include <string.h> /* strcpy strlen */
#include <stdlib.h> /* free */

/* this, and ggets.c, is available at:
<http://cbfalconer.home.att.net/download/ggets.zip> */
#include "ggets.h" /* simplifies i/o. Don't use gets() */

#define MAXNAME 20

/* Declare a four member structure type */
struct PayRecord {
int number;
char name[MAXNAME];
float rate;
int hours;
};

#define MAXSTRUCTS 10

/* persistent data */
static int usedslots; /* zeroed */
static struct PayRecord payroll[MAXSTRUCTS];

/* ---------------- */

/* Incomplete. Use strtol and strtod, then check length of
remaining line < MAXNAME after removing leading blanks.
return 1 for any failure. */
/* returns 0 for success, 1 for error, EOF for end of input */
static int getrecord(struct PayRecord *record)
{
char *ln, *nm;
int err;

if (err = ggets(&ln))
return EOF; /* no data or no memory */
else {
/* parse the fields into *record */
nm = ln;
record->number = 0; /* dummies for testing */
record->rate = 0.0;
record->hours = 40;

/* remove leading blanks */
while (' ' == *nm) nm++;

/* remainder of string is the name */
if (MAXNAME <= strlen(nm)) err = 1;
else {
strcpy(record->name, nm);
err = 0;
}

free(ln);
return err;
};
} /* getrecord */

/* ---------------- */

/* stuff record into the indexth slot of payroll */
static void saverecord(struct PayRecord record, int index)
{
payroll[index] = record;
} /* saverecord */

/* ---------------- */

/* dump a readable version of the record to f */
static void putrecord(struct PayRecord *record, FILE *f)
{
fprintf(f, "%d, %s, %f, %d\n",
record->number,
record->name,
record->rate,
record->hours);
} /* putrecord */

/* ---------------- */

/* The auxiliary functions getrecord, saverecord, and putrecord
do all the nitty gritty. main simply organizes them */
int main(void)
{
struct PayRecord current;
int i;
int inputerror;

puts("Enter lines in the following format. ^Z or ^D ends\n"
"(with at least one blank separating fields)\n"
" number rate hours name\n"
"EX: 2 12.50 40 Joe Q. Shmoo");
/* This organization, with a string entry last, */
/* makes parsing the fields fairly easy. */
/* puts() appends a final /n */

/* load until max or EOF signalled */
/* We test for max first to avoid overflows */
while ((usedslots < MAXSTRUCTS) &&
(EOF != (inputerror = getrecord(&current)))) {
if (inputerror) fprintf(stderr, "Input error\n");
else {
saverecord(current, usedslots);
usedslots++;
}
}
printf("%d items entered.\n\n", usedslots);

/* dump the lot */
/* since we use stdout we don't need to open/close files */
if (0 == usedslots) printf("Storage empty\n");
else
for (i = 0; i < usedslots; i++) {
putrecord(&payroll, stdout);
}

/* signal success, no file close needed */
return 0;
} /* main ldstruc */
 
N

Neil Ferguson

alex said:
I've written this structure and allowed the user to initialise the
members in an array of this structure. I also want the program to
print to the screen whats in this array after its been initialised.
I've written the program below, but it doesn't do the last thing
properly...any ideas?

/*A program to allow user to populate an array structure*/
#include <stdio.h>

/*Declare a four member structure */
struct PayRecord {int number; char name[20]; float rate; int hours;};



main()
{
int i;
/*Define an 1d array of struct*/
struct PayRecord employee[6];

/*loop for user to initialise the members of array*/
for(i=0; i<6; ++i)
{
printf("Please enter the following\n number, name, rate and hours
for an employee\n ");
scanf("%i %char %f %i", &employee.number, &employee.name,
&employee.rate, &employee.hours);
printf("%d %s %f %d\n ", employee.number, employee.name,
employee.rate, employee.hours);
}

return 0;
}


A tangential point: the ampersand on employee.name is superfluous. And
tangential to that, the following code (disregarding the dangerous
vulnerability to overflow) fails. The compiler figures you really mean the
indirection of the pointer, but you were just kidding about the array
indirection.

#include <stdio.h>

int main(int argc, char**argv)
{
char buf[80], buf2[80], *pbuf2 = buf2;

printf("enter 2 words\n");
fscanf(stdin, "%s %s", &buf, &pbuf2);
printf("you entered %s %s\n", buf, pbuf2);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top