Jump to content

Module Help!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
J-Camz

J-Camz

    Newbie

  • Members
  • PipPip
  • 20 posts
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


#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
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts

Quote

all[i]= readVal(&carModel,rSlope,sLength);

readVal takes a pointer to char as the first argument, but carModel is already a pointer because array names decay into a pointer to the first element. Take away the & operator.

#3
J-Camz

J-Camz

    Newbie

  • Members
  • PipPip
  • 20 posts
Thank you!
I also figured out the syntax error at the end but now 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.
Can someone help me please.

#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;

}





Edited by J-Camz, 27 November 2008 - 03:23 PM.


#4
birinight

birinight

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
Your code is a mess. :confused:

 for (i=0; i<2; i++);

      {

       all[i]= readVal(carModel,rSlope,sLength);

      }

      sLength = all[0];

      if (sLength == 999)

      {

         break;

      }


Fallow your code:

i=0
all[0] = readVal(carModel,rSlope,sLength); //ask 3 values

i=1
all[1]= all[0] = readVal(carModel,rSlope,sLength); //ask 3 values again

i=3
all[2] = all[0] = readVal(carModel,rSlope,sLength); //ask 3 values again

you are passing rscope, and sLenght as temporary variables. You have to pass the pointer and ask for pointers.

double readVal (char *carModel,double* rSlope, double* sLength) // function declaration

someting = readVal(carModel, &rSlope, &sLength); // passing the pointers when calling the function

You are tring to return 3 values???

for (i=0; i<2; i++);

      {

       return all[i];

      }

when the first return is called the function exits and it can't ever return more values.

There's more but you have to understand some more principles before making a program like this. Try to make more simple programs. You have to learn the difference between pointers and normal variables, and learn the scope of variables.

C Tutorial - Pointers
C Tutorial - Arrays
T223 'C' Tutorial - Variables declaration, inialisation, local, global, static.
Have a look at kpgen at sourceforge
Neither Emacs or Vi are my primary editors...
..and I'm not ashamed!!!