I've made the following data base/list...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#define MAX 10
struct account
{
int ssn[9];
double amount;
} list[MAX];
int validation(int b[],int n)
{
int f,i,s=0;
for(i=0; i<n-1; i++)
s+=ldexp(b[i],n-1-i);
if(s%11==b[n-1])
f=1;
else
f=0;
return f;
}
/* Initialize the list. */
void init_list(void)
{
struct account*pointer=list;
register int t;
int j;
for(t=0; t<MAX; ++t)
for (j=0; j<9; j++)
{
pointer[t].ssn[j]=0;
pointer[t].amount=0;
}
}
/* Get a menu selection. */
int menu_select(void)
{
char s[80];
int c;
do
{
printf("\n1. Input data\n");
printf("2. List the file\n");
printf("3Sum amounts & accounts\n");
printf("4. Quit\n");
printf("\n\nEnter your choice: ");
gets(s);
c = atoi(s);
}
while(c<0 || c>4);
return c;
}
/* Input data into the list. */
void enter(void)
{
int j,flag,p[9],slot;
double total;
slot = find_free();
if(slot==-1)
{
printf("\nList Full");
return;
}
do
{
for (j=0; j<9; ++j)
{
printf("give to %d_o digit of SSN:\n",j+1);
scanf("%d",&p[j]);
}
while(p[j]>=10||p[j]<0)
{
printf("Digit must be single possitive integer!!Try again:\n");
scanf("%d",&p[j]);
}
flag=validation(p,9);
if(flag==0)
printf("\n ssn is invalid\n\n-----------------------------------------\n");
}
while(flag==0);
printf("ssn is valid");
for (j=0; j<9; ++j)
{
list[slot].ssn[j]=p[j];
}
printf("\nGive the amount of the account:");
scanf("%lf",&total);
while(total<0)
{
printf("amount must be real positive no.!!Give again:\n");
scanf("%lf",&total);
}
list[slot].amount=total;
}
/* Find an unused structure. */
int find_free(void)
{
register int t;
for(t=0; t<MAX; ++t)
if(*list[t].ssn==0&&list[t].amount==0)break;
if(t==MAX)
{
return -1;
}
/* no slots free */
return t;
}
/* Display the list on the screen. */
void list(void)
{
struct account*pointer=&list[0];
int j;
register int k;
for(k=0; k<MAX; k++)
{
printf("SSN:");
for(j=0; j<9; j++)
{
printf("%d",pointer[k].ssn[j]);
}
printf("\nAmount:%lf\n\n",pointer[k].amount);
}
}
list_amounts()
{
int z=0;
int sum=0;
register int s;
struct account*pointer=list;
for(s=0; s<MAX; s++)
{
if (pointer[s].amount!=0)
{
z++;
}
sum=sum+list[s].amount;
}
printf("\The total no of accounts is:%d\n",z);
printf("The amount total is:%d",sum);
}
int main(void)
{
char choice;
init_list(); /* initialize the structure array */
for(;;)
{
choice = menu_select();
switch(choice)
{
case 1:
enter();
break;
case 2:
list();
break;
case 3:
list_amounts();
break;
case 4:
exit(0);
}
}
}
the issues are:
1)when i enter the amount it double prints the selection menu
i made a clrear screen code but i would like to eliminate that issue without it if possible...
2)
Do i have to initialize the list with 0's?
Is it possible if for example i enter lets say 3 accounts and i want to list them the programm to print only those three and not the rest 7(which are 0's)?
In other words to list only the accounts i ve entered?
Any other suggestions would be well accepterd?