Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming compiling syntax errors

  1. May 13, 2012 #1
    Got some errors when compiling and not sure what is the problem and how to fix it

    Code (Text):
    # include<stdio.h>
    #define FLEETSIZE 10
     
    typedef struct
        {
            int day, month, year;
        }Date_t;
     
    typedef struct
        {
            char make[15];
            Date_t manufactureDate;
            Date_t purchaseDate;
            double purchasePrice;
        }Car_t;
     
    void initFleet(Car_t fleet[]);
    void showDate(Date_t dateToPrint, int whichDate);
    double getPurchasePrice();
    void addCar(Car_t *carToAdd, int *currentCar);
    void clearFleetElement(Car_t *toClear);
    void showCar(Car_t toPrint);
    void showFleet(Car_t fleet[]);
    int getDate(Date_t *toGet, int whichDate);
     
    double getPurchasePrice()
        {
            double price;
            printf("Please enter the purchase price:\n$");
            scanf("%lf", &price);
            return(price);
        }
     
    void addCar(Car_t *carToAdd, int *currentCar)
        {
            printf("\n\nEnter car make:\n");
            scanf("%s", carToAdd->make);
            getDate(&(carToAdd->manufactureDate), 1);
            getDate(&(carToAdd->purchaseDate), 0);
            (carToAdd->purchasePrice) = getPurchasePrice();
            currentCar++;
        }
     
     
     
    void clearFleetElement(Car_t *toClear)
        {
            toClear->make[0] = 0;
            toClear->manufactureDate.day = 0;
            toClear->manufactureDate.month = 0;
            toClear->manufactureDate.year = 0;
            toClear->purchaseDate.day = 0;
            toClear->purchaseDate.month = 0;
            toClear->purchaseDate.year = 0;
            toClear->purchasePrice = 0;
        }
     
    void showDate(Date_t dateToPrint, int whichDate)
        {  
            if (whichDate==0)
            printf("%d/%d/%d", dateToPrint.day, dateToPrint.month, dateToPrint.year);
            if (whichDate==1)  
            printf("%d/%d/%d", dateToPrint.day, dateToPrint.month, dateToPrint.year);
        }
     
    void showCar(Car_t toPrint)
        {
            printf("Car Make:\t\t\t\t%s", toPrint.make);
            printf("\nManufacture Date:\t\t\t");
            showDate(toPrint.manufactureDate, 1);
            printf("\nPurchase Date:\t\t\t\t");
            showDate(toPrint.purchaseDate, 0);
            printf("\nPurchase Price:\t\t\t\t$%.2lf", toPrint.purchasePrice);
        }
     
     
     
    void showFleet(Car_t fleet[])
       {
          int i;
          for(i=0; i<FLEETSIZE; i++)
          {
             printf("\n\nCar number: %d\n", (i+1));
             showCar(fleet[i]);
          }
       }
     
     
    int getDate(Date_t *toGet, int whichDate)
       {
           int dayCheck=0, monthCheck=0, yearCheck=0;
           if (whichDate==0)
           printf("\n\nPlease enter date of purchase:");
           if (whichDate==1)  
           printf("\n\nPlease enter date of manufacture:");
           do
               {
                   printf("\nEnter the date:\nYear:");
                   scanf("%d", &toGet->year);
                   if(toGet->year < 2011 && toGet->year > 1900)
                       yearCheck = 1;
                   else
                       printf("Invalid selection, please re-enter");
                }while(!yearCheck);
           do
               {
                   printf("\nEnter the date:\nMonth:");
                   scanf("%d", &toGet->month);
           if(toGet->month <13 && toGet->month > 0)
               monthCheck = 1;
           else
               printf("Invalid selection, please re-enter");
                }while(!monthCheck);
           do
               {
                   printf("\nEnter the date:\nDay:");
                   scanf("%d", &toGet->day);
                   if(toGet->day <31 && toGet->day >1)
                       dayCheck = 1;
                   else
                       printf("Invalid selection, please re-enter");
                }while(!dayCheck);
           return 0;
        }
     
    void initFleet(Car_t fleet[])
        {
            int i=0;
            for(i=0; i<FLEETSIZE; i++)
                {
                    clearFleetElement(&fleet[i]);
                }
        }
     
    readFleet(Car_t toRead[])
        {
            FILE *fFleet;
            fFleet = fopen("Fleet_File.bin", "rb");
            fread(toRead,10*sizeof(Car_t),1,fFleet);
            fclose(fFleet);
        }

    saveFleet(Car_t toSave[])
        {
            FILE *fFleet;
            int i;
            fFleet = fopen("Fleet_File.bin", "wb");
            fwrite(toSave, 10*sizeof(Car_t), 1, fFleet);
            fclose(fFleet);
        }

    int main()
        {
            Car_t fleet[FLEETSIZE];
            int currentCar = 0, menuSelectInteger = 0, menuLoopControl = 1;
            initFleet(fleet);
            do
                {
                    printf("\n\nMain menu:\n\n");
                    printf("Options:\n\n");
                    printf("1. Add a car to fleet.\n");
                    printf("2. Delete last car from the fleet.\n");
                    printf("3. Display full fleet listing.\n");
                    printf("4. Save the fleet to a file.\n");
                    printf("5. Read the fleet from a file.\n");
                    printf("0. Exit the program\n");
                    printf("\nPlease enter your choice number:\n\n");
                    scanf("%d", &menuSelectInteger);
                    switch(menuSelectInteger)
                    {
                        case 1:
                        addCar(&fleet[currentCar], &currentCar);
                        currentCar++;
                        printf("Current car: %d", currentCar);
                        break;
                        case 2:
                        if (currentCar == 0)
                            printf("No further cars to delete from fleet(currentCar=%d)\n", currentCar);
                            else
                                {
                                    clearFleetElement(&fleet[(currentCar-1)]);
                                    currentCar--;
                                }
                        break;
                        case 3:
                        printf("Option 3 selected");
                        showFleet(fleet);
                        break;
                        case 4:
                        saveFleet(fleet);  
                        printf("\n%d\n", menuSelectInteger);
                        break;
                        case 5:
                        readFleet(fleet);
                        currentCar=FLEETSIZE;
                        printf("\n%d\n", menuSelectInteger);
                        break;
                        case 0:
                        printf("Thank you for using this program!");
                        menuLoopControl=0;
                        break;
                        default:
                        printf("Invalid selection. Please make a selection between 1 and 5, or enter 0 (zero) to exit.");
                        break;
                    }
                }while(menuLoopControl);
            system("pause");
            return 0;
     
    }
     
    Last edited by a moderator: May 14, 2012
  2. jcsd
  3. May 13, 2012 #2

    jtbell

    User Avatar

    Staff: Mentor

    Please tell us what those error messages are and which lines of your code they apply to.
     
  4. May 13, 2012 #3
    ERROR: syntax error before or at line 136 in file
    ERROR: syntax error before or at line 139 in file
    ERROR: syntax error before or at line 141 in file
    ERROR: syntax error before or at line 144 in file
    ERROR: syntax error before or at line 145 in file
    ERROR: syntax error before or at line 148 in file
     
  5. May 13, 2012 #4
    You forgot data types on the readFleet and saveFleet functions (you probably meant to put void).
     
  6. May 13, 2012 #5
    oh thanks so much
    but how come i cant save or read to the file?
     
  7. May 13, 2012 #6
    ERROR: memory of array buf in fread(buf,size,count,stream) is less than the specified input stream at line 139 in file
     
  8. May 13, 2012 #7
    I'm not sure. Is this a runtime error or a compile-time error? It looks like you are trying to read data that exceeds the size of the buffer, but I don't see how that is happening in your code. I tried googling the error message and didn't come up with any hits so I'm a little perplexed as to where this error message is coming from.
     
  9. May 13, 2012 #8
    im not sure im still confused about that problem
     
  10. May 13, 2012 #9
    http://www.cplusplus.com/reference/clibrary/cstdio/fread/


    Here's yours
    Code (Text):
    fread(toRead,10*sizeof(Car_t),1,fFleet);
    Do you mean to be reading one element with a size of 10*sizeof(car_t), or do you mean to read 10 elements of size Car_t?
     
  11. May 13, 2012 #10
    what im trying to do is fleet can hold up to 10 cars that can be saved and read on the file. Is it that I have to check for the file I/O for fopen() returning a NULL pointer?
     
  12. May 13, 2012 #11
    I think maybe you should try this

    Code (Text):
    fread(toRead, sizeof(Car_t), 10, fFleet);
    instead of this
    Code (Text):
    fread(toRead, 10*sizeof(Car_t), 1, fFleet);
     
    Last edited: May 13, 2012
  13. May 14, 2012 #12
    unfortunately it is not working and still displaying the error message
     
  14. May 14, 2012 #13

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    That shouldn't make any difference. It's just a convenience for the programmer that fread gives you two parameters for "how many things to read" and "the size of each thing". It just multiples them together to get "how much data to read".

    The error message looks very strange (and it's close to the boundary of not even being in English IMO). What compiler, IDE, and operating system are you using?

    FWIW the read and write optiosn worked fine for me using a Microsoft compiler on Windows.
     
  15. May 14, 2012 #14
    im using a compiler called ChIDE on the mac OS is that a problem?
     
  16. May 14, 2012 #15

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Looking at the ChIDE website, they seem to have written their own C/C++ interpreter.
    They don't seem to claiim it follows any particular standards, so I gues you can't complain if it doesn't work the same way as ANSI standard C.
     
  17. May 15, 2012 #16
    oh i didn't realise..so what is your recommendation for a compiler?
     
  18. May 15, 2012 #17

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Sorry, I'm not a Mac user. Code::Blocks is a nice simple IDE on Windows, and their site says there is a Mac version, Code::Blocks doesn't include its own compiler, so check out the Code::Blocks website to see what Mac C/C++ compilers it can use.
     
  19. May 15, 2012 #18
    i tried code blocks on windows and it doesnt compile when i clicked on it
     
    Last edited: May 16, 2012
  20. May 18, 2012 #19
    i got the compiling to work but read and save to file still dont work on code blocks any suggestions?
     
    Last edited: May 18, 2012
  21. May 18, 2012 #20
    CodeBlock is an IDE which needs a compiler for your source files to work with, you can go to Settings -> Compiler and Debugger to see if any compiler is being selected.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C programming compiling syntax errors
  1. C Programming Compiler (Replies: 11)

Loading...