Database in C and thoughts about input.
I am reading from King's book on Page 394-395
There is a C database program which it implemented with arrays of structures
iventory.c
Code:
#include<stdio.h>
#include "readline.h"
#define NAME_LEN 25
#define MAX_PARTS 100
struct part {
int number;
char name[NAME_LEN+1];
int on_hand;
} inventory[MAX_PARTS];
int num_parts = 0 ;
int find_part(int number);
void insert(void);
void search(void);
void update(void);
void print(void);
int main(void)
{
char code;
for(;;) {
printf("Enter operation code: ");
scanf(" %c" , &code);
while( getchar() !='\n')
;
switch(code) {
case 'i': insert();
break;
case 's': search();
break;
case 'u': update();
break;
case 'p': print();
break;
case 'q': return 0;
default: printf("Illegal code\n");
}
printf("\n");
}
}
int find_part(int number)
{
int i;
for(i=0; i<num_parts; i++)
if( inventory[i].number == number)
return i;
return -1;
}
void insert(void)
{
int part_number;
if (num_parts == MAX_PARTS) {
printf("Database is full; Can't add more parts. \n");
return;
}
printf("Enter part number: ");
scanf("%d" , &part_number);
if( find_part(part_number) >=0 ) {
printf("Part already exists. \n");
return;
}
inventory[num_parts].number = part_number;
printf("Enter part name: ");
read_line(inventory[num_parts].name , NAME_LEN);
puts(inventory[num_parts].name);
printf("Enter quantity on hand: ");
scanf("%d" , &inventory[num_parts].on_hand);
num_parts++;
}
void search(void)
{
int i , number;
printf("Enter part number: ");
scanf("%d" , &number);
i = find_part(number);
if( i >=0 ){
printf(" Part name : %s \n " , inventory[i].name);
printf(" Quantity on hand: %d \n" , inventory[i].on_hand);
} else
printf("Part not found. \n ");
}
void update(void)
{
int i , number , change;
printf(" Enter part number: ");
scanf("%d" , &number);
i= find_part(number);
if(i>=0) {
printf(" Enter change in quantity on hand: ");
scanf("%d" , &change);
inventory[i].on_hand += change;
} else
printf("Part not found");
}
void print(void)
{
int i;
printf(" Part Number Part Name "
"Quantity on Hand\n");
for(i=0; i < num_parts; i++)
printf("%7d %-25s%11d\n" , inventory[i].number , inventory[i].name , inventory[i].on_hand);
}
readline.h
Code:
#ifndef READLINE_H
#define READLINE_H
int read_line(char str[] , int n);
#endif
and
readline.c
Code:
#include<ctype.h>
#include<stdio.h>
#include "readline.h"
int read_line( char str[] , int n)
{
int ch , i=0;
while( isspace( ch = getchar() ))
;
while( ch!= '\n' && ch != EOF) {
if( i < n)
str[i++] = ch;
ch = getchar();
}
str[i] ='\0';
return i;
}
I can understood this program I think is very easy but I have some queries about the read_line function.... King writes that we must use this edition of read_line instead of this :
Code:
/*#include<stdio.h>
#include "readline.h"
int read_char(void)
{
int ch = getchar();
if( ch == '\n' || ch == '\t' )
return ' ';
return ch;
}
void read_line(char *word , int len)
{
int ch , pos = 0;
while( (ch = read_char() ) == ' ')
;
while( ch != ' ' && ch != EOF ) {
if(pos < len)
word[pos++] = ch;
ch = read_char();
}
word[pos] = '\0'; // Το κάνει string.
}*/
Why? Because if we have an input like
Code:
Enter part number: 528
Enter part name: Disk Drive
If we try to read the part name using the previous read_line we will encounter the '\n' character immediately and stop reading.This problem is common when numerical input is followed by character input.Our solution will be to write a version of read_line that skips white space characters before it begins storing characters.Not only will this solve the new-line problem but it also allows us to avoid storing any blanks that precede the part name
I didn't see any problem with '\n' since the previous edition of read_line converts it to space character ... What is stopped? I only see a problem when Disk Drive string encountered the space character (between Disk and Drive). I can't understand why King explains it with this way.... I have traced the function read_line ...
English is not my native language and sometimes I face difficulties with the English books.