The classic 'invisible' parse error.

This is a discussion on The classic 'invisible' parse error. within the C Programming forums, part of the General Programming Boards category; I'm getting a parse error on both of the following lines, and I can't figure out why. CAR is typdeffed ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    38

    The classic 'invisible' parse error.

    I'm getting a parse error on both of the following lines, and I can't figure out why. CAR is typdeffed to be a structure, numCars is an integer, and costPtr/wtPtr are both pointers. For the life of me I can't see it, which is why I hope some fresh minds might be able to help! The typedef for struct CAR is below.

    Code:
    FillArray ( CAR train, numCars, float &costPtr, int &wtPtr );
    FindTotals ( CAR train, numCars, float &costPtr, int &wtPtr );
    
    typedef struct car
        {
           int   carNum;
           char  cargo[LENGTH];
           char  origin[LENGTH];
           char  destination[LENGTH];
           int   weight;
           int   miles;
           float cost;
        }CAR;
    Last edited by ChristianTool; 04-09-2004 at 05:14 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>FillArray ( CAR train, numCars, float &costPtr, int &wtPtr );
    Maybe
    >>FillArray ( CAR train, int numCars, float *costPtr, int *wtPtr );

    Make sure the typedef for CAR comes before these prototypes.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    I typdeffed it in the header file, which is # included. I'll try moving it.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Here's the full code.

    Code:
    /*******************************************************************
     * File:              proj3.c
     * Created by:        Chris
     * Created on:        04/07/2004
     * Class:             CMSC201
     * Section:           0204
     * Email:             chrisko1@umbc.edu
     *
     *  Description:      Main file of Project 3, a train report program.
     *******************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "trains.h"
    #include <strings.h>
    
    /* Maximum string length for structure */
    #define LENGTH 12
    
    int main()
    {
       int numCars;
    
       typedef struct car
       {
                int   carNum;
                char  cargo[LENGTH];
                char  origin[LENGTH];
                char  destination[LENGTH];
                int   weight;
                int   miles;
                float cost;
       }CAR;
    
       CAR *train;
    
       PrintGreeting ();
    
       numCars = ReadNumCars ();
    
       train = (CAR *) malloc(numCars * sizeof(CAR));
       
       FillArray ( train,  numCars, &costPtr, &wtPtr );
       FindTotals ( train, numCars, &costPtr, &wtPtr );
    
       
       return 0;
    }
    
    /*******************************************************************
     * File:              train.c
     * Created by:        Chris
     * Created on:        04/06/2004
     * Class:             CMSC201
     * Section:           0204
     * Email:             chrisko1@umbc.edu
     *
     *  Description:      Library file of Project Three containing all
     *                    of the functions used within the program.
     *
     *******************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "trains.h"
    #include <string.h>
    
    #define LENGTH 12
    
    /* Display greeting message */
    /* name: PrintGreeting      */
    /* inputs: none             */
    /* returned value: none     */
    void PrintGreeting ()
    {
       printf( "\nThank you for using Train Report!  This program will");
       printf( "\nread information from the data file, and print out a");
       printf( "\ntrain report on each of those cars, and statistics");
       printf( "\nabout the whole train, followed by a graphical");
       printf( "\nrepresentation of the train.  Enjoy!\n\n");
    }
    
    
    /* Reads number of cars in train */
    /* name: ReadNumCars             */
    /* inputs: none                  */
    /* returned value: none          */
    int ReadNumCars ()
    {
       int cars = 0;
    
       scanf( "%d", &cars);
    
       return cars;
    }
    
    /* Fills the array with items from the data file   */
    /* name: FillArray                                 */
    /* inputs: number of cars                          */
    /* returned value: none                            */
    void FillArray (CAR* train, int numCars, float* costPtr, int* wtPtr)
    {
       int i;
    
       char str1[LENGTH] = "coal";
       char str2[LENGTH] = "cattle";
       char str3[LENGTH] = "hogs";
       char str4[LENGTH] = "eggs";
       char str5[LENGTH] = "autos";
    
       for ( i = 0; i < numCars; i++ )
       {
          scanf("%d", &train[i].carNum);
          scanf("%s", train[i].cargo);
          scanf("%s", train[i].origin);
          scanf("%s", train[i].destination);
          scanf("%d", &train[i].weight);
          scanf("%d", &train[i].miles);
    
    if( strcmp(train[i].cargo, str1) == 0 )
          {
             train[i].cost = 29.1;
          }
          if( strcmp(train[i].cargo, str2) == 0 )
          {
             train[i].cost = 52.5;
          }
          if( strcmp(train[i].cargo, str3) == 0 )
          {
             train[i].cost = 63.1;
          }
          if( strcmp(train[i].cargo, str4) == 0 )
          {
             train[i].cost = 75.9;
          }
          if( strcmp(train[i].cargo, str5) == 0 )
          {
             train[i].cost = 16.5;
          }
    
       }
    
    }
    
    /* Finds the total weight and total cost of the train                     */
    /* name: FindTotals                                                       */
    /* inputs: array of structures, number of cars, cost and weight pointers  */
    /* returned values: none                                                  */
    void FindTotals (CAR* train, int numCars, float* costPtr, int* wtPtr)
    {
       int i;
       int increment;
       int weight = 0;
       float cost = 0;
    
       costPtr = &cost;
       wtPtr = &weight;
    
       for ( i = 0; i < numCars; i++ )
       {
          increment = train[i].cost * train[i].miles * train[i].weight;
          cost = cost + increment;
       }
       i = 0;
    
       for ( i = 0; i < numCars; i++ )
       {
          weight = weight + train[i].weight;
       }
    }
    
    
    /* Prints the train report                                               */
    /* name: PrintReport                                                     */
    /* inputs: array of structures, number of cars, weight and cost pointer  */
    /* returned values: none                                                 */
    void PrintReport (CAR* train, int numCars, float* costPtr, int* wtPtr)
    {
       int i;
    
       printf( "\n\nThere are %d cars in this train.\n", numCars);
       printf( "The data for each car is as follows:\n\n" );
       for ( i = 0; i < numCars; i++ )
       {
          printf( "\n\nCar number  : %d", train[i].carNum );
          printf( "\nCargo       : %s", train[i].cargo );
          printf( "\nOrigin      : %s", train[i].origin );
          printf( "\nDestination : %s", train[i].destination );
          printf( "\nWeight      : %d pounds", train[i].weight );
          printf( "\nDistance    : %d miles", train[i].miles );
          printf( "\nCost        : $  %f", train[i].cost );
       }
    
       printf( "\n\n-------------------------------" );
       printf( "\n-------------------------------\n");
       printf( "\nNumber of Cars :            %d", numCars);
       printf( "\nTotal Weight   :            %d tons", *wtPtr);
       printf( "\nTotal Cost     :  $         %f", *costPtr);
    
    
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The only place you have CAR declared is within your main function. You need to have it in an #include or something to that effect.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    trains.h?

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Yeah, I declared it in trains.h, and got rid of the declaration in main. That, and I declared the pointers in main and removed the & from the function calls, and it compiled = ]. Now there's logic errors to mess with.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    logic errors are always fun. Hope you get them and don't be afraid to ask for more help here.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    I didn't have a properly formatted input data file. Now it seems to be working perfectly! Just have to write the last function and I shall be done.

    Edit: Thantos, you HOPE I get logic errors? Ouch, that's harsh!
    Last edited by ChristianTool; 04-09-2004 at 06:55 PM.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Hope you get them
    Heh a little misleading. I meant "get them" as in find them and fix them.

    But hey getting logic errors builds character.

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >But hey getting logic errors builds character.
    Suddenly my mind just snapped back into 'Calvin & Hobbes' mode.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    "Why is it whenever I build character, he saves $100?"

  13. #13
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    next time use vi

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Vi? Finally finished with the project. Pulled something of an all-nighter to get it done, and much thanks for the help from Ken and Thantos.

  15. #15
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    vi highlights incomplete parens and such....heh
    the unix editor "vi" or linux vim whatever....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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