Thread: C error?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    C error?

    I'm almost done this code, but I don't know why there is an error...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define FILENAME "chill.txt"
    
    /* Define the structure */
    struct windchill
    {
          int NROWS, NCOLS;
          float NCOLStemp[17], NROWSspeed[21], matrix[21][17];
    }
    
    /* FUNCTION PARAMETERS */
    float get_airTemp(float airTemp);
    float get_windSpeed(float windSpeed);
    float get_windChillTemp(struct windchill w1, int fileRow, int fileCol);
    
    int main()
    {
          struct windchill w1;
          int i, j, fileRow, fileCol, quit = 0;
          int colCtr, rowCtr, matrixRowCtr, matrixColCtr;
          float windChillTemp;
          float airTemp, windSpeed;
          
          /* Create a pointer of type file */
          FILE *windChillFactorPtr;  
          
          w1.NROWS = 21;
          w1.NCOLS = 17;
          
          /* Open the file */
          windChillFactorPtr = fopen(FILENAME, "r");
    
          /* If file exists, run the program */
          if(windChillFactorPtr != NULL)
          {
                printf("The file %s exists.\n", FILENAME);             
                
                while(quit != 1)
                {
                        /* Run functions to get the wind speed and temperature */
                        get_airTemp(airTemp);
                        get_windSpeed(windSpeed);
    
                        /* Read values from air temperature into column array*/            
                        for(j=0; j <= w1.NCOLS - 1; j++)
                        {
                              fscanf(windChillFactorPtr,"%f", &w1.NCOLStemp[j]);
                        }
                        /* Read values from wind speed into row array */
                        for(i=0; i<= w1.NROWS - 1; i++)
                        {
                              fscanf(windChillFactorPtr,"%f", &w1.NROWSspeed[i]);
                        }
                        /* Fill in the matrix */
                        for(i =0; i <= w1.NROWS -1; i++)
                        {
                              for(j =0; j <= w1.NCOLS -1; j++)
                              {
                                    fscanf(windChillFactorPtr,"%f", &w1.matrix[i][j]);
                              }
                        }
    
                        /* Close the file */
                        fclose(windChillFactorPtr);                  
                  
                  /* Call on function get_windChillTemp */
                  windChillTemp = get_windChillTemp(w1, fileRow, fileCol);
                  
                  /* Print the final answer */
                  printf("Wind Chill Temperature = %f\n\n", windChillTemp);      
                  
                  /* To quit, have user enter 1 */
                  printf("Press any key to continue, 1 to quit.\n");
                  scanf("%d", &quit);
                }
          }
          
          /* Otherwise, print an error message and quit the program. */
          else
          {
              printf("Error occured when opening the file.");
          }
    
    
          system("PAUSE");
          return 0;
    }
    /*-----------------------------------------------------*/
    /* A function to get the air temperature from the user */
    float get_airTemp(float airTemp)
    {
          printf("Enter the air temperature: \n");
          scanf("%f", airTemp);
    
          return airTemp;
    }
    /*-----------------------------------------------------*/
    /*    A function to get the wind speed from the user   */
    float get_windSpeed(float windSpeed)
    {
          printf("Enter the wind speed: \n");
          scanf("%f", windSpeed);
          return windSpeed;
    }
    
    float get_windChillTemp(struct windchill w1, int fileRow, int fileCol)
    {     
          int k, l, finalK, finalL;
          int airTemp, windSpeed;
          float changeInTemp, windChillTemp;
          
          w1.NROWS=21;
          w1.NCOLS=17;
          
          /* Get air temperature and wind speed from functions above */
          airTemp = get_airTemp(airTemp); 
          windSpeed = get_windSpeed(windSpeed);
      
          /* Round off air temperature and wind speed to a value in the file */
      
          for(k=0; k <= w1.NCOLS; k++)
          {         
                    if(airTemp <= w1.NCOLStemp[k] && airTemp >= w1.NCOLStemp[k+1])
                    {
                                  finalK = k;
                    }
          }
      
          for(l=0; l <= w1.NROWS; l++)
          {
                    if(windSpeed >= w1.NROWSspeed[l] && windSpeed <= w1.NROWSspeed[l])
                    {
                                  finalL = l;
                    }
          } 
      
          /* List all possible options for rounding of numbers in the matrix */
      
          if(abs(w1.NCOLStemp[finalK] - airTemp) < abs(w1.NCOLStemp[finalK+1] - airTemp) && abs(w1.NROWSspeed[finalL] - airTemp) < abs(w1.NROWSspeed[finalL+1] - airTemp))
          {
                    changeInTemp = w1.matrix[finalK][finalL];
          }
          else if(abs(w1.NCOLStemp[finalK] - airTemp) > abs(w1.NCOLStemp[finalK+1] - airTemp) && abs(w1.NROWSspeed[finalL] - airTemp) < abs(w1.NROWSspeed[finalL+1] - airTemp))      
          {
                    changeInTemp = w1.matrix[finalK+1][finalL] ;
          }
          else if(abs(w1.NCOLStemp[finalK] - airTemp) < abs(w1.NCOLStemp[finalK+1] - airTemp) && abs(w1.NROWSspeed[finalL] - airTemp) > abs(w1.NROWSspeed[finalL+1] - airTemp))
          {
                    changeInTemp = w1.matrix[finalK+1][finalL+1];
          }
          else                                       
          {
                    changeInTemp = w1.matrix[finalK][finalL+1];
          }
      
          /* To find wind chill temperature */
          windChillTemp = airTemp - changeInTemp;
          return windChillTemp;
    
      system("PAUSE");	
    }
    /*-----------------------------------------------------*/
    The error is in line 15:

    Code:
    float get_airTemp(float airTemp);
    It says: two or more data types in declaration of 'get_airTemp'

    There is also an error without a line number, that says:

    [Build Error] n\make.exe ***[Untitled1.o] Error 1


    Does anyone know how to get rid of this?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >struct windchill
    >{
    >      int NROWS, NCOLS;
    >      float NCOLStemp[17], NROWSspeed[21], matrix[21][17];
    >}
    I think you need a semicolon at the end.
    Code:
    struct windchill
    {
          int NROWS, NCOLS;
          float NCOLStemp[17], NROWSspeed[21], matrix[21][17];
    };

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
          return windChillTemp;
    
      system("PAUSE");	
    }
    That system() will never be executed, as it falls right after a return statement.

    Code:
          int k, l, finalK, finalL;
    
          /* ... */
    
          for(k=0; k <= w1.NCOLS; k++)
          {         
                    if(airTemp <= w1.NCOLStemp[k] && airTemp >= w1.NCOLStemp[k+1])
                    {
                                  finalK = k;
                    }
          }
    
          /* ... */
    
          if(abs(w1.NCOLStemp[finalK] - airTemp) < abs(w1.NCOLStemp[finalK+1] - airTemp) /* ... */
    What happens if that conditional in the for loop is never executed, and finalK is not initialized? You'll probably get a seg fault. Can you guarantee that finalK will always get a (valid) value? (I haven't looked closely enough at your code to find out.)

    Code:
    float get_windSpeed(float windSpeed)
    {
          printf("Enter the wind speed: \n");
          scanf("&#37;f", windSpeed);
          return windSpeed;
    }
    There's no need to pass in windSpeed. This would work as well.
    Code:
    float get_windSpeed(void)
    {
          float windSpeed;
          printf("Enter the wind speed: \n");
          scanf("%f", windSpeed);
          return windSpeed;
    }
    As far as I can see, the code could call fclose() more than once, but fopen() is only executed once. Using an fclose()'d file is a very bad idea.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf("&#37;f", windSpeed);

    you should pass pointer to the var to scanf... otherwise you are asking for crash
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM