-
Calculating speed
Hello I'm working on a project for a school presentation, The program is not really finished but 1 module is causing a linker error , "LNK1120 Unresolved external" and I'm unable to find why.
Code:
void CalSpeed(CAR cars[] , int count)
{
int i;
float slopeRad;
float slopeTangent;
printf("MODEL\tSKIDLENGTH\tROADSLOAP\tSPEED\n");
for(i = 0; i < count; i++)
{
slopeRad = cars[i].roadslope * pi / 180;
slopeTangent = sin(slopeRad) / cos(slopeRad) ;
Speed[i] = sqrt(30 * cars[i].skidlength * (slopeTangent + brakefactor));
printf("%s\t%d\t%d\t%.2f\n", cars[i].carmodel , cars[i].skidlength , cars[i].roadslope , Speed[i]);
}
}
Here is the prototype for the module
Code:
void CalSpeed(CAR cars[] , int count);
And its call is at the end of this function:
Code:
void readValues(){
int x = 0;
char correct[3];
char test[50];
while(!feof(stdin))
{
printf("Enter A cars information below Enter ctrl + Z to end entry\n\n");
printf("\n\t\t===================================\n\n\t\t ENTRY WINDOW \n\n\t\t===================================");
fflush(stdin);
printf("\nEnter car model=>");
fgets(car[x].carmodel , 50 , stdin);
if(feof(stdin))
break;
printf("\nEnter the skid length of car=>");
fgets(test , 50 , stdin);
check(test);
car[x].skidlength = atoi(test);
while (car[x].skidlength <= 0)
{
system("cls");
allegro_message("INVALID. Enter a skid length greater than 0");
printf("Enter skid length => ");
fgets(test , 50 , stdin);
check(test);
car[x].skidlength = atoi(test);
}
printf("\nEnter the degree of the road slope=>");
fgets(test , 50 , stdin);
check(test);
car[x].roadslope = atoi(test);
while (car[x].roadslope > 30 || car[x].roadslope < -30)
{
system("cls");
fflush(stdin);
allegro_message("INVALID. Enter a degree > -30 and < +30");
printf("Enter roadsloap => ");
fgets(test , 50 , stdin);
check(test);
car[x].roadslope = atoi(test);
}
system("cls");
printf("Is this information correct? [Y] or [N]:\n\n");
printf("%s: ", "Model");
printf("%s\n" , car[x].carmodel);
printf("%s: " , "Skidlength");
printf("%d\n\n" , car[x].skidlength );
printf("%s: " , "Roadslope");
printf("%d\n\n" , car[x].roadslope );
fgets(correct , 2 , stdin);
correct[0] = toupper(correct[0]);
response_check(correct);
correct[0] == 'N' ? x-- : x++;
system("cls");
}
CalSpeed(car , x -1);
}
If anyone could help it would be most appreciated
-
What's with all the complicated stdin stuff?
Get rid of feof > SourceForge.net: Feof - cpwiki
Get rid of fflush(stdin) > SourceForge.net: Fflush - cpwiki
Also, you have a ton of copy/pasting, especially when you're error checking.
-
I suppose I could come up with a better way of terminating the loop than feof and make the code a bit more readable but for now the code is still a work in progress I definitely plan on fixing it up a little bit once I have the main components running , the only problem I have at the moment tho is the linker error related to the function CalSpeed , If I remove the function the program runs , however when I have it I get a linker error.
-
Is your program in just one source file? If not, the problem is probably that you did not compile and link all your source files, e.g., because you forgot to include them when running the compiler, or left them out of the build script or IDE project.
-
Nope , the project is all in one source file , I can run it if I block out that one function.
-
Do you think its possible the problem may be in the way I used the functions sqrt , sin or cos?
-
Nevermind commenting them out I got the same error.
-
Post the smallest and simplest program that you expect will compile but which demonstrates the error. At the moment, I could ask if say, you placed the prototype in the wrong place, but having to play this question/answer game in the absence of code that you have is unnecessarily tedious.
-
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
const float pi = 3.1416;
const float brakefactor = 0.7;
float Speed[];
FILE *record;
FILE *backup;
struct car_records
{
char carmodel[50];
int skidlength;
int roadslope;
};
typedef struct car_records CAR;
void CalSpeed(CAR cars[] , int count);
CAR car[100];
int main()
{
char num[3];
char answer[3];
strcpy(car[0].carmodel , "Toyota");
car[0].roadslope = 20;
car[0].skidlength = 35;
CalSpeed(car , 1);
return 0;
}
void CalSpeed(CAR cars[] , int count)
{
int i;
float slopeRad;
float slopeTangent;
printf("MODEL\tSKIDLENGTH\tROADSLOAP\tSPEED\n");
for(i = 0; i < count; i++)
{
slopeRad = cars[i].roadslope * pi / 180;
slopeTangent = sin(slopeRad) / cos(slopeRad) ;
Speed[i] = 0;//sqrt(30 * cars[i].skidlength * (slopeTangent + brakefactor));
printf("%s\t%d\t%d\t%.2f\n", cars[i].carmodel , cars[i].skidlength , cars[i].roadslope , Speed[i]);
}
}
Sorry about the delay , in theory this should run but I get the linker error.
-
I don't get a linker error, but you're not allocating any space for Speed.
-
Hmm... I cannot replicate your error. However, my compiler does complain about this line:
Did you forget to specify the size of the array?
-
Wow that was what was causing the problem I forgot to allocate space for Speed, I really didn't imagine that was causing it. Thanks For the help everyone