C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-09-2008, 04:18 AM   #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: 28
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:
Quote:
#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;
}
Scupham is offline   Reply With Quote
Old 12-09-2008, 04:35 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 12-09-2008, 04:43 AM   #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
Scupham is offline   Reply With Quote
Old 12-09-2008, 04:46 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 12-09-2008, 05:39 AM   #5
Registered User
 
Join Date: Dec 2008
Posts: 19
At home i use Chrome
Uni i use IE7
Scupham is offline   Reply With Quote
Old 12-09-2008, 05:45 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 12-09-2008, 05:49 AM   #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, "%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;
}
Scupham is offline   Reply With Quote
Old 12-09-2008, 05:56 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
#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.
matsp is offline   Reply With Quote
Old 12-09-2008, 05:59 AM   #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?
Scupham is offline   Reply With Quote
Old 12-09-2008, 06:02 AM   #10
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 12-09-2008, 06:09 AM   #11
Registered User
 
Join Date: Dec 2008
Posts: 19
Quote:
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.
Scupham is offline   Reply With Quote
Old 12-09-2008, 06:15 AM   #12
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 12-09-2008, 06:42 AM   #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
Scupham is offline   Reply With Quote
Old 12-09-2008, 07:06 AM   #14
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.

Quote:
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
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 12-09-2008, 07:20 AM   #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?
Scupham is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:18 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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