[C] Compiler error while using functions

This is a discussion on [C] Compiler error while using functions within the C Programming forums, part of the General Programming Boards category; I am getting a compiler error which is preventing me from completing my program. If i can get the one ...

  1. #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
    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:
    #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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Ok changed that thanks

  4. #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,"&#37;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.

  5. #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 09:38 AM. Reason: [code][/code] tags are the tags to use for code - not quote, not bold, not ....

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    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).
    Last edited by Elysia; 12-09-2008 at 03:15 AM.
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    1
    the program will run without errors and warnings, but it would not run properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  2. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. can't figure out the compiler error
    By cgmacewindu in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2002, 06:45 PM
  5. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM

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