Jump to content

Need help fixing this bug

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Johnny Correa

Johnny Correa

    Newbie

  • Members
  • Pip
  • 2 posts
Hey guys, I wrote a program for my CS class that uses data structures to store data for a gym. The problem is that the the account with the highest ID is always accounted for twice. For example, if you first run the program, and select option 5 to view the record, you will notice that the client with ID number 105 is printed twice. Also, in the function averages, his stats are added twice when it loops through. If you choose option 2 in the main menu to add a new client, and give the new client a higher ID than any of the already existing clients, then that new client will be accounted for twice. I have worked on this bug for hours, but I cannot figure anything out. If anyone could help, I would greatly appreciate it.

Here is the code


#include <stdio.h>


struct CustomerData

{

   int customerID;

   char customerFirstName[20];

   char customerLastName[20];

   int age;

   char gender[2];

   float weight;

   float height;

   int membershipTerm;

};


int menu(void);

void averages(FILE *ptr);

void addRecord(FILE *ptr);

void changeRecord(FILE *ptr);

void deleteRecord(FILE *ptr);

void viewRecord(FILE *ptr);//DELETE BEFORE TURN IN!!!!!

void initialCustomers(FILE *ptr);


int main()

{

   FILE *cPtr;

   int choice = 0;




   if ((cPtr = fopen ("customers.dat", "wb+")) == NULL)

   {

      printf("Could not open file.\n");

   }

   else

   {

      initialCustomers(cPtr);

      while (choice != 6)

      {

         choice = menu();

         switch(choice)

         {

               case 1:

               averages(cPtr);

               break;

               case 2:

               addRecord(cPtr);

               break;

               case 3:

               changeRecord(cPtr);

               break;

               case 4:

               deleteRecord(cPtr);

               break;

               case 5:

               viewRecord(cPtr);

               break;

               case 6:

               break;

               default:

               printf("Invalid choice");

               break;

         }

      }

   }

   fclose(cPtr);

   return 0;

}


int menu(void)

{

   int i;


   printf("\nMAIN MENU\n\n");

   printf("1. Calculate Averages\n");

   printf("2. Add Record\n");

   printf("3. Change Record\n");

   printf("4. Delete Record\n");

   printf("5. View All Records\n");

   printf("6. Quit\n");

   printf("\nSelection: ");


   scanf("%d", &i);


   return i;

}


void averages(FILE *ptr)

{

   struct CustomerData client = {0,"","",0,"",0.0,0.0,0};


   float avgAge = 0, avgWeight = 0, avgHeight = 0, avgMem = 0;

   int count = 0;

   rewind(ptr);


   if((ptr = fopen("customers.dat","rb+")) == NULL)

   {

      printf("file could not be opened");

   }

   else{

      while (!feof(ptr))

      {

         fread(&client, sizeof(struct CustomerData), 1, ptr);

         if (client.customerID != 0)

         {

            avgAge += client.age;

            avgWeight += client.weight;

            avgHeight += client.height;

            avgMem += client.membershipTerm;

            count++;


         }

      }

   }

   //for some reason it would add the last record on file twice, so i

   //created lines 113-117 as a work around for the bug

   avgAge -= client.age;

   avgWeight -= client.weight;

   avgHeight -= client.height;

   avgMem -= client.membershipTerm;

   count--;

   avgAge /= count;

   avgWeight /= count;

   avgHeight /= count;

   avgMem /= count;

   printf("\nThe average age is %.2f.\n", avgAge);

   printf("\nThe average weight is is %.2f.\n", avgWeight);

   printf("\nThe average height is %.2f.\n", avgHeight);

   printf("\nThe average membership term is %.2f.\n\n", avgMem);




}

void addRecord(FILE *ptr)

{

   struct CustomerData client = {0,"","",0,"",0.0,0.0,0};

   int id;


   printf("Enter an ID number to be added: ");

   scanf("%d", &id);


   fseek(ptr, (id - 1) * sizeof(struct CustomerData), SEEK_SET);


   fread(&client, sizeof(struct CustomerData), 1, ptr);


   if (client.customerID != 0)

   {

      printf("This ID already exist.");

   }

   else

   {

      printf("Enter first name, last name, age, gender, weight, height, membership term:\n");

      scanf("%s%s%d%s%f%f%d)", &client.customerFirstName, &client.customerLastName, &client.age, &client.gender, &client.weight, &client.height, &client.membershipTerm);


      client.customerID = id;


      fseek(ptr, (client.customerID - 1) * sizeof(struct CustomerData), SEEK_SET);

      fwrite(&client, sizeof(struct CustomerData), 1, ptr);

   }

}

void changeRecord(FILE *ptr)

{

   struct CustomerData client = {0,"","",0,"",0.0,0.0,0};

   int id;


   printf("Enter a customer ID to be edited: ");

   scanf("%d", &id);


   fseek(ptr, (id - 1) * sizeof(struct CustomerData), SEEK_SET);

   fread(&client, sizeof(struct CustomerData), 1, ptr);


   while (client.customerID == 0)

   {

      printf("This ID does not exist. Enter a valid ID: ");

      scanf("%d", &id);


      fseek(ptr, (id - 1) * sizeof(struct CustomerData), SEEK_SET);

      fread(&client, sizeof(struct CustomerData), 1, ptr);

   }


   printf("Enter first name, last name, age, gender, weight, height, membership term:\n");

   scanf("%s%s%d%s%f%f%d)", &client.customerFirstName, &client.customerLastName, &client.age, &client.gender, &client.weight, &client.height, &client.membershipTerm);


   client.customerID = id;


   fseek(ptr, (client.customerID - 1) * sizeof(struct CustomerData), SEEK_SET);

   fwrite(&client, sizeof(struct CustomerData), 1, ptr);

}

void deleteRecord(FILE *ptr)

{

   struct CustomerData client;

   struct CustomerData deleteClient = {0,"","",0,"",0.0,0.0,0};

   int id;


   printf("Enter a customer ID to be deleted: ");

   scanf("%d", &id);


   fseek(ptr, (id - 1) * sizeof(struct CustomerData), SEEK_SET);

   fread(&client, sizeof(struct CustomerData), 1, ptr);


   while (client.customerID == 0)

   {

      printf("This ID does not exist. Enter a valid ID: ");

      scanf("%d", &id);


      fseek(ptr, (id - 1) * sizeof(struct CustomerData), SEEK_SET);

      fread(&client, sizeof(struct CustomerData), 1, ptr);

   }


   fseek(ptr, (id - 1) * sizeof(struct CustomerData), SEEK_SET);

   fwrite(&deleteClient, sizeof(struct CustomerData), 1, ptr);


   printf("The record with customerID %d has been deleted.\n", id);


}

void initialCustomers(FILE *ptr)

{

   struct CustomerData initialClient1 = {101,"Jane","Cane",17,"F",120.15,160.56,6};

   struct CustomerData initialClient2 = {102,"Mike","Space",33,"M",90.78,180.35,12};

   struct CustomerData initialClient3 = {103,"Sean","Todd",45,"M",110,191.50,3};

   struct CustomerData initialClient4 = {104,"Alanis","Hall",25,"F",50.3,178.65,9};

   struct CustomerData initialClient5 = {105,"Frank","Cool",47,"F",95.8,175.45,12};


   fseek(ptr, (100) * sizeof(struct CustomerData), SEEK_SET);

   fwrite(&initialClient1, sizeof(struct CustomerData), 1, ptr);

   fwrite(&initialClient2, sizeof(struct CustomerData), 1, ptr);

   fwrite(&initialClient3, sizeof(struct CustomerData), 1, ptr);

   fwrite(&initialClient4, sizeof(struct CustomerData), 1, ptr);

   fwrite(&initialClient5, sizeof(struct CustomerData), 1, ptr);

}


void viewRecord(FILE *ptr)

{

   struct CustomerData client = {0,"","",0,"",0.0,0.0,0};


   rewind(ptr);

   printf("\n%-5s%-21s%-21s%-4s\%-5s\n\n", "ID", "First Name", "Last Name", "Age", "Gender");

   //printf("%-6s%-6s%-4s\n", "Weight", "Height", "Membership Term");


   if((ptr = fopen("customers.dat","rb+")) == NULL)

   {

      printf("file could not be opened");

   }

   else

   {

      while (!feof(ptr))

      {

         fread(&client, sizeof(struct CustomerData),1,ptr);


         if(client.customerID != 0)

         {

            printf("\n%-5d%-21s%-21s%d%-3s", client.customerID, client.customerFirstName, client.customerLastName, client.age, client.gender);

         }

      }

   }

   printf("\n");

}


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Do you happen to have an extra carriage return after the last line of data? If so, then you wouldn't be at EOF, and it would try to read data again, even though you're effectively at EOF.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Johnny Correa

Johnny Correa

    Newbie

  • Members
  • Pip
  • 2 posts
I apologize, but I'm not quite sure what you mean. Could you please tell me what you mean by carriage return?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
A carriage return would move the text file from one line to the next. You haven't included the file being parsed, so we don't know what data is being processed.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
@Seb:

scanf - C++ Reference
fread - C++ Reference

Edit: I just necro'ed a thread. Crap.
sudo rm -rf /

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
We're all allowed to answer questions, regardless of who they're aimed at. Since Johnny didn't respond, I did. Excuse me for taking the time to answer one of your questions. As for your code, you'll get whatever was in there beforehand, or perhaps partially written corrupted data. That depends on the way it's implemented.

And though your "tone" or "mood" may be obvious to you, the impression your statements leave on us is what matters most. From your other posts it's not a good one.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users