How am I supposed to do this without a structure?

This is a discussion on How am I supposed to do this without a structure? within the C Programming forums, part of the General Programming Boards category; My instructor said I wasn't allowed to use a structure as we hadn't been taught it yet. So how am ...

  1. #1
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15

    How am I supposed to do this without a structure?

    My instructor said I wasn't allowed to use a structure as we hadn't been taught it yet. So how am I supposed to read this from a file into an array?
    It's set out like this
    M 40 6
    F 21 2
    F 61 5
    M 14 1


    This is what I have but of course it won't work properly as the first data type is char not int

    Code:
    #include <stdio.h>
    
    #define RECORDS 200
    #define FIELDS 3
    
    void read_file();
    
    int main()
    {
       read_file();
    }
    
    void read_file()
    {
       FILE *fpin;
    
       int rec[RECORDS][FIELDS];
       int m, n;
    
       fpin = fopen("custsurvey.dat", "r");
    
       if(fpin == NULL)
       {
          printf("Error opening file");
       }
       else
       {
          for(m = 0; m < RECORDS; m++)
          {
             for(n = 0; n < FIELDS; n++)
             {
                fscanf("%d", &rec[m][n]);
             }
          }
       }
       fclose(fpin);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Parallel arrays is the norm before studying structs with one array. Your first array will be type char, your others will be type int. char array[i] and int array1[i], int array2[i], etc., will all refer to the same record (person), and will be used as fields in a record or members in a struct.

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    What you could do is write 1 into the array if it is M and 2 if it is F.



    maybe
    Code:
          for(m = 0; m < RECORDS; m++)
          {
             char sex;
    
             fscanf("%c %d %d\n", &sex,   &rec[m][1],   &rec[m][1]   );
             if (sex=='M') rec[m][0]=1;
             if (sex=='F') rec[m][0]=2;
             if ( sex!='M' || sex!='F') printf("\nOh dear!!");
    
    
        }

  4. #4
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    Ok. Esbo I had had that idea but wasn't sure how to do it. Thanks.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    better not tu use magic numbers
    Code:
    enum sex
    {
       Female = 1,
       Male
    };
    and use these constants instead of 1 and 2
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    better not tu use magic numbers
    Code:
    enum sex
    {
       Female = 1,
       Male
    };
    and use these constants instead of 1 and 2
    Of course, enum may also not be available yet...

    --
    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.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Quote Originally Posted by matsp View Post
    Of course, enum may also not be available yet...

    --
    Mats
    Then #define
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User gil_savir's Avatar
    Join Date
    Jan 2009
    Posts
    13
    Quote Originally Posted by esbo View Post
    Code:
    ...
             if ( sex!='M' || sex!='F') printf("\nOh dear!!");
    ...
    esbo, your if condition will always result in TRUE. I guess you meant:
    Code:
    if ( sex!='M' && sex!='F') ...

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by gil_savir View Post
    esbo, your if condition will always result in TRUE. I guess you meant:
    Code:
    if ( sex!='M' && sex!='F') ...
    Indeed, careless of me. I was going to use elseif there anyway so it would be OK
    if the data was correct.
    My fault for providing error checking lol.

    Actually specking of errors I think a more serious one was:-

    Code:
      fscanf("%c %d %d\n", &sex,   &rec[m][1],   &rec[m][1]   );
    Code:
      fscanf("%c %d %d\n", &sex,   &rec[m][1],   &rec[m][2]   );
    Last edited by esbo; 01-21-2009 at 06:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double pointer to a structure
    By rohit_second in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 03:32 AM
  2. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  3. Reference to a Structure
    By boschow in forum C Programming
    Replies: 3
    Last Post: 03-28-2008, 02:37 PM
  4. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  5. Creating Dynamic Structure in C
    By SomeNath in forum C Programming
    Replies: 1
    Last Post: 03-19-2005, 08:11 AM

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