Like Tree1Likes
  • 1 Post By jimblumberg

Input 3 accounts in 1 person using structures?

This is a discussion on Input 3 accounts in 1 person using structures? within the C Programming forums, part of the General Programming Boards category; I'm having problems with inputting accounts for each person (customer). Help? Haha. ^^ For each person, he may have up ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    6

    Input 3 accounts in 1 person using structures?

    I'm having problems with inputting accounts for each person (customer). Help? Haha. ^^

    For each person, he may have up to three accounts. Inside the account, he has to input his account number and balance. Then when I view it, it has to display the number of accounts the person has.

    You might be wondering why it has to input his account number and balance when it's just the number of accounts it displays. Haha! But this isn't really the whole project. The options are from A-I. As you can see, I just put A, B, H, and I.

    Here's my code.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdbool.h>
    
    #define SIZE 10
    #define MAX 3
    
    
    typedef struct
    {
          char cID[7];
          char first[30];
          char last[20];
          char midi; 
          char address[30];
          char city[20]; 
    }CUSINF;
    
    typedef struct
    {
          char account[6];
          int balance;
    }CUSACC;
    
    
    void getCusInf(CUSINF x[], int count);
    void getCusAcc(CUSACC y[], CUSINF x[], int count);
    void viewCus(CUSINF x[], CUSACC y[], int count);
    
    
    int main(void)
    {
        CUSINF x[SIZE];
        CUSACC y[MAX];
        char choice;
        int count=0, res;
        char id[7];
        
        do{
           system("cls");  
           printf("\n[a] Add Customer Information");
           printf("\n[b] Add Customer Account");
           printf("\n[h] View All");
           printf("\n[i] Quit");
           printf("\n\nEnter Choice: ");
           scanf(" %c", &choice);
           
           switch(choice)
           {
                case 'a': 
                case 'A': system("cls");
                          getCusInf(x, count);
                          count=count+1;
                          break;
                case 'b':
                case 'B': getCusAcc(y, x, count);
                          count=count+1;
                          break;
                case 'h':
                case 'H': system("cls");
                          viewCus(x, y, count);
                          count=count+1;
                          break;
                case 'i': 
                case 'I': printf("\n\nThank you for using this program!");
                          getch();
                          exit(0);
                          
                default: printf("\n\nInvalid Input! Try Again.\n\n");
                         getch();
                         break;
           }
           
        }while(choice!='i' || choice!='I');
        
        getch();
    }
    
    
    void getCusInf(CUSINF x[], int count)
    {
         int i, res;
         char id[7];
         
         if(count<SIZE)
         {
              fflush(stdin);
              printf("\nEnter Customer ID: ");
              gets(id);
    
              for(i=0;i<count;i++)
              {
                   res=strcmp(x[i].cID, id);
                   if(res==0)
                   {
                             printf("ID already taken. Enter different ID.");
                   }
              }
              if(res!=0)
              {
                        strcpy(x[i].cID, id);
                        printf("Enter First Name: ");
                        gets(x[count].first);
                        printf("Enter Last Name: ");
                        gets(x[count].last);
                        printf("Enter Middle Initial: ");
                        scanf(" %c", &x[count].midi);
                        fflush(stdin);
                        printf("Enter Address: ");
                         gets(x[count].address);
                         printf("Enter City: ");
                         gets(x[count].city);
              }     
         }
         else
         printf("\nSorry you cannot add information anymore.");
         
         getch();
    }
    
    
    void getCusAcc(CUSACC y[], CUSINF x[], int count)
    {
         int i, res;
         char id[7];
         fflush(stdin);
         
         printf("\n\nEnter ID Number [xx-xxx]: ");
         gets(id);
            
         if(count<MAX)
         {        
            for(i=0;i<count;i++)
            {
                 res=strcmp(x[i].cID, id);
                 if(res==0)
                 {
                           printf("Enter Account Number: ");
                           gets(y[count].account);
                           printf("Enter Balance: ");
                           scanf("%d", &y[count].balance);
                           
                 }                     
            } 
            if(res!=0)
                         printf("\n\nNO RECORDS FOUND!");  
          }
          else
                 printf("\nSorry you cannot add information anymore.");
         
         getch();
         system("cls");
    }
    
    
    void viewCus(CUSINF x[], CUSACC y[], int count)
    {
         int i=0;
         fflush(stdin);
         
         printf("Costumer Information\n\n");
         for(i=0;i<count;i++)
         {
                 fflush(stdin);       
                 printf("Customer's ID: %s\n", x[i].cID);
                 printf("Customer's Name: %s %c. %s\n", x[i].first, x[i].midi, x[i].last);
                 printf("Customer's Address: %s\n", x[i].address);
                 printf("Customer's City: %s\n", x[i].city);
                 //PRINT Number of Accounts
                 printf("\n\n\n");
         }
         
         getch();
    }
    I'm also having problems with viewing all the info. Like, if it's the first time I view it, it's okay. But when I view it the second time around, third, etc., it displays trashes. I can't really explain it. :/

    I'm trying to solve it, too. Hopefully you guys could help me, too. THANK YOU SO MUCH IN ADVANCE!! ^^

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    There are a couple of things wrong with the following snippet:
    Code:
              fflush(stdin);
              printf("\nEnter Customer ID: ");
              gets(id);
    First fflush() is not defined in the standard to work with input streams (stdin). Next you should never use gets, this function is very unsafe because it is possible to enter more characters than the size of the buffer (id). You should replace all your gets() with fgets().

    You are also missing at least one include file. Check the documentation for the exit() function. Your compiler maybe including this file but you should never rely on some external file including a file for you.

    Since your program is relying on undefined behavior (fflush(stdin)) that my compiler doesn't support I can't run your program as written, so you will need to describe your problems with much more detail.


    Jim

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Thanks a lot, Jim!

    I'm using Dev C++ as my compiler, btw. In viewing (option H), if I view it the first time, it displays normally. But when I view it the second time around, this happens:
    Name:  prog.png
Views: 200
Size:  12.6 KB

    Trash output. :I

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    Your trash is probably being caused by this snippet.
    Code:
                case 'H': system("cls");
                          viewCus(x, y, count);
                          count=count+1;
                          break;
    What is the value of count, the first time through? And the second? How many records do you have?

    Jim
    ZuK likes this.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Haha! The problem was exactly that. :3 I got rid of it and it works perfectly fine now! Thanks so much!

    Now I just need to work on having a maximum of three accounts for one person.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,068
    I would suggest you practice using nested structures (if you understand structures, using a structure within a structure shouldn't take too long to get used to). This way, you can have all of your customer information in a single structure, not broken up into two. And achieving your goal of three accounts maximum for one person would also be easy to implement with the nested structure configuration.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Yes, that was what I wanted to do, having a structception, but I never really haven't had the time to fully learn it since this project was given the day after our teacher discussed structures. =___=

    I think I've got this. I guess I just needed to have three account number and balance in my CUSACC struct. Hopefully it will do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input program with structures
    By usernameisvalid in forum C Programming
    Replies: 22
    Last Post: 07-20-2011, 04:41 PM
  2. input from text using structures
    By ehj3000 in forum C Programming
    Replies: 12
    Last Post: 09-16-2005, 10:30 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  4. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  5. Accounts
    By gnu-ehacks in forum Linux Programming
    Replies: 3
    Last Post: 12-03-2001, 08:00 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21