![]() |
| | #1 | ||
| Registered User Join Date: Dec 2008
Posts: 19
| [C] Compiler error while using functions 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:
Code: Quote:
| ||
| Scupham is offline | |
| | #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 | |
| | #3 |
| Registered User Join Date: Dec 2008
Posts: 19
| Ok changed that thanks |
| Scupham is offline | |
| | #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",
¤t_measurements_ptr->x,
¤t_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;
}
} |
| JoeBloggs is offline | |
| | #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",
¤t_measurements_ptr->x,
¤t_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;
}
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 | |
| | #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); 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:
Last edited by Elysia; 12-09-2008 at 04:15 AM. | |
| Elysia is offline | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |