Thread: Simplifying code

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Simplifying code

    So my code works but I've been told it's too complicated and that I will lose marks unless I simplify it... Any help would be great.

    Code:
       while (1)
       {
          c = fgetc(fp);
          if (c == EOF)
             break;
          while ((c == ' ') || (c == '\r') || (c == '\n'))
          {
             c = fgetc(fp);
             if (c == EOF)
                break;
          }
          buffer[0] = c;
          index = 1;
          c = fgetc(fp);
          while ((c != ' ') && (c != '\r') && (c != '\n') && (c != EOF))
          {
             buffer[index] = c;
             index++;
             c = fgetc(fp);
          }
          buffer[index] = '\0';
          empNo = atoi(buffer);
          if (c == EOF)
             break;
    
          while ((c == ' ') || (c == '\r') || (c == '\n'))
          {
             c = fgetc(fp);
             if (c == EOF)
                break;
          }
          buffer[0] = c;
          index = 1;
          c = fgetc(fp);
          while ((c != ' ') && (c != '\r') && (c != '\n') && (c != EOF))
          {
             buffer[index] = c;
             index++;
             c = fgetc(fp);
          }
          buffer[index] = '\0';
          strcpy(lastName, buffer);
          if (c == EOF)
             break;
    
          while ((c == ' ') || (c == '\r') || (c == '\n'))
          {
             c = fgetc(fp);
             if (c == EOF)
                break;
          }
          buffer[0] = c;
          index = 1;
          c = fgetc(fp);
          while ((c != ' ') && (c != '\r') && (c != '\n') && (c != EOF))
          {
             buffer[index] = c;
             index++;
             c = fgetc(fp);
          }
          buffer[index] = '\0';
          strcpy(firstName, buffer);
          if (c == EOF)
             break;
    
          while ((c == ' ') || (c == '\r') || (c == '\n'))
          {
             c = fgetc(fp);
             if (c == EOF)
                break;
          }
          buffer[0] = c;
          index = 1;
          c = fgetc(fp);
          while ((c != ' ') && (c != '\r') && (c != '\n') && (c != EOF))
          {
             buffer[index] = c;
             if (index > 0)
             {
                if ((buffer[index] == '.') && (buffer[index - 1] == '.'))
                {
                   printf("\nERROR");
                   FreeList();
                   fclose(fp);
                   return 5;
                }
             }
             index++;
             c = fgetc(fp);
          }
          buffer[index] = '\0';
          hourlyRate = (float)(atof(buffer));

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have three repetitions of this:
    Code:
    while ((c == ' ') || (c == '\r') || (c == '\n'))
          {
             c = fgetc(fp);
             if (c == EOF)
                break;
          }
    Doesn't it seem like they could be combined into just one loop or one function?

    Same with the other interior while loops. When you see code blocks that are repeated 3 times in the same function, that's lack of a good design.

    Step back and take a functional look at what your program does. Try and see the forest, not just the tree's.

    Once you've gone to the trouble to code this nitty gritty Rube Goldberg "and it works", type of program, it's very hard to go back and re-design it from a more top down, perspective.

    Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM