Hi there everyone,
Hoping eveyone is alright!
I just got built this simple database program. Things were quite fine not until
I reached a point where i couldn't read the records in my little database. I
have tried to figure out where I could have gone wrong but I don't seem to get
anywhere. Can you guys help me out please.
I was also trying to test input by a user for different criteria i.e, whether
input is a digit, alpha, within a specific range, but came short on realising
that i couldn't utilise the if construct(remember that an when one statement
evaluates to true in an if construct, it will terminate) to effectively display
different messages related to what has been captured from a user. Can someone
take me through this too so I can test user input effectively. Many thanks in advance
Here is a bit of the code :

1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <ctype.h>
4. #include <string.h>
5. #include <conio.h>
6. 
7. void mainmenu(void);
8. 
9.  struct customer_rec
10. {
11. char f_name[12];
12. char s_name[12];
13. int id;
14. char dob[10];
15. char address[20];
16. char e_address[15];
17. char gender[1];
18. char tel_num[9];
19. 
20. }customer[1000];
21. 
22.  FILE *cus_data,*cus_data2;
23.  char telnum2[9],again1,again2,retain;
24.  int check,traverse;
25.  long int datsize, track=0;
26.  int x,f,z,t,j;
27.  int choice;
28.   void main(void)
29. {
30. 
31.    char access1[7]="cram";
32.    char access2[7];
33.    int i=0;
34. 
35.      do
36.      {
37. 
38.        printf("\n");
39.        printf("\n\t\t^^^^^^^^^^EDDIE'S VIDEO LIBRARY^^^^^^^^^^\n");
40.        printf("\n\t^^^^Hi,please Welcome to Eddie,s customer management system!^^^^\n");
41.          printf("\n\t\t\tEnter password to proceed: ");
42.          gets(access2);
43.           if(strcmp(access2,access1) == 0)
44.         {
45. 
46.           cus_data = fopen ( "\\CUSTO.DAT", "rb+" );
47.           if ( cus_data == NULL )
48.          {
49.            cus_data = fopen ( "\\CUSTO.DAT","wb+" );
50.            if ( cus_data == NULL )
51.            {
52.             puts ("Cannot open file!");
53.             exit(0);
54.            }
55.          }
56.             datsize = sizeof(customer[1000]);
57. 
58.         }
59.           else
60.         {
61.              printf("\n\t\tInvalid password ! Pls Try Again:   ");
62.          getch();
63.           clrscr();
64.           i++ ;
65.           if(i==3)
66.           exit(0);
67.         }
68. 
69.      }while(strcmp(access2,access1) !=0&&i<3);
70. 
71. 
72.   while(1)
73.    {
74.      clrscr();
75.      mainmenu();
76. 
77.      switch(choice)
78.   {
79.    case 1:
80.            fseek(cus_data,0,SEEK_END);
81.            again1='Y';
82.             while(again1=='Y'||again1=='y')
83.               {
84.                 do
85.                  {
86.                   gotoxy(5,2);
87.                   clreol();
88.                   printf("^^^^^ADD CUSTOMER RECORD^^^^^\n");
89.                   printf("\n");
90.                   gotoxy(5,5);
91.                   printf("Please, enter first name:");
92.                   scanf("%s",customer[track].f_name);
93.                    if(strlen(customer[track].f_name)>12||strlen(customer[track].f_name)<1)
94.                     {
95.                      gotoxy(23,20);
96.                      printf("Name must be between[1-12] characters!\n");
97.                      getch();
98.                      gotoxy(23,20);
99.                      clreol();
100.                     }
101. 
102.                  }while(strlen(customer[track].f_name)>12||strlen(customer[track].f_name)<1);
103. 
104.                  do
105.                  {
106.                       gotoxy(5,8);
107.                       clreol();
108.                       printf("Enter sur name:");
109.                       scanf("%s",customer[track].s_name);
110.                       clreol();
111.                       if(strlen(customer[track].s_name)>12||strlen(customer[track].s_name)<1)
112.                      {
113.                        gotoxy(23,20);
114.                        printf("Name can only be in the [1-12] range!");
115.                        getch();
116.                        gotoxy(23,20);
117.                        clreol();
118.                      }
119. 
120. 
121.                  }while(strlen(customer[track].s_name)<1||strlen(customer[track].s_name)>12);
122. 
123. 
124.                  do
125.                  {
126.                   gotoxy(5,10);
127.                   clreol();
128.                   printf("Enter customer's gender[M/F]:");
129.                   scanf("%s",customer[track].gender);
130.                     if(strlen(customer[track].gender)==0 ||strlen(customer[track].gender)>1)
131.                      {
132.                       gotoxy(23,20);
133.                       printf("Please enter gender as shown!\n");
134.                       getch();
135.                       gotoxy(23,20);
136.                       clreol();
137.                      }
138.                     }while(strlen(customer[track].gender)==0 ||strlen(customer[track].gender)>1);
139.                  do
140.                  {
141.                   gotoxy(5,13);
142.                   clreol();
143.                   printf("Enter ID No:");
144.                   scanf("%4i",&customer[track].id);
145.                     if((customer[track].id<1000) ||(customer[track].id>5000))
146.                      {
147.                       gotoxy(23,20);
148.                       printf("Please enter valid ID(1000-5000)!\n");
149.                       getch();
150.                       gotoxy(23,20);
151.                       clreol();
152.                      }
153.                     }while((customer[track].id <1000) ||(customer[track].id>5000));
154. 
155. 
156.                 do
157.                  {
158.                   gotoxy(5,16);
159.                   clreol();
160.                    printf("Enter date of birth [dd/mm/yy]:");
161.                    scanf("%s",customer[track].dob);
162.                     if(strlen(customer[track].dob)<1||strlen(customer[track].dob)>10)
163.                      {
164.                       printf("Please enter valid date!");
165.                       getch();
166.                       gotoxy(23,20);
167.                       clreol();
168.                      }
169. 
170.                  }while(strlen(customer[track].dob)<1||strlen(customer[track].dob)>10);
171. 
172.                  do
173.                   {
174.                    gotoxy(5,19);
175.                    clreol();
176.                     printf("Enter Address:");
177.                     scanf("%s",customer[track].address);
178.                     if(strlen(customer[track].address)<1||strlen(customer[track].address)>20)
179.                       {
180.                        gotoxy(23,20);
181.                        printf("Address can only be in the [1-20] range!");
182.                        getch();
183.                        gotoxy(23,20);
184.                        clreol();
185.                       }
186.                  }while(strlen(customer[track].address)<1||strlen(customer[track].address)>20);
187. 
188. 
189.                do
190.                  {
191.                   gotoxy(5,22);
192.                   clreol();
193.                   printf("Enter e-mail address:");
194.                    scanf("%s",customer[track].e_address);
195.                    if(strlen(customer[track].e_address)<1||strlen(customer[track].e_address)>15)
196.                      {
197.                        gotoxy(23,20);
198.                        printf("Enter a valid e-mail address!");
199.                        getch();
200.                        gotoxy(23,20);
201.                        clreol();
202.                      }
203. 
204.                  }while(strlen(customer[track].e_address)<1||strlen(customer[track].e_address)>15);
205.                do
206.                 {
207.                   gotoxy(5,25);
208.                   clreol();
209.                   printf("Enter telephone number:");
210.                   scanf("%s",customer[track].tel_num);
211.                    if(strlen(customer[track].tel_num)<1||strlen(customer[track].tel_num)>15)
212.                      {
213.                        gotoxy(23,20);
214.                        printf("Enter a valid telephone number!\n");
215.                        getch();
216.                        gotoxy(23,20);
217.                        clreol();
218.                      }
219. 
220. 
221.                  }while(strlen(customer[track].tel_num)<7||strlen(customer[track].tel_num)>12);
222.                    fwrite ( &customer[1000], datsize, 1, cus_data );
223.                    printf("Would you like to add another record[Y/N]:?");
224.                    fflush(stdin);
225.                    again1=getche();
226.                    clrscr();
227.           }
228.                  break;
229.    case 2:
230. 
231.            rewind(cus_data);
232.            while(fread(&customer[1000],datsize,1,cus_data)==1)
233.            {
234.               gotoxy(2,2);
235.               printf("^^^^^^^SHOW CUSTOMER RECORDS^^^^^^^^");
236.               printf("\n");
237.               gotoxy(2,5);
238.               printf("First name: %s",customer[track].f_name);
239.               gotoxy(2,8);
240.               printf("Sur name: %s",customer[track].s_name);
241.               gotoxy(2,11);
242.               printf("Gender: %s",customer[track].gender);
243.               gotoxy(2,13);
244.               printf("customer ID: %i",customer[track].id);
245.               gotoxy(2,16);
246.               printf("Date of birth: %s",customer[track].dob);
247.               gotoxy(2,19);
248.               printf("Address: %s",customer[track].address);
249.               gotoxy(2,21);
250.               printf("E-mail: %s",customer[track].e_address);
251.               gotoxy(2,23);
252.               printf("Tel: %s\n",customer[track].tel_num);
253.               gotoxy(10,27);
254.               printf("To see more records, press enter");
255.               track=getche();
256.               track++;
257.             }
258.                 break;

At line 232 above, you're always reading into the 1000th position of the array, which doesn't even exist (valid indices are from 0 to 999). I think you meant &customer[track].

Then at line 255, you're trashing your value of track by assigning to it the value of getche(). Use a temporary variable there or just ignore the return value completely.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.