**Anyone know why my code is not compling? I keep getting the errors shown below
ID: 3834957 • Letter: #
Question
**Anyone know why my code is not compling? I keep getting the errors shown below, and my code is also below that. Not sure where I'm going wrong with my brackets.
tracker.c:31:1: error: function definition is not allowed here
{^
tracker.c:40:60: error: function definition is not allowed here
double nearest_point_on_road(int road, double x, double y){ //checking ...
^
tracker.c:59:1: error: function definition is not allowed here
{
^
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Put this right after the #includes.
#ifndef SECRET_TESTS
double M1=-1.0, B1=0; // Road 1 slope and intercept
double M2=1.0, B2=0; // Road 2 slope and intercept
#else
// This allows me to test your program easily with different roads
double M1=SECRET_M1, B1=SECRET_B1;
double M2=SECRET_M2, B2=SECRET_B2;
#endif
// initializing all recognized and necessary variables
double Total_distance=0;
double Peak_speed=0.0;
int Changed_direction=0;
int Changed_road=0;
int First_road=1, Road=1;
void track(double time, double x, double y) {
double distance(double x1, double y1, double x2, double y2)
{
double distance;
double distance_x = x1-x2;
double distance_y = y1- y2;
distance = sqrt( (distance_x * distance_x) + (distance_y * distance_y));
return distance;
}
double nearest_point_on_road(int road, double x, double y){ //checking to see which parameter/road is being selected
double b = 0;
if(road == 1){
b = y-(x *(-1/M1));//function to calculate the nearest point on the road by using perpendicular lines
x = (b-B1)/(M1+(1/M1));
}
else if(road == 2){
b = y-(x *(-1/M2));
x = (b-B2)/(M2+(1/M2));
}
if (x==-0){
x=0;
}
return x;
}
double distance_to_road(int road, double x, double y)
{
double b;
double dist;
double x1;
double x2;
double y1;
double y2;
x1 = x; //reinitializing values from previous function into x1 and y1
y1 = y;
x2 = nearest_point_on_road(road, x, y); //checking to see the closest point you are at on either road, then computing the distance and is then returned
if (road == 1) {
b = y-x*(-1/M1);
y=x*(-1/M1)+b;
y2 = B1 + M2*x2; //nearest point on the road function
}
//recognizing the distance of which road is being calculated
if (road == 2) {
b = y-x*(-1/M1);
y = x*(-1/M1)+b;
y2 = B1 + M2*x2;
}
dist = distance (x1, y1, x2, y2);
return dist; //calculated distance is returned
}
}
/* Function Declarations */
void track(double time, double x, double y);
int main() {
double time, x, y;
int ret;
do {
// get time,x,y coordinate from stdin
ret = scanf("%lf%lf%lf",&time,&x,&y);
if (ret==3)
{
// call into the tracking algorithm
track(time,x,y);
}
// if we didn't get three doubles, time to quit
} while(ret==3);
// print out final results
printf("Total distance = %.2lf ",Total_distance);
printf("Peak speed = %.2lf ",Peak_speed);
printf("# of changes in direction = %d ",Changed_direction);
printf("# of changes in road = %d ",Changed_road);
printf("First Road = %d, Last Road = %d ",First_road,Road);
return 0;
}
Explanation / Answer
This is a c code, seems like you are compilling it with g++.
Try using gcc
$ gcc tracker.c
$
$ g++ tracker.c
tracker.c: In function u2018void track(double, double, double)u2019:
tracker.c:23:1: error: a function-definition is not allowed here before u2018{u2019 token
{
^
tracker.c:31:63: error: a function-definition is not allowed here before u2018{u2019 token
double nearest_point_on_road(int road, double x, double y){ //checking to see which parameter/road is being selected
^
tracker.c:101:1: error: expected u2018}u2019 at end of input
}
^
=========================================================
Here I amde your code compile iwth g++ also (basically you were defining a function inside a function)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Put this right after the #includes.
#ifndef SECRET_TESTS
double M1=-1.0, B1=0; // Road 1 slope and intercept
double M2=1.0, B2=0; // Road 2 slope and intercept
#else
// This allows me to test your program easily with different roads
double M1=SECRET_M1, B1=SECRET_B1;
double M2=SECRET_M2, B2=SECRET_B2;
#endif
// initializing all recognized and necessary variables
double Total_distance=0;
double Peak_speed=0.0;
int Changed_direction=0;
int Changed_road=0;
int First_road=1, Road=1;
void track(double time, double x, double y)
{
}
double distance(double x1, double y1, double x2, double y2)
{
double distance;
double distance_x = x1-x2;
double distance_y = y1- y2;
distance = sqrt( (distance_x * distance_x) + (distance_y * distance_y));
return distance;
}
double nearest_point_on_road(int road, double x, double y){ //checking to see which parameter/road is being selected
double b = 0;
if(road == 1){
b = y-(x *(-1/M1));//function to calculate the nearest point on the road by using perpendicular lines
x = (b-B1)/(M1+(1/M1));
}
else if(road == 2){
b = y-(x *(-1/M2));
x = (b-B2)/(M2+(1/M2));
}
if (x==-0){
x=0;
}
return x;
}
double distance_to_road(int road, double x, double y)
{
double b;
double dist;
double x1;
double x2;
double y1;
double y2;
x1 = x; //reinitializing values from previous function into x1 and y1
y1 = y;
x2 = nearest_point_on_road(road, x, y); //checking to see the closest point you are at on either road, then computing the distance and is then returned
if (road == 1) {
b = y-x*(-1/M1);
y=x*(-1/M1)+b;
y2 = B1 + M2*x2; //nearest point on the road function
}
//recognizing the distance of which road is being calculated
if (road == 2) {
b = y-x*(-1/M1);
y = x*(-1/M1)+b;
y2 = B1 + M2*x2;
}
dist = distance (x1, y1, x2, y2);
return dist; //calculated distance is returned
}
/* Function Declarations */
void track(double time, double x, double y);
int main() {
double time, x, y;
int ret;
do {
// get time,x,y coordinate from stdin
ret = scanf("%lf%lf%lf",&time,&x,&y);
if (ret==3)
{
// call into the tracking algorithm
track(time,x,y);
}
// if we didn't get three doubles, time to quit
} while(ret==3);
// print out final results
printf("Total distance = %.2lf ",Total_distance);
printf("Peak speed = %.2lf ",Peak_speed);
printf("# of changes in direction = %d ",Changed_direction);
printf("# of changes in road = %d ",Changed_road);
printf("First Road = %d, Last Road = %d ",First_road,Road);
return 0;
}
But your logic still doesn't seems right to me as it is doing nowthing.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.