[C] Debug help

This is a discussion on [C] Debug help within the C Programming forums, part of the General Programming Boards category; Ok this is my first time running a debug on a program. Im getting this: From what i can tell ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    [C] Debug help

    Ok this is my first time running a debug on a program.

    Im getting this:
    Name:  debug.jpg
Views: 271
Size:  154.4 KB

    From what i can tell sumx cannot get the data from the struct measurements.
    Any ideas on how i can fix this?

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>


    struct filenames
    {
    char measurements_filename[101] ;
    char line_parameters_filename[101];
    };

    struct measurements
    {
    double x,y;
    struct measurements *next;
    };

    struct line_parameters
    {
    double gradient, constant;
    struct line_parameters *next;
    };



    int main (void)
    {
    struct filenames filenames, *filenames_ptr;
    struct measurements measurements, *measurement_ptr;
    struct line_parameters line_parameters, *line_parameters_ptr;

    void get_filenames(struct filenames *);
    void read_measurments(struct filenames*, struct measurements *);
    void calculate_line_parameters(struct measurements*, struct line_parameters * );
    void write_line_parameters(struct filenames, struct line_parameters );

    filenames_ptr = &filenames;
    measurement_ptr= &measurements;
    line_parameters_ptr= &line_parameters;


    get_filenames(filenames_ptr);
    fprintf(stdout, "%s\n",filenames_ptr->measurements_filename);
    fprintf(stdout, "%s\n",filenames_ptr->line_parameters_filename);

    read_measurments(filenames_ptr, measurement_ptr);
    fprintf(stdout, "Measurements Read\n");

    calculate_line_parameters(measurement_ptr, line_parameters_ptr);
    fprintf(stdout, "Calculating Parameters\n");

    write_line_parameters(filenames, line_parameters);
    fprintf(stdout, "Gradient and Constant written to file");
    return(0);
    }




    void get_filenames(struct filenames *filenames_ptr)
    {
    fprintf(stdout, "Enter file name:\n");
    fscanf(stdin, "%s",filenames_ptr->measurements_filename);

    fprintf(stdout, "Enter file name to Save to:\n");
    fscanf(stdin, "%s",filenames_ptr->line_parameters_filename);
    return;
    }




    void read_measurments(struct filenames *filenames_ptr, struct measurements *measurement_ptr)
    {
    char line[101];
    char *line_ptr;
    int no_values=0;
    struct measurements *current_measurements_ptr=measurement_ptr;
    FILE *input_stream;
    input_stream=fopen(filenames_ptr->measurements_filename, "r");

    if(input_stream!=NULL)
    {
    fprintf(stdout, "File found!\n");
    fgets(line,sizeof(line), input_stream);

    while (((line_ptr=fgets(line,sizeof(line), input_stream))!=NULL) && ((no_values= sscanf(line,"%lf %lf",
    &current_measurements_ptr->x,
    &current_measurements_ptr->y))==2))
    {
    current_measurements_ptr -> next=(struct measurements*)malloc(sizeof(struct measurements));
    if (measurement_ptr!=NULL)
    {
    current_measurements_ptr=current_measurements_ptr -> next;
    current_measurements_ptr -> next= NULL;
    }

    }
    }
    fclose(input_stream);
    return;
    }




    void calculate_line_parameters(struct measurements *measurements, struct line_parameters *line_parameters_ptr)

    {
    struct measurements *measurement_ptr=NULL, *current_measurements_ptr;

    double sumx=0, sumy=0, sumxx=0, sumxy=0, n=0;

    current_measurements_ptr = measurements;

    while(current_measurements_ptr->next != NULL)
    {

    sumx += measurement_ptr->x;
    sumy += measurement_ptr->y;
    sumxx += (measurement_ptr->x) * (measurement_ptr->x);
    sumxy += (measurement_ptr->x) * (measurement_ptr->y);

    n ++;
    current_measurements_ptr = current_measurements_ptr->next;
    }

    line_parameters_ptr->gradient = ((n * sumxy)-(sumx * sumy))/((n * sumxx)-(sumx * sumx));
    line_parameters_ptr->constant = ((sumxy * sumx) - (sumxx * sumy)) / ((sumx * sumx) -(n * sumxx));
    fprintf(stdout, "lol WIN");
    return;
    }



    void write_line_parameters(struct filenames filenames, struct line_parameters line_parameters)
    {
    FILE *output_stream;
    output_stream = fopen(filenames.line_parameters_filename, "w");

    fprintf(output_stream, "%lf \n", line_parameters.gradient);
    fprintf(output_stream, "%lf \n", line_parameters.constant);

    fclose(output_stream);
    return;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your linked list is really quite weird: your first node is a local variable in main, then you allocate the rest of the nodes from the heap.

    Your post's indentation is non-existant, so it's really hard to read - and I don't really know how you achieve that, since your code in the screenshot looks indented as it should be.

    Edit: 2 seconds after posting that, it occurs to me:
    struct measurements *measurement_ptr=NULL
    will result in exactly what you are experiencing.

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

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Oh its just when i copy over into this the indentations go a miss.

    i could upload a .c file if that would be better.
    I think i need to sit down and look at my linked lists and what there doing

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's really strange, as it works just fine for me every time. What browser do you use?

    --
    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
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    At home i use Chrome
    Uni i use IE7

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And which one have you been using for the latest posts? Chrome? It may be that it's broken in some way.

    --
    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
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Ok i now have a working copy

    One problem my output is:

    [QUOTE]Gradient = -1.#IND00
    Constant = -1.#IND00
    [/CODE]

    Some how these are not whole number s:S

    Code:
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    
    
    struct filenames
    	{
    	char measurements_filename[101] ;
    	char line_parameters_filename[101];
    	};
    
    struct measurements
    	{
    	double x,y;
    	struct measurements *next;
    	};
    
    struct line_parameters
    	{
    	double gradient, constant;
    	struct line_parameters *next;
    	};
    
    
    
    int main (void)
    {
    struct filenames filenames, *filenames_ptr;
    struct measurements measurements, *measurement_ptr;
    struct line_parameters line_parameters, *line_parameters_ptr;
    	
    	void get_filenames(struct filenames *);
    	void read_measurments(struct filenames*, struct measurements *);
    	void calculate_line_parameters(struct measurements*, struct line_parameters * );
    	void write_line_parameters(struct filenames, struct line_parameters );
    
    filenames_ptr = &filenames;
    measurement_ptr= &measurements;
    line_parameters_ptr= &line_parameters; 
    
    
    get_filenames(filenames_ptr);
    fprintf(stdout, "&#37;s\n",filenames_ptr->measurements_filename);
    fprintf(stdout, "%s\n",filenames_ptr->line_parameters_filename);
    
    read_measurments(filenames_ptr, measurement_ptr);
    fprintf(stdout, "Measurements Read\n");
    
    calculate_line_parameters(measurement_ptr, line_parameters_ptr);
    fprintf(stdout, "Calculating Parameters\n");
    
    write_line_parameters(filenames, line_parameters);
    fprintf(stdout, "Gradient and Constant written to file\n");
    return(0);
    }
    
    
    
    
    void get_filenames(struct filenames *filenames_ptr)
    {
    fprintf(stdout, "Enter file name:\n");
    fscanf(stdin, "%s",filenames_ptr->measurements_filename);
    
    fprintf(stdout, "Enter file name to Save to:\n");
    fscanf(stdin, "%s",filenames_ptr->line_parameters_filename);
    return;
    }
    
    
    
    
    void read_measurments(struct filenames *filenames_ptr, struct measurements *measurement_ptr)
    {
    char line[101];
    char *line_ptr;
    int no_values=0;
    struct measurements *current_measurements_ptr=measurement_ptr;
    FILE *input_stream;
    input_stream=fopen(filenames_ptr->measurements_filename, "r");
    
    if(input_stream!=NULL)
    		{
    		fprintf(stdout, "File found!\n");
    		fgets(line,sizeof(line), input_stream);
    			
    			while (((line_ptr=fgets(line,sizeof(line), input_stream))!=NULL) && ((no_values= sscanf(line,"%lf %lf",
    										&current_measurements_ptr->x,
    										&current_measurements_ptr->y))==2))
    			{
    			current_measurements_ptr -> next=(struct measurements*)malloc(sizeof(struct measurements));		
    		
    			if((line_ptr!=NULL) && (no_values!=2))
    			fprintf(stdout, "Error reading line %s \n", line);		
    			current_measurements_ptr -> next= NULL;	
    			
    			}
    }	
    fclose(input_stream);
    return;
    }
    				
    
    
    
    void calculate_line_parameters(struct measurements *measurements, struct line_parameters *line_parameters_ptr)
    
    {
    struct measurements *measurement_ptr=NULL, *current_measurements_ptr;
    
    double sumx=0, sumy=0, sumxx=0, sumxy=0, n=0;
    
    current_measurements_ptr = measurements;
    
    while(current_measurements_ptr->next != NULL)
    	{
    
    	sumx  +=  measurement_ptr->x;
    	sumy  +=  measurement_ptr->y;
    	sumxx += (measurement_ptr->x) * (measurement_ptr->x);
    	sumxy += (measurement_ptr->x) * (measurement_ptr->y);
    	
    	n ++;
    	current_measurements_ptr = current_measurements_ptr->next;
    	}
    
    line_parameters_ptr->gradient = ((n * sumxy)-(sumx * sumy))/((n * sumxx)-(sumx * sumx));
    line_parameters_ptr->constant = ((sumxy * sumx) - (sumxx * sumy)) / ((sumx * sumx) -(n * sumxx));
    return;
    }
    
    
    
    void write_line_parameters(struct filenames filenames, struct line_parameters line_parameters)
    {
    FILE *output_stream;
    output_stream = fopen(filenames.line_parameters_filename, "w");
    
    fprintf(stdout, "Gradient; %lf\n",line_parameters.gradient);	
    fprintf(stdout, "Gradient; %lf\n",line_parameters.constant);
    fprintf(output_stream, "Gradient = %lf \n", line_parameters.gradient);
    fprintf(output_stream, "Constant = %lf \n", line_parameters.constant);
    
    fclose(output_stream);
    return;
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    #IND means that you have a "indefinite" value. This is usually the result of using undefined variables (that haven't been loaded with proper floating point values). I would guess a printout of what you are adding up would give you the failing value quite quickly.

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

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    ie printing out everything to screen that im looking at from the file and also all the calculations?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Scupham View Post
    ie printing out everything to screen that im looking at from the file and also all the calculations?
    Just the data that is the input of the calculation should be fine - my guess is that you have a #IND number in your input data - which leads to #IND in the result.

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

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    X Y
    1.0 2.876
    2.6 4.234
    3.7 8.874
    4.5 7.698
    6.0 9.932
    That is my input data into the system.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Your indentation is broken.
    Also, http://cpwiki.sf.net/Don't_remove_parameter_names

    With your latest code, I get:
    Warning 1 warning C6246: Local declaration of 'filenames' hides declaration of the same name in outer scope. For additional information, see previous declaration at line '28' of 'g:\w00t\visual studio 2008\projects\temp\temp2.cpp': Lines: 28 g:\w00t\visual studio 2008\projects\temp\temp2.cpp 35
    Warning 4 warning C6387: 'argument 1' might be '0': this does not adhere to the specification for the function 'fclose': Lines: 75, 76, 77, 78, 79, 80, 82, 99 g:\w00t\visual studio 2008\projects\temp\temp2.cpp 99
    Warning 5 warning C6011: Dereferencing NULL pointer 'measurement_ptr': Lines: 109, 111, 113, 115, 118 g:\w00t\visual studio 2008\projects\temp\temp2.cpp 118

    As for warning 4, it basically says that the file handle may be NULL when you call fclose which would result in a crash. You want to close it ONLY if it was opened successfully.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Hey im using Visual Studio 6, im not getting any errors or warnings.

    For error 4, this program is being marked against different data types ensuring it can read and write. In the specification it is not needed to deal with the data not being found.
    (Uni Project, Product Design & Manufacture. Strange module for and Engineer lol)

    Im guessing that also one of my ptr's is just going off into the distance and not working maybe? and thats why im getting this error

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by Scupham View Post
    Hey im using Visual Studio 6, im not getting any errors or warnings.
    A bit old compiler, perhaps.
    Can you upgrade? The newest Visual Studio (Express) is free and provide you with valuable compiler upgrades.

    For error 4, this program is being marked against different data types ensuring it can read and write. In the specification it is not needed to deal with the data not being found.
    (Uni Project, Product Design & Manufacture. Strange module for and Engineer lol)
    Perhaps, but still, it is good practice and might earn an extra point or two.
    The last thing you want is your program crashing if it failed to open a file. All you have to do is move the line of code inside the if statement.

    Also, two other things I have noticed.
    Code:
    struct filenames filenames, *filenames_ptr;
    struct measurements measurements, *measurement_ptr;
    struct line_parameters line_parameters, *line_parameters_ptr;
    	
    filenames_ptr = &filenames;
    measurement_ptr= &measurements;
    line_parameters_ptr= &line_parameters; 
    
    get_filenames(filenames_ptr);
    // etc
    This is just unnecessary. It could simply be written as:
    Code:
    struct filenames filenames;
    struct measurements measurements;
    struct line_parameters line_parameters;
    	
    get_filenames(&filenames);
    // etc
    Also, a repetition:
    Code:
    	void get_filenames(struct filenames *);
    	void read_measurments(struct filenames*, struct measurements *);
    	void calculate_line_parameters(struct measurements*, struct line_parameters * );
    	void write_line_parameters(struct filenames, struct line_parameters );
    Prototypes are best put at the top of the source file, or in a header, and not inside functions. Further, as I showed the link before, do not strip the parameter names from the prototypes. The link explains why.

    And the last thing I noticed is that you use fscanf to read strings.
    You are doing it wrong, however, and this is not good.
    For info about the problem and how to fix it: https://apps.sourceforge.net/mediawi...tle=Scanf_woes
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Hi thanks for the advice, cannot use 2008 as im in a Uni lab atm. However the program is marked on Vis Basic 6, should should be ok.

    I tried changing the struct etc as you said, but this created errors later on in text.

    Can anyone see which pointer is leading my program astray?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 12:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM

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