Thread: Project Due Need Help

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    Question Project Due Need Help

    Hi I am doing a project using binary files.I've wrote a small bit of code with no compile errors.I'm using Dev-C++ compiler(If you know a better free one please tell me)When I run it the command prompt opens and closes straight away and I don't know why This has happened me before but I need this project done VERY SOON!! can anyone help?!?This is the code I've written.
    It's planned to be a banking system If anyone has Ideas for this I would also like to hear them.Thanks for listening. I did a bit of debugging and came up with this,
    in the load() method here ->


    Code:
       1.
          #include   <stdio.h>
       2.
          #include   <string.h>
       3.
          #include   <stdlib.h>
       4.
           
       5.
           
       6.
          #define MAX 100
       7.
           
       8.
           
       9.
          int menu(void);
      10.
          int display(int i);
      11.
          void customer_search(void);
      12.
          void AccNo_search(void);
      13.
          void enter(void);
      14.
          void save(void);
      15.
          void load(void);
      16.
           
      17.
          struct catalog
      18.
          {
      19.
                 char name[80];
      20.
                 char AccNo[6];
      21.
                 char address[80];
      22.
                 unsigned date;
      23.
                 unsigned char month;
      24.
               
      25.
          }*cat[MAX];
      26.
           
      27.
          struct bank
      28.
          {
      29.
                  char BankName[20];
      30.
                  unsigned LastAccNum;
      31.
                  unsigned sortCode;
      32.
                 
      33.
          }*bank[MAX];
      34.
           
      35.
          int top = 0;
      36.
           
      37.
           
      38.
          int main(void)
      39.
          {
      40.
                        int choice;
      41.
                       
      42.
                        load();
      43.
                       
      44.
                        do
      45.
                          {
      46.
                                   choice = menu();
      47.
                                   
      48.
                                   switch(choice)
      49.
                                         {
      50.
                                                 case 1: enter();
      51.
                                                      break;
      52.
                                                 
      53.
                                                 case 2: customer_search();
      54.
                                                      break;
      55.
                                                     
      56.
                                                 case 3: AccNo_search();
      57.
                                                      break;
      58.
                                                     
      59.
                                                 case 4: save();
      60.
                                         }
      61.
                          }
      62.
                 
      63.
                 while(choice !=5);
      64.
          system("PAUSE");
      65.
                 return 0;
      66.
           
      67.
          }   
      68.
                     
      69.
                     
      70.
                      /* Return a menu selection*/
      71.
                     
      72.
          int menu(void)
      73.
          {
      74.
              int i;
      75.
              char str[80];
      76.
             
      77.
                   printf("\t\n\aEircom Billing System\n\n");             
      78.
                   printf("1->Add Customer\n");
      79.
                   printf("2->Search by Name\n");
      80.
                   printf("3->Search by Account Number\n");
      81.
                   printf("4->Save added Customers\n");
      82.
                   printf("5->Quit\n");     
      83.
                       
      84.
                     do
      85.
                          {
      86.
                                           printf("Make your selection:  ");
      87.
                                           gets(str);
      88.
                                           i = atoi(str);
      89.
                                           printf("\n");                       
      90.
                          } while(i < 1 || i > 5);   
      91.
                         
      92.
                     return i;
      93.
          }                   
      94.
                                               
      95.
              /*Enter customer into file */
      96.
             
      97.
             
      98.
          void enter(void)
      99.
          {
     100.
               int i;
     101.
               char temp[80];
     102.
               
     103.
               
     104.
               for (i = top; i<MAX ; i++)
     105.
                      {
     106.
                                             
     107.
                             cat[i] = malloc(sizeof(struct catalog));
     108.
                             
     109.
                                    if(!cat[i])
     110.
                                           {
     111.
                                                           printf("Out Of Memory\n");
     112.
                                                           return;
     113.
                                           }
     114.
                           
     115.
                           printf("Enter customer name (ENTER to Quit):");
     116.
                           gets(cat[i] -> name);               
     117.
                           
     118.
                                       if(!*cat[i] -> name)
     119.
                                                   break;
     120.
                           
     121.
                           printf("\nEnter AccNo:  ");
     122.
                           gets(cat[i] -> AccNo);               
     123.
                           
     124.
                           printf("\nEnter address:  ");
     125.
                           gets(cat[i] -> address);               
     126.
                           
     127.
                           printf("\nEnter year: ");
     128.
                           gets(temp);               
     129.
                           cat[i] -> date = (unsigned) atoi(temp);
     130.
                           
     131.
                           printf("\nEnter month: ");
     132.
                           gets(temp);               
     133.
                           cat[i] -> month = (unsigned) atoi(temp);
     134.
                           
     135.
                           
     136.
                         
     137.
                          }
     138.
                       
     139.
               top =i ;
     140.
                         
     141.
          }
     142.
             
     143.
          /* Search for customer */
     144.
           
     145.
          void customer_search(void)
     146.
          {
     147.
               char name[80];
     148.
               int i , found;
     149.
               
     150.
                   printf("Name:  ");
     151.
                   gets(name);
     152.
                   
     153.
                   found = 0;
     154.
                   
     155.
                         for (i=0; i<top; i++)
     156.
                             {
     157.
                                   if (!strcmp(name,cat[i]->name))
     158.
                                                  {
     159.
                                                           display(i);
     160.
                                                           found = 1;
     161.
                                                           printf("\n");
     162.
                                                  }
     163.
                             }
     164.
                             
     165.
                       if(!found)
     166.
                             printf("Not Found\n");
     167.
           
     168.
          }
     169.
           
     170.
           
     171.
           
     172.
          void AccNo_search(void)
     173.
          {
     174.
               char AccNo[80];
     175.
               int i , found;
     176.
               
     177.
                   printf("AccNo:  ");
     178.
                   gets(AccNo);
     179.
                   
     180.
                   found = 0;
     181.
                   
     182.
                         for (i=0; i<top; i++)
     183.
                             {
     184.
                                   if (!strcmp(AccNo,cat[i] -> AccNo))
     185.
                                                  {
     186.
                                                           display(i);
     187.
                                                           found = 1;
     188.
                                                           printf("\n");
     189.
                                                  }
     190.
                             }
     191.
                             
     192.
                       if(!found)
     193.
                             printf("Not Found\n");
     194.
          }
     195.
           
     196.
           
     197.
                                 
     198.
                       /*Display entries*/   
     199.
                       
     200.
          int display(int i)
     201.
          {
     202.
                            system("CLS");
     203.
                            printf("%s\n",cat[i]->AccNo);
     204.
                            printf("Account Holder Name: %s\n",cat[i]->name);
     205.
                            printf("address: %s\n",cat[i]->address);
     206.
                            printf("Year and Month: %u %u ",cat[i]->date,cat[i]->month);
     207.
          }       
     208.
             
     209.
                                  /*load file*/
     210.
                                 
     211.
          void load(void)
     212.
          {
     213.
               FILE *fp,*fp2;
     214.
               int i;
     215.
               
     216.
                   if((fp =fopen("index.bin","rb"))==NULL )//|| fp2 =fopen("bank.bin","rb"))==NULL)
     217.
                          {
     218.
                                                        printf("File does not exist on disk");
     219.
                                                        return;
     220.
                          }
     221.
                         
     222.
                         
     223.
                   if(fread(&top,sizeof top,1,fp) !=1)
     224.
                                        {
     225.
                                                  printf("Error Reading count");
     226.
                                                  exit(1);
     227.
                                        }
     228.
                                       
     229.
                   for (i=0 ; i < top ; i++)
     230.
                            {
     231.
                                  cat[i] = malloc(sizeof(struct catalog));
     232.
                                         
     233.
                                         if (!cat[i])
     234.
                                            {
     235.
                                                     printf("Out of Memory->\n");
     236.
                                                     top = i-1;
     237.
                                                     break;
     238.
                                            }
     239.
                                           
     240.
                                         if(fread(cat[i], sizeof(struct catalog),1,fp) !=1)
     241.
                                                          {
     242.
                                                                        printf("Error reading catalog data->\n");
     243.
                                                                        exit(1);
     244.
                                                          }
     245.
                            }
     246.
               
     247.
               
     248.
               
     249.
               for (i=0 ; i < top ; i++)
     250.
                            {
     251.
                                  bank[i] = malloc(sizeof(struct bank));
     252.
                                         
     253.
                                         if (!bank[i])
     254.
                                            {
     255.
                                                     printf("Out of Memory->\n");
     256.
                                                     top = i-1;
     257.
                                                     break;
     258.
                                            }
     259.
                                           
     260.
                                         if(fread(bank[i], sizeof(struct bank),1,fp) !=1)
     261.
                                                          {
     262.
                                                                        printf("Error reading catalog data->\n");
     263.
                                                                        exit(1);
     264.
                                                          }
     265.
                            }
     266.
               fclose(fp2); 
     267.
               fclose(fp);
     268.
          }
     269.
           
     270.
           
     271.
          /* Save the catalog file */
     272.
           
     273.
          void save(void)
     274.
          {
     275.
              FILE *fp;
     276.
               int i;
     277.
               
     278.
                     if((fp =fopen("index.bin","w+"))==NULL)
     279.
                                 {
     280.
                                                        printf("Cant open file->\n");
     281.
                                                        exit(1);
     282.
                                 }
     283.
                     if(fwrite(&top,sizeof top,1,fp) !=1)
     284.
                                         {
     285.
                                                  printf("Error Writting count.\n");
     286.
                                                  exit(1);
     287.
                                         }
     288.
                   
     289.
                   for(i=0 ; i < top ; i++)
     290.
                           {
     291.
                                 if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)
     292.
                                                   printf("Error writing count.\n");
     293.
                                                   exit(1);
    
                           }
    
                           
    
                   fclose(fp);
    
          }
    Can Anyone help PLEASE!!!!!!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Suggestions about your post:
    • Normal-size font is just fine.
    • Double-spaced code is unnecessarily whitespace intensive. In other words, bad.
    • Line numbers mean that we can't copy the code directly into an editor to compile it. If you want to refer to specific lines in a file, use comments and colours.


    Notes and suggestions about your code:
    • perror() is useful for messages like "Can't open file". You should at least say the name of the file that could not be opened, if not why, which is where perror() comes in.
    • sizeof(variable) can have or not have parentheses, it's true, but choose one style and stick with it.
    • At line 266 (they can come in useful!), you're closing a file that was never opened.
    • gets() is absolutely horrible. Do not use it. Use fgets(). cpwiki.sf.net/gets
    • atoi() isn't much better. Use strtod() or sscanf(). [edit] http://cboard.cprogramming.com/showp...3&postcount=11 [/edit]
    Last edited by dwks; 01-30-2008 at 06:38 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    add at the end of main getchar(); . That will wait until you press enter.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    sorry!!!

    Code:
       #include   <string.h>
          #include   <stdlib.h>
    
          #define MAX 100
    
          int menu(void);
          int display(int i);  
         void customer_search(void);
          void AccNo_search(void);
          void enter(void); .
          void save(void);
          void load(void);
    .
    struct catalog
          {
      
                 char name[80];
                 char AccNo[6];
                 char address[80];
                 unsigned date;
                 unsigned char month;
               
          }*cat[MAX];
      
    
    struct bank
          {
                  char BankName[20];
                  unsigned LastAccNum;
                  unsigned sortCode;
          }*bank[MAX];
    
    
          int top = 0;
    
    
          int main(void)
          {
                        int choice;
                        load();
    
                        do
                          {
                                   choice = menu();
                                   switch(choice)
                                         {
                                                 case 1: enter();
                                                      break;
                                                 
                                                 case 2: customer_search();
                                                      break;
                                                     
                                                 case 3: AccNo_search();
                                                      break;
                                                     
                                                 case 4: save();
                                         }
      
                          }
                 
                 while(choice !=5);
    
          system("PAUSE");
                 return 0;
          }   
    
    
    
    
    
                      /* Return a menu selection*/
          int menu(void)
          {
              int i;
              char str[80];
             
                   printf("\t\n\aEircom Billing System\n\n");             
                   printf("1->Add Customer\n");
                   printf("2->Search by Name\n");
                   printf("3->Search by Account Number\n");
                   printf("4->Save added Customers\n");
                   printf("5->Quit\n");     
                     do
                          {
                                           printf("Make your selection:  ");
                                           gets(str);
                                           i = atoi(str);
                                           printf("\n");                       
                          } while(i < 1 || i > 5);   
                     return i;
          }                   
    
    
    
              /*Enter customer into file */
      
    
    void enter(void)
          {
               int i;
               char temp[80];
               for (i = top; i<MAX ; i++)
                      {
                       
                               cat[i] = malloc(sizeof(struct catalog));
                          
    		          if(!cat[i])
                                           {
                                                           printf("Out Of Memory\n");
                                                           return;
                                           }
                          
    
                           printf("Enter customer name (ENTER to Quit):");
                           gets(cat[i] -> name);               
                           
                                       if(!*cat[i] -> name)
                                                   break;
                           
                               printf("\nEnter AccNo:  ");
                               gets(cat[i] -> AccNo);               
                          
    		        printf("\nEnter address:  ");
                              gets(cat[i] -> address);               
                           
    			printf("\nEnter year: ");
                       	 gets(temp);               
                          	 cat[i] -> date = (unsigned) atoi(temp);
                       	
    			    printf("\nEnter month: ");
             		              gets(temp);               
    	                       cat[i] -> month = (unsigned) atoi(temp);
                          }
               top =i ;
          }
    
    
          /* Search for customer */
    
    
     void customer_search(void)
          {
               char name[80];
               int i , found;
                   printf("Name:  ");
                   gets(name);
                   found = 0;
                   
                         for (i=0; i<top; i++)
                             {
                                   if (!strcmp(name,cat[i]->name))
                                                  {
                                                           display(i);
                                                           found = 1;
                                                           printf("\n");
     
                                                  }
                             }
                       if(!found)
                             printf("Not Found\n");
          }
    
    
    
     void AccNo_search(void)
          {
               char AccNo[80];
               int i , found;
               
                   printf("AccNo:  ");
                   gets(AccNo);
                   
                   found = 0;
                   
                         for (i=0; i<top; i++)
                             {
                                   if (!strcmp(AccNo,cat[i] -> AccNo))
                                                  {
                                                           display(i);
                                                           found = 1;
                                                           printf("\n");
                                                  }
                             }
                       if(!found)
                             printf("Not Found\n");
          }
    
    
                       /*Display entries*/   
    
    
     int display(int i)
          {
                            system("CLS");
                            printf("%s\n",cat[i]->AccNo);
                            printf("Account Holder Name: %s\n",cat[i]->name);
                            printf("address: %s\n",cat[i]->address);
                            printf("Year and Month: %u %u ",cat[i]->date,cat[i]->month);
          }       
    
    
    
                                  /*load file*/
    
     void load(void)
          {
               FILE *fp,*fp2;
               int i;
       
    
                if((fp =fopen("index.bin","rb"))==NULL )//|| fp2 =fopen("bank.bin","rb"))==NULL)
                          {
                                                        printf("File does not exist on disk");
                                                        return;
     
                          }
             
             if(fread(&top,sizeof top,1,fp) !=1)
                                        {
                                                  printf("Error Reading count");
                                                  exit(1);
                                        }
                                       
                   
    
    	for (i=0 ; i < top ; i++)
                            {
                                  cat[i] = malloc(sizeof(struct catalog));
                                         
    			if (!cat[i])
                                            {
                                                     printf("Out of Memory->\n");
                                                     top = i-1;
                                                     break;
                                            }
                                           
                                   if(fread(cat[i], sizeof(struct catalog),1,fp) !=1)
                                                          {
                                                                        printf("Error reading catalog data->\n");
                                                                        exit(1);
                                                          }
                            }
               
               for (i=0 ; i < top ; i++)
                            {
                                  bank[i] = malloc(sizeof(struct bank));
                                         
                             if (!bank[i])
                                           {
                                                     printf("Out of Memory->\n");
                                                     top = i-1;
    					  break;
                                            }
    
                            if(fread(bank[i], sizeof(struct bank),1,fp) !=1)
                                                          {
                                                                        printf("Error reading catalog data->\n");
                                                                        exit(1);
                                                          }
                            }
               fclose(fp2); 
               fclose(fp);
          }
    
    
          /* Save the catalog file */
    
    
    void save(void)
          {
              FILE *fp;
               int i;
               
                     if((fp =fopen("index.bin","w+"))==NULL)
                                 {
                                                        printf("Cant open file->\n");
                                                        exit(1);
                                 }
                     if(fwrite(&top,sizeof top,1,fp) !=1)
                                         {
                                                  printf("Error Writting count.\n");
                                                  exit(1);
                                         }
                   
                   for(i=0 ; i < top ; i++)
                           {
                                 if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)
                                                   printf("Error writing count.\n");
                                                   exit(1);
    
                           }
    
                           
    
                   fclose(fp);
    
          }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm tempted to say your code needs indentation help.
    We you have time you should indent properly and get rid of gets.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Here's a link giving the complete solution of your problem.


  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    yes I do have a probably with Indent.And foxman love the pic thanks

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    problem not probably

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Help please!
    By black_hole??? in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2009, 12:55 AM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. MFC in an empty project
    By cunnus88 in forum Windows Programming
    Replies: 0
    Last Post: 10-09-2005, 09:19 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM