need a little help to store values in structure variables

This is a discussion on need a little help to store values in structure variables within the C Programming forums, part of the General Programming Boards category; Hello this is my first time here... Humm I need some help on how to store the values below. I ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    need a little help to store values in structure variables

    Hello this is my first time here... Humm I need some help on how to store the values below. I am suppose to find if points are inside a circle
    0 0
    4 4
    1 2
    0 1 2
    5 3 4
    2 2 3

    The first 3 values go in array pts and the other 3 values(with 3 points) go to array crs. I have tokenized the values and store the x and y values which are
    0 0
    4 4
    1 2
    0 1
    5 3
    2 2

    but I cannot find a way to store the values 2 4 3 (radius)

    I'm posting my code so you can see what I have done so far
    any help is appreciated.
    Attached Files Attached Files
    • File Type: c A2P2.c (987 Bytes, 87 views)

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    It's often a little easier for others to look at your work if you just post your code like so:

    Code:
    #include <stdio.h>           
    #include <ctype.h>           
    #include <string.h>          
    #include <stdlib.h>          
    
    #define ROW 3
    #define COL 2
    #define POINTS 6
    #define RADIUS 3
    
    struct points {
        int x;     
        int y;     
    
    };
    
    typedef struct points Points;
    
    struct circle {
        Points center;
        float radius; 
    };                
    
    typedef struct circle Circle;
    Points pts[3];               
    Circle crs[3];               
    
    void storeData(FILE * fdata);
    void solve();                
    
    int main()
    {         
    
        char filename[20];
        FILE *fdata;      
    
        printf("Enter file name: ");
        gets(filename);             
    
    //if file doesn't open report it and terminate
        if ((fdata = fopen(filename, "r")) == NULL) {
            printf("Failed to open file\n");         
            return 0;                                
    
        }
    
        storeData(fdata);
        solve();
    
        return 0;
    }
    
    void storeData(FILE * fdata)
    {
        int i, k, j, a, b, c, d, e, f;
        char temp[10];
        char *tokenPtr;
    
        for (i = 0; i < 6; i++) {
    
            fgets(temp, 10, fdata);
            //puts(temp);
    
            tokenPtr = strtok(temp, " ");
            a = atoi(tokenPtr);
            crs[i].center.x = a;
    
    
            tokenPtr = strtok(NULL, " ");
            b = atoi(tokenPtr);
            crs[i].center.y = b;
    
    
        }
    }

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    Thanks! I am new so....I still don't know how to post it right

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by ixcame View Post
    Thanks! I am new so....I still don't know how to post it right
    No problem. Just be sure to give these forum stickies a read:


  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Did you try using strtok and atof to store the radius?

    And if you know how fgets works then why are you still using gets at all?
    Code:
    fgets(filename, 20, stdin);
    filename = strtok (filename, "\n");

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    A couple of other points about style:

    1) You did this:

    Code:
    struct points {
        int x;
        int y;
    
    };
    
    typedef struct points Points;
    
    struct circle {
        Points center;
        float radius;
    };
    
    typedef struct circle Circle;
    Points pts[3];
    Circle crs[3];
    While typedef's are certainly allowed (and they even might save some typing), in this instance, I am not sure it gained you clarity. Although the names you used for the typedef's are basically saying what structure it is using, i.e, Points = struct points, and Circle = struct circle, in my opinion this is still not as easy to read as:

    Code:
    struct points {
        int x;     
        int y;     
    };
    
    struct circle {
        struct points center;
        double radius;       
    };                       
    
    struct circle crs[POINTS];
    By removing a bit of unecessary stuff, I also got it down in size by 4 lines total, and 9 characters less than your version.

    2) Notice this:

    Code:
    #define POINTS 6
    struct circle crs[POINTS];
    I used your #define POINTS, as it seemed to be the number you were using for entries from your data file. By doing:

    Code:
    struct circle crs[3];
    What do you think happens in the following bit of your storeData() function?:

    Code:
     for (i = 0; i < 6; i++) {
    
            fgets(temp, 10, fdata);
            //puts(temp);
    
            tokenPtr = strtok(temp, " ");
            a = atoi(tokenPtr);
            crs[i].center.x = a;
    3) You did this:

    Code:
    tokenPtr = strtok(temp, " ");
            a = atoi(tokenPtr);
    Be aware that strtok returns a pointer to the next token, or NULL if there are no more tokens. Assuming you get a NULL return from strtok (and you will if you add something to deal with the radius numbers), and then pass that on to atoi, what is that going to do with your program? (Hint: Think KABOOM!!) You need to check the return value of strtok for NULL (That applies for your call(s) to fgets as well!).

    4) You did:

    Code:
    int i, k, j, a, b, c, d, e, f;
    But you only used i, a, and b. Furthermore, why not give use some useful names (i is ok, because that is C idiom for loop counters, but a and b are not so good). How about:

    Code:
    int xcoord;
    int ycoord;
    double radius;
    Last edited by kermit; 08-15-2010 at 06:11 AM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by kermit View Post
    A couple of other points about style:

    1) You did this:

    Code:
    struct points {
        int x;
        int y;
    
    };
    
    typedef struct points Points;
    
    struct circle {
        Points center;
        float radius;
    };
    
    typedef struct circle Circle;
    Points pts[3];
    Circle crs[3];
    While typedef's are certainly allowed (and they even might save some typing), in this instance, I am not sure it gained you clarity. Although the names you used for the typedef's are basically saying what structure it is using, i.e, Points = struct points, and Circle = struct circle, in my opinion this is still not as easy to read as:

    Code:
    struct points {
        int x;     
        int y;     
    };
    
    struct circle {
        struct points center;
        double radius;       
    };                       
    
    struct circle crs[POINTS];
    That's funny. I'm completely the opposite. "struct points" looks just ugly and annoying. Substituting it for Points would, IMO, be much better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 100
    Last Post: 06-21-2010, 02:22 AM
  2. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  3. Replies: 9
    Last Post: 02-16-2009, 11:20 AM
  4. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  5. loop to store values
    By keendoc in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2005, 02:43 PM

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