1. ## Data Structure issues

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. */
{
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");

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(;;)
{
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?

2. Since your list is not an automatic variable, it is initialized (once and only once) with zeros at the start of your program since no constant initial values are supplied.

Yes, it is possible. You could either keep track of how many accounts the user has inputted information for while your program is gathering input, or you could check to see which structures are still filled with zeros while you are performing the output so you can omit them.

One suggestion: You might store your "ssn" as a character string or BCDs instead of an array of integers. If you intended for each integer in the array to represent a single digit, you should be advised that it is an inefficient use of memory.

3. thnx kitty..
as for the SSN the tasks asks to be an array of integers...i dont know if i could store it otherwise,,
What should i do about the double appearance of the menu list?
a valid ssn is 045678990

4. You have a trailing newline in the input buffer from entering the amount. Read this: Cprogramming.com FAQ > Flush the input buffer.

5. Originally Posted by anduril462
You have a trailing newline in the input buffer from entering the amount. Read this: Cprogramming.com FAQ > Flush the input buffer.
so is the clearscreen commands not right to solve this issue?
where should i put the flushing routine in my code to solve the problem?

6. I don't see any clearscreen commands, but you need to clear the input buffer at the end of your enter() function. You have to hit enter for it to read in the amount of the account, but that enter isn't consumed by the scanf call since '\n' isn't part of a valid number. Thus, after entering account info, you print your menu, call gets, it reads in the newline leftover from entering account info, and does nothing since it's not 1, 2, 3 or 4. Then, it reprints the menu and prompts you for input.