Thread: Module Help!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    14

    Module Help!

    I keep getting the following errors and I can't figure out what is wrong.
    22 [Warning] passing arg 1 of `readVal' from incompatible pointer type
    100 syntax error at end of input


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define    pi 3.146
    #define    brakeFactor 0.7
    double readVal (char *carModel,double rSlope, double sLength);
    double calculateSpeed (double rSlope, double sLength);
    
    int main ()
    {
        double all[300], pass[300], slopeRad, slopeTangent, speedArray[200]={0}, sum=0, speed, sLength, sLengthArray[200]={0},rSlope,rSlopeArray[200]={0}; 
        char carModel[1000], carModelArray[200][1000],*carModelP;
        int count=0, i; 
        carModelP=carModel;
        
        
        while (sLength != 999)
        {
          for (i=0; i<2; i++);
          {
           all[i]= readVal(&carModel,rSlope,sLength);
          }
          sLength = all[0];
          if (sLength == 999)
          {
             break;
          }
          else
          {
           rSlope = all[1];
           speed = calculateSpeed(rSlope,sLength);
           sum = sum + speed;
           count = count + 1;
           for (i=0;i<count;i++)
            {
                sLengthArray[count]= sLength;
                rSlopeArray[count]= rSlope;
                strcpy(carModelArray[count],carModel);
                speedArray[count]= speed;
            }
           }
           printf ("|\n");
           printf ("|\tHISTORICAL DATA FROM SPEEDING CARS\n");
           printf ("|\tSkidlength\tRoadSlope\tSpeed\n");
           for (i=1; i<=count; i++)
           {
               printf("|\t%.3lf\t\t%0.lf\t\t%.2lf\n", sLengthArray[i], rSlopeArray[i], speedArray[i]);
           }
           printf ("|\n");
           system ("pause");
           return 0;
    }
    
    
    
    double readVal (char *carModel,double rSlope, double sLength)
    {
          double all [300];
          int i;
          printf ("Please enter the car model.\n");
          scanf ("%s", *carModel);
          do
          {
      			printf ("Please enter a skid length.If 999 is entered, no calculations will be done\n");
    			scanf ("%lf", &sLength);
          }
          while(sLength <0);
          if (sLength == 999)
          {
             all[0]= sLength;
             return all[0];         
          }
          else
          {              
            do
            {
      			printf ("Please enter a road slope between -30 and 30.\n");
                scanf ("%lf", &rSlope);
            }
            while (rSlope<-30||rSlope>30);
          }
          all[0]=sLength;
          all[1]=rSlope;
          for (i=0; i<2; i++);
          {
           return all[i];
          }
          
    }
    
    
    double calculateSpeed (double rSlope, double sLength)
    {
         double slopeRad, slopeTangent, speed;
         slopeRad = rSlope*(pi/180);
         slopeTangent= sin(slopeRad)/cos(slopeRad);
         speed = sqrt(30*sLength*(slopeTangent + brakeFactor));
         return speed;
    }
    Someone help me please

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. readVal expects a pointer to char, you give it a pointer to an array of char. Lose the &.

    2. My gcc tells me "nested functions are disabled", meaning you probably didn't ever close your main function.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    1. readVal expects a pointer to char, you give it a pointer to an array of char. Lose the &.

    2. My gcc tells me "nested functions are disabled", meaning you probably didn't ever close your main function.
    Thank you very much, it solved the problem but now that I got it to compile and run I have another problem.
    The program is suppose to ask for the car information and calculate the speed as long as 999 is not entered for the skid length. Then once the loop ends it's suppose to print the data.
    My problem now is that the loop is not ending and it keeps asking for information even after i enter 999.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define    pi 3.146
    #define    brakeFactor 0.7
    double readVal (char *carModel,double rSlope, double sLength);
    double calculateSpeed (double rSlope, double sLength);
    
    int main ()
    {
        double all[300], pass[300], slopeRad, slopeTangent, speedArray[200]={0}, sum=0, speed, sLength, sLengthArray[200]={0},rSlope,rSlopeArray[200]={0}; 
        char carModel[1000], carModelArray[200][1000],*carModelP;
        int count=0, i; 
        carModelP=carModel;
        
        
        while (sLength != 999)
        {
          for (i=0; i<2; i++);
          {
           all[i]= readVal(carModel,rSlope,sLength);
          }
          sLength = all[0];
          if (sLength == 999)
          {
             break;
          }
          else
          {
           rSlope = all[1];
           speed = calculateSpeed(rSlope,sLength);
           sum = sum + speed;
           count = count + 1;
           for (i=0;i<count;i++)
            {
                sLengthArray[count]= sLength;
                rSlopeArray[count]= rSlope;
                strcpy(carModelArray[count],carModel);
                speedArray[count]= speed;
            }
           }
        }
        printf ("|\n");
        printf ("|\tHISTORICAL DATA FROM SPEEDING CARS\n");
        printf ("|\tSkidlength\tRoadSlope\tSpeed\n");
        for (i=1; i<=count; i++)
        {
          printf("|\t%.3lf\t\t%0.lf\t\t%.2lf\n", sLengthArray[i], rSlopeArray[i], speedArray[i]);
        }
        printf ("|\n");
        system ("pause");
        return 0;
    }
    
    
    
    double readVal (char *carModel,double rSlope, double sLength)
    {
          double all [300];
          int i;
          printf ("Please enter the car model.\n");
          scanf ("%s", &carModel);
          do
          {
      			printf ("Please enter a skid length.If 999 is entered, no calculations will be done\n");
    			scanf ("%lf", &sLength);
          }
          while(sLength <0);
          if (sLength == 999)
          {
             all[0]= sLength;
             return all[0];         
          }
          else
          {              
            do
            {
      			printf ("Please enter a road slope between -30 and 30.\n");
                scanf ("%lf", &rSlope);
            }
            while (rSlope<-30||rSlope>30);
          }
          all[0]=sLength;
          all[1]=rSlope;
          for (i=0; i<2; i++);
          {
           return all[i];
          }
    }
    
    double calculateSpeed (double rSlope, double sLength)
    {
         double slopeRad, slopeTangent, speed;
         slopeRad = rSlope*(pi/180);
         slopeTangent= sin(slopeRad)/cos(slopeRad);
         speed = sqrt(30*sLength*(slopeTangent + brakeFactor));
         return speed;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
     sLength = all[0];
    This line of code would be the culprit.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Code:
     sLength = all[0];
    This line of code would be the culprit.
    How would I return the values of the array in readVal and make them available in the main?
    Also, I'm sorry if this is annoying but I started learning programming like last month so this is all new to me and we haven't learnt much yet.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The question is, why do you always want to check the first length, and not the length that was just typed in by the user?

    Also notice that you can only return one value from the function, so you'll have to decide whether you want to return rSlope or sLength (unless you choose to use parameters for output). Notice also that the all[] array in the function is not the same as the one in main.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    The question is, why do you always want to check the first length, and not the length that was just typed in by the user?

    Also notice that you can only return one value from the function, so you'll have to decide whether you want to return rSlope or sLength (unless you choose to use parameters for output). Notice also that the all[] array in the function is not the same as the one in main.
    well in class we were told that in order to return more than 1 value from a module they can be placed in an array and the entire array returned. so the first element of all [] would always stored the slenght. I'm not sure how to return the entire array though.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by J-Camz View Post
    well in class we were told that in order to return more than 1 value from a module they can be placed in an array and the entire array returned. so the first element of all [] would always stored the slenght. I'm not sure how to return the entire array though.
    Well, that's a shame, since it's not actually true. You can approximate it by passing the array into the function:
    Code:
    void aFunction(int anArray[100]) {
        anArray[0] = 9;
    }
    Here the array that is passed into the function is modified, and those modifications can be seen where the function was called from. But an array cannot be returned.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    our teacher mentioned passing by reference so i did some research and attempted something but the program keeps asked for the slope instead of ending so i highly doubt i got it right.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define    pi 3.146
    #define    brakeFactor 0.7
    double readVal (char *carModel,double *rSlope, double *sLength);
    double calculateSpeed (double rSlope, double sLength);
    
    int main ()
    {
        double all[300], slopeRad, slopeTangent, speedArray[200]={0}, sum=0, speed, sLength, *sLengthP, sLengthArray[200]={0},rSlope, *rSlopeP,rSlopeArray[200]={0}; 
        char carModel[1000], carModelArray[200][1000],*carModelP;
        int count=0, i; 
        carModelP=carModel;
        sLengthP = &sLength;
        rSlopeP = &rSlope;
        
        
        while (sLength != 999)
        {
          readVal(carModel,&rSlope,&sLength);
          if (sLength == 999)
          {
             break;
          }
          else
          {
           speed = calculateSpeed(rSlope,sLength);
           sum = sum + speed;
           count = count + 1;
           for (i=0;i<count;i++)
            {
                sLengthArray[count]= sLength;
                rSlopeArray[count]= rSlope;
                strcpy(carModelArray[count],carModel);
                speedArray[count]= speed;
            }
           }
        }
        printf ("|\n");
        printf ("|\tHISTORICAL DATA FROM SPEEDING CARS\n");
        printf ("|\tSkidlength\tRoadSlope\tSpeed\n");
        for (i=1; i<=count; i++)
        {
          printf("|\t%.3lf\t\t%0.lf\t\t%.2lf\n", sLengthArray[i], rSlopeArray[i], speedArray[i]);
        }
        printf ("|\n");
        system ("pause");
        return 0;
    }
    
    
    
    double readVal (char *carModel,double *rSlope, double *sLength)
    {
          double all [300];
          int i;
          printf ("Please enter the car model.\n");
          scanf ("%s", &carModel);
          do
          {
      	               printf ("Please enter a skid length.If 999 is entered, no calculations will be done\n");
    			scanf ("%lf", &sLength);
          }
          while(sLength <0);            
          do
          {
      	    printf ("Please enter a road slope between -30 and 30.\n");
                scanf ("%lf",&rSlope);
         }
         while (*rSlope<-30||*rSlope>30);
    }
    
    double calculateSpeed (double rSlope, double sLength)
    {
         double slopeRad, slopeTangent, speed;
         slopeRad = rSlope*(pi/180);
         slopeTangent= sin(slopeRad)/cos(slopeRad);
         speed = sqrt(30*sLength*(slopeTangent + brakeFactor));
         return speed;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should be getting warnings here -- since (in your readVal function) sLength is a double *, &sLength is a double **, which can't be used in the scanf as a double *. None of your parameters-via-pointer need a & when used with scanf.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    You should be getting warnings here -- since (in your readVal function) sLength is a double *, &sLength is a double **, which can't be used in the scanf as a double *. None of your parameters-via-pointer need a & when used with scanf.
    THANK YOU SO MUCH FOR BEING SO PATIENT AND HELPFUL ! I GOT IT TO WORK.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find Injected DLLs In A Process?
    By pobri19 in forum Windows Programming
    Replies: 35
    Last Post: 02-06-2010, 09:53 AM
  2. Touchkit xorg module error
    By thesourcehim in forum Linux Programming
    Replies: 0
    Last Post: 10-06-2008, 06:54 AM
  3. Merge Module Problems
    By mercury529 in forum Windows Programming
    Replies: 0
    Last Post: 11-29-2006, 03:30 PM
  4. Function basics
    By sjleonard in forum C++ Programming
    Replies: 15
    Last Post: 11-21-2001, 12:02 PM