TIP: Click on subject to list as thread! ANSI
echo: locuser
to: All
from: Dieter Mirbach
date: 1994-05-30 23:56:56
subject: C Structures

Hello all,

My first idiot question abiout C !!

My final assignment involves reading/writing & appending records 
to a predfined structure. And modify individual records with in 
the structure. 

I've come across a small snag. In all my lecture notes, and the 
big bastard of a C Primer book, I cant see anyway to either check 
for errors as I enter individual elemnts of a structure, or how 
to modify individual elements. For instance, if I defined a 
quantity as an integer, I can still enter alpha. Obvuiusly, the 
resultant structure defaults to zero, but I want to trap the 
error prior to the value being incoporate into the structure. 
Can this be done ??

First pass of code follows:


   #include 
   #include 
   #include 
   #include 

   #define MAXNAME 20
   #define MAX_HEADING_SIZE 80

   struct record                        // Prepare a structure of the record
	  {                             // type ( template ).
	     char   name[MAXNAME];
	     int    quantity;
	     int    stock;
	     float  cost;
	  } ;

   struct rec                           // This is for the read/modify
	  {                             //
	     char   n[MAXNAME];
	     int    q;
	     int    s;
	     float  c;
	  } ;

   char *filename = "a:assembly.dat";		// This will have to be
changed for user input
   char *wrt_heading = "    COMPONENT NAME   QTY/ASSY        STOCK QTY
  COMP COST";
   char rd_heading[MAX_HEADING_SIZE];

   FILE  *fp;

   struct record item;     // Declare item as a structure of the record type.
   struct rec    it;       //

   /*  This is the first part. It writes data to  a:\assembly.dat. */

   void main(void)
   {
      fp = fopen( filename, "w");
      printf("\033[2J");

      puts(" Key in records now. Press the Enter key after each field.");
      puts(" After the last record, key in  !  for the NAME field to");
      puts(" terminate  data entry.\n");


      puts( wrt_heading);
      fputs( wrt_heading, fp);
      puts("");

      do
      {
	 printf("   ");
	 scanf("%s", item.name);
	 if( item.name[0] != '!')
	 {
	   printf("\033[1A\033[23C");
           scanf("%d", &item.quantity);         // Error checking
                                                // here somewhere 
                                                // ??
	   printf("\033[1A\033[37C");
	   scanf("%d", &item.stock);


	   printf("\033[1A\033[51C");
	   scanf("%f", &item.cost);

	   fwrite( &item, sizeof(item), 1, fp);
	   sleep(1);
	 }

      }
      while( item.name[0] != '!');

      fclose(fp);
      sleep(3);

================================

There are a few errors, such as incorrect assignment of structure 
fields, but this was ripped from one of my course "examples", and 
I'm only partly through changing things around.

When I'm a little more through, I'll post the rest regarding 
modifying individual fields & appending additional records.

Ta in advance,

Dieter



... WARNING!  Removal of this tagline prohibited by law!
--- Blue Wave/QWK v2.10

--- PQWK200
* Origin: Mirbachs MadPoint (3:711/934.8)
SEEN-BY: 711/934
@PATH: 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.