C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-09-2008, 03:12 AM   #1
Registered User
 
Join Date: Dec 2008
Posts: 19
[C] Compiler error while using functions

I am getting a compiler error which is preventing me from completing my program.

If i can get the one error out it should solve all of the others.

ALL ERRORS ARE IN BOLD SECTION OF CODE

Errors
Quote:
J:\test\test.c(101) : error C2143: syntax error : missing ';' before 'type'
J:\test\test.c(104) : error C2143: syntax error : missing ';' before 'type'
J:\test\test.c(111) : error C2065: 'sumx' : undeclared identifier
J:\test\test.c(111) : warning C4244: '+=' : conversion from 'double ' to 'int ', possible loss of data
J:\test\test.c(112) : error C2065: 'sumy' : undeclared identifier
J:\test\test.c(112) : warning C4244: '+=' : conversion from 'double ' to 'int ', possible loss of data
J:\test\test.c(113) : error C2065: 'sumxx' : undeclared identifier
J:\test\test.c(113) : warning C4244: '+=' : conversion from 'double ' to 'int ', possible loss of data
J:\test\test.c(114) : error C2065: 'sumxy' : undeclared identifier
J:\test\test.c(114) : warning C4244: '+=' : conversion from 'double ' to 'int ', possible loss of data
J:\test\test.c(116) : error C2065: 'n' : undeclared identifier
J:\test\test.c(120) : error C2065: 'line_parameters_ptr' : undeclared identifier
J:\test\test.c(120) : error C2223: left of '->gradient' must point to struct/union
J:\test\test.c(121) : error C2223: left of '->constant' must point to struct/union
Error executing cl.exe.

test.obj - 10 error(s), 4 warning(s)

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);
calculate_line_parameters(measurement_ptr, line_parameters_ptr);
write_line_parameters(filenames, line_parameters);
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, *new_measurements_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 *measurement_ptr, 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 = new_measurements_ptr ;

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;
}
Thanks for your time
Scupham is offline   Reply With Quote
Old 12-09-2008, 03:21 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
If you indent your code properly, then you will see that you are missing a end-brace, which causes the compiler to get completely lost. The compiler doesn't care about indentation, but humans do!

--
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, 03:26 AM   #3
Registered User
 
Join Date: Dec 2008
Posts: 19
Ok changed that thanks
Scupham is offline   Reply With Quote
Old 12-09-2008, 03:28 AM   #4
Registered User
 
Join Date: Dec 2008
Posts: 3
Code:
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, *new_measurements_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;
}
} 
Bolded and underlined.
JoeBloggs is offline   Reply With Quote
Old 12-09-2008, 03:38 AM   #5
Registered User
 
Join Date: Dec 2008
Posts: 19
EDIT: bolded wrong section, actually crashing at calculating data

Ok new issues.

I get the program crashing when i run, it seems to crash when im calculating the line parameters

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, "%sn",filenames_ptr->measurements_filename);
fprintf(stdout, "%sn",filenames_ptr->line_parameters_filename);

read_measurments(filenames_ptr, measurement_ptr);
calculate_line_parameters(measurement_ptr, line_parameters_ptr);
write_line_parameters(filenames, line_parameters);
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_patr->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;
}
Any ideas?

Last edited by Salem; 12-09-2008 at 10:38 AM. Reason: [code][/code] tags are the tags to use for code - not quote, not bold, not ....
Scupham is offline   Reply With Quote
Old 12-09-2008, 04:05 AM   #6
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Indent the code. You were told before.
UPDATE:
Code:
sumx += measurement_ptr->x;
sumy += measurement_ptr->y;
sumxx += (measurement_ptr->x) * (measurement_ptr->x);
sumxy += (measurement_patr->x) * (measurement_ptr->y);
measurement_ptr is uninitialized.
Heed your compiler warnings!
If none are shown, enable max warnings.
Project properties -> C/C++ -> General -> Warnings -> Set to level 4.
You can also launch your program through the debugger. It will break at things such as using an unitialized variable (default hotkey: F5, though it may vary with keyboard scheme).
__________________
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.

Last edited by Elysia; 12-09-2008 at 04:15 AM.
Elysia is offline   Reply With Quote
Old 12-09-2008, 04:09 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Is the first line of your input not used?

I take it you don't actually want to use the last measurement, right?

--
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:25 PM   #8
Registered User
 
Join Date: Dec 2008
Posts: 1
the program will run without errors and warnings, but it would not run properly.
mindagap is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it required to provide default definitions for virtual functions in a base class? Canadian0469 C++ Programming 7 11-17-2008 01:00 PM
Though implementation problem Elysia C++ Programming 296 05-31-2008 01:02 PM
Passing pointers between functions heygirls_uk C Programming 5 01-09-2004 06:58 PM
can't figure out the compiler error cgmacewindu C++ Programming 2 12-01-2002 07:45 PM
error LNK2001: unresolved external symbol Unregistered C Programming 12 07-12-2002 08:45 PM


All times are GMT -6. The time now is 10:40 AM.


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