Calculating speed

This is a discussion on Calculating speed within the C Programming forums, part of the General Programming Boards category; Hello I'm working on a project for a school presentation, The program is not really finished but 1 module is ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    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

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    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.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Nope , the project is all in one source file , I can run it if I block out that one function.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Do you think its possible the problem may be in the way I used the functions sqrt , sin or cos?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Nevermind commenting them out I got the same error.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    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.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't get a linker error, but you're not allocating any space for Speed.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Hmm... I cannot replicate your error. However, my compiler does complain about this line:
    Code:
    float Speed[];
    Did you forget to specify the size of the array?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed of pointers vs. speed of arrays/structs
    By Kempelen in forum C Programming
    Replies: 32
    Last Post: 06-27-2008, 10:16 AM
  2. calculating the mean
    By bartleby84 in forum C Programming
    Replies: 9
    Last Post: 08-27-2007, 11:47 AM
  3. Replies: 6
    Last Post: 01-08-2006, 01:49 PM
  4. calculating cos
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2005, 02:39 PM
  5. VB Speed compared to VC++ Speed.
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 04:01 PM

Tags for this Thread


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