Thread: Can someone please help me with my assignment (Help Needed Desperately)

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    6

    Can someone please help me with my assignment (Help Needed Desperately)

    I am having trouble adding two records one after the other. There’s no problem when I add the first record but when I try to add another I get that exception error message. When I close the program down and run it again and then try to add the record it works fine (that’s until I try to add the second record again), anyways this is the code I got so far…………

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <alloc.h>
    
    FILE *fSupplier;
    void DisplaySupplierMenu();
    void SearchBySupplierCode();
    void AddSupplierRecord();
    void AmendSupplierRecord();
    bool SearchSupplier();
    bool SearchSupplierOne();
    void DisplaySearchedSupplierRecord();
    bool WriteNewSupplierRecord();
    
    void LoadSupplierRecord(struct SupplierRefer **q);
    void AddSupplierNode(struct SupplierRefer **q, int num);
    void AssignSupplierRefer(struct SupplierRefer **p);
    
    bool FindSupplierName();
    void OpenSupplierFile();
    void CloseSupplierFile();
    
    int CurrSupplierRecNum=0;
    int TotalSupplierNode=0;
    char SearchedSupplierCode[7];
    char SearchedSupplierName[21];
    char purpose[25];
    
    struct Supplier{
    	 char SupplierCode[7];
    	 char SupplierName[21];
    	 char SupplierAddress[21];
    };
    
    struct Supplier Supplier;
    
    struct SupplierRefer{
    	 char SupplierCode[7];
    	 char SupplierName[21];
    	 struct  SupplierRefer *Supplierlink;
    };
    struct SupplierRefer *mySupplierrefer;
    
    int main()
    {
    	 mySupplierrefer=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer));
    	 LoadSupplierRecord(&mySupplierrefer);
    	 bool isExit=false;
    	 while (!isExit)
    	 {
    		DisplaySupplierMenu();
    		char ch;
    		gets(&ch);
    		switch (ch)
                    {
                            case '1':
                                    SearchBySupplierCode();
                                    break;
                            case '2':
                                    AddSupplierRecord();
                                    break;
                            case '3':
                                    AmendSupplierRecord();
                                    break;
                            case '0':
                                    isExit=true;
    			 }
    		}
    	 return 0;
    }
    
    void DisplaySupplierMenu()
    {
             puts("                                                                     \n");
    	 puts("           *         1 : View Supplier Record                       *\n");
    	 puts("           *         2 : Add Supplier Record                        *\n");
    	 puts("           *         3 : Amend Supplier Record                      *\n");
    	 puts("           *         0 : Exit                                       *\n");
             puts("                                                                     \n");
    }
    
    void OpenSupplierFile()
    {
            fSupplier=fopen("a:\\Supplier.txt", "r+");
            if (fSupplier==0)
            {
                    fSupplier=fopen("a:\\Supplier.txt", "w");
                    fclose(fSupplier);
                    fSupplier=fopen("a:\\Supplier.txt", "r+");
            }
    }
    
    void CloseSupplierFile()
    {
            fclose(fSupplier);
    }
    
    void SearchBySupplierCode()
    {
            OpenSupplierFile();
            puts("Enter Supplier Code For The Record You Want To Search\n");
            gets(SearchedSupplierCode);
    
            if (SearchSupplier())
            {
                    DisplaySearchedSupplierRecord();
                    CloseSupplierFile();
                    return;
            }
            else
            {
                    printf("Supplier Code Wasn't Found");
                    return;
            }
    }
    
    void DisplaySearchedSupplierRecord()
    {
            long offset;
            printf("Searched Record Details\n");
    
            offset=sizeof(Supplier)* CurrSupplierRecNum;
            fseek(fSupplier,offset,0);
            fread(&Supplier, sizeof(Supplier), 1, fSupplier);
            puts("\nSupplier Code");
            puts(Supplier.SupplierCode);
            puts("\nSupplier Name");
            puts(Supplier.SupplierName);
            puts("\nSupplier Address");
            puts(Supplier.SupplierAddress);
            printf("\nSearched Successfully");
    }
    
    void AddSupplierRecord()
    {
            puts("\nEnter New Supplier Code\n");
            gets(SearchedSupplierCode);
            OpenSupplierFile();
    	while(true)
            {
                    if (SearchSupplierOne())
                    {
                            printf("\nEntered Supplier Code Already Exixts\n");
                            puts("\nEnter New Supplier Code\n");
    		        gets(SearchedSupplierCode);
                    }
    	        else
                    {
    			 if (WriteNewSupplierRecord())
                             {
                                    AddSupplierNode(&mySupplierrefer, TotalSupplierNode);
                                    fclose(fSupplier);
                                    return;
                             }
    			 else
                             {
                                    fclose(fSupplier);
                                    return;
                             }
                    }
            }
    }
    
    bool WriteNewSupplierRecord()
    {
            long offset;
            char temp_name[21];
            char temp_address[21];
            offset=sizeof(Supplier)* TotalSupplierNode;
            fseek(fSupplier, offset, 0);
    
            fflush(stdin);
    
            if(strlen(SearchedSupplierCode)== 6)
            {
                    strcpy(Supplier.SupplierCode, SearchedSupplierCode);
                    puts("Enter Value For Supplier Name, Should Have Atleast Single Character\n");
                    gets(temp_name);
                    if(strlen(temp_name)>0 & strlen(temp_name)<21)
                    {
                            strcpy(Supplier.SupplierName, temp_name);
                            puts("Enter Value For Supplier Address\n");
                            gets(temp_address);
                            if(strlen(temp_address)>0 & strlen(temp_address)<21)
                            {
                                    strcpy(Supplier.SupplierAddress, temp_address);
                            }
                            else
                            {
                                    puts("Enterd Supplier Address Is Not Valid, Should Be Between 1-20 Characters");
                                    return false;
                            }
                            fwrite(&Supplier, sizeof(Supplier), 1, fSupplier);
                            puts("New Record Added Successfully");
                            return true;
                    }
                    else
                    {
                            puts("Enterd Supplier Name Is Not Valid, Should Be Between 1-20 Characters, Should Have Atleast Single Character");
    		        AddSupplierRecord();
                    }
            }
            else
            {
                    puts("Enterd Supplier Code Is Not Valid, Should Be 6 Characters\n");
                    AddSupplierRecord();
            }
    }
    
    void AmendSupplierRecord()
    {
            long offset;
            char temp_name[21];
            char temp_address[21];
    
            fflush(stdin);
            puts("\nEnter Supplier Code You Want To Amend\n");
            gets(SearchedSupplierCode);
            OpenSupplierFile();
            if (SearchSupplier())
            {
                    offset=sizeof(Supplier)* CurrSupplierRecNum;
                    fseek(fSupplier, offset, 0);
                    fread(&Supplier, sizeof(Supplier), 1, fSupplier);
    
                    puts(Supplier.SupplierName);
                    puts("\nEnter New Value For Supplier Name\n");
                    gets(temp_name);
                    if(strlen(temp_name)>0 & strlen(temp_name)<21)
                            strcpy(Supplier.SupplierName, temp_name);
                    else
                    {
                            printf("Entered Supplier Name Is Not Valid, Should Be Between 1-20 Characters");
                            return;
                    }
    
                    puts(Supplier.SupplierAddress);
                    puts("Enter New Value For Supplier Address\n");
                    gets(temp_address);
                    if(strlen(temp_address)>0 & strlen(temp_address)<21)
                            strcpy(Supplier.SupplierAddress, temp_address);
                    else
                    {
                            printf("Entered Supplier Address Is Not Valid, Should Be Between 1-20 Characters");
                            return;
                    }
                    fseek(fSupplier, offset, 0);
                    fwrite(&Supplier, sizeof(Supplier),1,fSupplier);
                    fflush(stdin);
    
                    puts("Record Successful Amended");
                    CloseSupplierFile();
                    LoadSupplierRecord(&mySupplierrefer);
    	  }
    	  else
    	  {
                    printf("Searched Supplier Code Does Not Exist");
                    fflush(stdin);
              }
    }
    
    bool SearchSupplier()
    {
            int index=-1;
            struct SupplierRefer *temp;
            temp=mySupplierrefer;
            if  (TotalSupplierNode==0 )
            {
                    puts("File Is Empty.");
                    return false;
            }
            while (index<=TotalSupplierNode)
            {
                    if  (strcmp(SearchedSupplierCode,temp->SupplierCode)==0)
                    {
                            CurrSupplierRecNum=index;
                            return true;
                    }
                    else
                    {
                            index+=1;
                            temp=temp->Supplierlink;
                    }
            }
            return false;
    }
    
    bool SearchSupplierOne()
    {
            int index=0;
            struct SupplierRefer *temp;
            temp=mySupplierrefer;
            if  (TotalSupplierNode==0 )
            {
                    puts("File Is Empty.");
                    return false;
            }
            while (index<=TotalSupplierNode)
            {
                    if  (strcmp(SearchedSupplierCode,temp->SupplierCode)==0)
                    {
                            CurrSupplierRecNum=index;
                            return true;
                    }
                    else
                    {
                            index+=1;
                            temp=temp->Supplierlink;
                    }
            }
            return false;
    }
    
    bool FindSupplierName()
    {
            OpenSupplierFile();
            return false ;
    }
    
    void  LoadSupplierRecord(struct SupplierRefer **q)
    {
            long offset;
    	struct SupplierRefer *temp;
    	temp=*q;
    	TotalSupplierNode=0;
    	OpenSupplierFile();
    
            while(true)
            {
                    if(fread(&Supplier, sizeof(Supplier),1,fSupplier)==1)
                    {
                            if (temp==NULL)
                            {
                                    temp=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer));
                                    AssignSupplierRefer(&temp);
                            }
                            else
                            {
                                    temp->Supplierlink=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer) );
                                    temp=temp->Supplierlink;
                                    AssignSupplierRefer(&temp);
                            }
                    }
                    else
                    {
                            CloseSupplierFile();
                            break;
                    }
            }
            return;
    }
    
    void AssignSupplierRefer(struct SupplierRefer **p)
    {
            struct SupplierRefer *temp;
            temp=*p;
    
            long offset=sizeof(Supplier)*TotalSupplierNode;
            fseek(fSupplier, offset, 0);
            fread(&Supplier, sizeof(Supplier), 1,fSupplier);
            strcpy(temp->SupplierCode,Supplier.SupplierCode);
            strcpy(temp->SupplierName, Supplier.SupplierName);
            TotalSupplierNode+=1;
            return;
    }
    
    void AddSupplierNode(struct SupplierRefer **q, int num)
    {
            int index;
            struct  SupplierRefer *temp;
    
            if (TotalSupplierNode==0 || *q==NULL)
            {
                    *q=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer));
                    temp=*q;
                    AssignSupplierRefer(&temp);
                    return;
            }
            else
            {
                    while(index<=TotalSupplierNode)
                    {
                            temp=temp->Supplierlink;
                    }
                            temp=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer) );
                            AssignSupplierRefer(&temp);
    
            }
            return;
    }
    any help with be greatly appreciated, Thanx

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    6
    I better provide bit more information about the program

    This program suppose to create three separate text files, am still working on the first (supplier.txt) file got another two more files to work.

    The user should be able to view, add and amend records. I think only problem with the program right now is that it just doesn't allow me to add two records one after the other. Once the problem is fixed I can move on to the other two files :-)

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    A couple things to get started (from a quick read-thru). You close by a function and directly - pick one method and stick to it. "SearchBySupplier doesn't close the file unless it succeeds - but always opens - fix this.

    Your recursive calls to AddSupplierRecord keep opening the file again with no close - you may be just using up all the available file pointers. Also the WriteNewSupplierRecord doesn't return anything after it recalls AddSupplierRecord - but sadly enough you don't know what to return in his case - you will have to straighten out your logic here - put all the conditional part of developing the record into one function and the open, write, close and add to tree stuff in the other then call it only when everything is ok.

    There may be more, but that should get you started again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Desperate help needed. C++ Uni Assignment.
    By ILLaViTaR in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2009, 01:30 PM
  3. Basic C-- Help Desperately Needed
    By toadkiwi in forum C Programming
    Replies: 10
    Last Post: 02-14-2008, 11:08 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM