Not returning correct results

This is a discussion on Not returning correct results within the C Programming forums, part of the General Programming Boards category; All my calculations are coming up under the Female columns. They're in the correct age and lead type, but all ...

  1. #1
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    All my calculations are coming up under the Female columns. They're in the correct age and lead type, but all the males are being calculated as females! What am I doing wrong?

    Code:
    #include <stdio.h>
    #define MAXRECORDS 4
    
    struct record
    {
       char gender;
       int age;
       int lead;
    };
    
    void read_file(struct record rec[]);
    void calculations(int c[], struct record rec[]);
    void print_report(int c[], struct record rec[]);
    
    int main()
    {
       int c[36] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
       struct record rec[MAXRECORDS];
       read_file(rec);
       calculations(c, rec);
       print_report(c, rec);
    }
    
    void read_file(struct record rec[])
    {
       FILE *fpin;
       int i;
    
       fpin = fopen("custsurvey.dat", "r");
    
       if(fpin == NULL)
       {
          printf("Error opening file");
       }
       else
       {
          for(i = 0; i < MAXRECORDS; i++)
             fscanf(fpin, " %c  %d %d", &rec[i].gender, &rec[i].age, &rec[i].lead);
       }
    
       fclose(fpin);
    
       return;
    }
    
    void calculations(int c[], struct record rec[])
    {
       int i = 0;
    
       while(i < MAXRECORDS)
       {
          if((rec[i].gender = 'F') && (rec[i].age < 25) && (rec[i].lead == 1))
          {
             c[0]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 1))
          {
             c[1]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age > 40) && (rec[i].lead == 1))
          {
             c[2]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age < 25) && (rec[i].lead == 2))
          {
             c[3]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 2))
          {
             c[4]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age > 40) && (rec[i].lead == 2))
          {
             c[5]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age < 25) && (rec[i].lead == 3))
          {
             c[6]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 3))
          {
             c[7]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age > 40) && (rec[i].lead == 3))
          {
             c[8]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age < 25) && (rec[i].lead == 4))
          {
             c[9]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 4))
          {
             c[10]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age > 40) && (rec[i].lead == 4))
          {
             c[11]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age < 25) && (rec[i].lead == 5))
          {
             c[12]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 5))
          {
             c[13]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age > 40) && (rec[i].lead == 5))
          {
             c[14]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age < 25) && (rec[i].lead == 6))
          {
             c[15]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 6))
          {
             c[16]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age > 40) && (rec[i].lead == 6))
          {
             c[17]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age < 25) && (rec[i].lead == 1))
          {
             c[18]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 1))
          {
             c[19]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age > 40) && (rec[i].lead == 1))
          {
             c[20]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age < 25) && (rec[i].lead == 2))
          {
             c[21]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 2))
          {
             c[22]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age > 40) && (rec[i].lead == 2))
          {
             c[23]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age < 25) && (rec[i].lead == 3))
          {
             c[24]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 3))
          {
             c[25]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age > 40) && (rec[i].lead == 3))
          {
             c[26]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age < 25) && (rec[i].lead == 4))
          {
             c[27]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 4))
          {
             c[28]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age > 40) && (rec[i].lead == 4))
          {
             c[29]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age < 25) && (rec[i].lead == 5))
          {
             c[30]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 5))
          {
             c[31]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age > 40) && (rec[i].lead == 5))
          {
             c[32]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age < 25) && (rec[i].lead == 6))
          {
             c[33]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 6))
          {
             c[34]++;
             i++;
          }
          else if((rec[i].gender = 'M') && (rec[i].age > 40) && (rec[i].lead == 6))
          {
             c[35]++;
             i++;
          }
       }
    
       return;
    }
    void print_report(int c[], struct record rec[])
    {
       FILE *fpout;
    
       fpout = fopen("surveyreport.txt", "w");
    
       if(fpout == NULL)
       {
          printf("Error opening file");
       }
       else
       {
          fputs("                            CUSTOMER SURVEY REPORT\n", fpout);
          fputs("                            ======================\n", fpout);
          fputs("                      FEMALES                   MALES\n", fpout);
          fputs("              =======================  =======================\n", fpout);
          fputs("              younger  range   older   younger  range   older\n", fpout);
          fputs("              than 25 25 - 40 than 40  than 25 25 - 40 than 40\n", fpout);
          fputs("    LEAD TYPE =======================  ======================= TOTAL\n", fpout);
          fputs("    =========                                                  =====\n\n", fpout);
          fprintf(fpout,"       REPEAT   %3d     %3d     %3d      %3d     %3d     %3d\n\n", c[0], c[1], c[2], c[18], c[19], c[20]);
          fprintf(fpout,"TELEVISION AD   %3d     %3d     %3d      %3d     %3d     %3d\n\n", c[3], c[4], c[5], c[21], c[22], c[23]);
          fprintf(fpout," NEWSPAPER AD   %3d     %3d     %3d      %3d     %3d     %3d\n\n", c[6], c[7], c[8], c[24], c[25], c[26]);
          fprintf(fpout,"     RADIO AD   %3d     %3d     %3d      %3d     %3d     %3d\n\n", c[9], c[10], c[11], c[27], c[28], c[29]);
          fprintf(fpout,"WORD OF MOUTH   %3d     %3d     %3d      %3d     %3d     %3d\n\n", c[12], c[13], c[14], c[30], c[31], c[32]);
          fprintf(fpout,"        OTHER   %3d     %3d     %3d      %3d     %3d     %3d\n\n", c[15], c[16], c[17], c[33], c[34], c[35]);
       }
    }
    Last edited by drty2; 01-18-2009 at 06:48 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Code:
     
     if((rec[i].gender = 'F')
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    oh lol thanks

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I haven't looked at it, but I'm quite sure you could simplify your "calculations" function quite a bit.

    There are several things you could do. The first one is to move the i++ out of those if-statements [by the way, if you have a bad record, e.g. one that doesn't have 'M' or 'F' in the record, you will end up with an infinite loop, trying to process the same bad record forever - because your current code only does i++ when you have found a valid record.

    Second, you are testing for the same condition many times:
    Code:
         if((rec[i].gender = 'F') && (rec[i].age < 25) && (rec[i].lead == 1))
          {
             c[0]++;
             i++;
          }
          else if((rec[i].gender = 'F') && (rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 1))
          {
             c[1]++;
             i++;
          }
    can be written as:
    Code:
         if(rec[i].gender = 'F')
         {
           if (rec[i].age < 25) && (rec[i].lead == 1))
           {
              c[0]++;
            }
            else if((rec[i].age >= 25) && (rec[i].age <= 40) && (rec[i].lead == 1))
            {
               c[1]++;
            }
            ...
          } else if (rec[i].gender = 'M') ... 
    
          i++;
    I would probably also make the c array into a struct with a 2-element array, so you just test for male/female once, and set a variable to indicate which it is, then you can do the age/lead testing ONCE, rather than having it twice.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You can save a lot of typing and tedious counting by re-writing
    Code:
    int c[36] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    as
    Code:
    int c[36] = {0};
    All elements that you do not specify are automatically initialized to zero. (And you have to put at least one element in, at least in C.)

    My same notes about fclose() etc in the other thread also still apply here.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Help with struct... not returning correct results.
    By drty2 in forum C Programming
    Replies: 7
    Last Post: 01-18-2009, 10:25 PM
  3. Replies: 3
    Last Post: 08-13-2008, 01:34 PM
  4. Incorrect results from fmod()?
    By karlthetruth in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 09:12 AM
  5. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM

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