Thread: Data Structure issues

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    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. */
    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?

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    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.
    Last edited by kittykitty; 02-16-2011 at 03:17 PM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    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
    Last edited by junglist; 02-16-2011 at 03:47 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have a trailing newline in the input buffer from entering the amount. Read this: Cprogramming.com FAQ > Flush the input buffer.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    Quote Originally Posted by anduril462 View Post
    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. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. static data structure in a library
    By melight in forum C Programming
    Replies: 6
    Last Post: 01-10-2009, 11:12 AM
  3. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM