C programming compiling syntax errors

In summary: Check); do { printf("\nEnter the date:\nYear:"); scanf("%d", &toGet->year); if(toGet->year < 2011 && toGet->year > 1900) yearCheck = 1; else
  • #1
dudforreal
116
0
Got some errors when compiling and not sure what is the problem and how to fix it

Code:
# 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:
Technology news on Phys.org
  • #2
Please tell us what those error messages are and which lines of your code they apply to.
 
  • #3
jtbell said:
Please tell us what those error messages are and which lines of your code they apply to.

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
 
  • #4
You forgot data types on the readFleet and saveFleet functions (you probably meant to put void).
 
  • #5
UberGoober said:
You forgot data types on the readFleet and saveFleet functions (you probably meant to put void).

oh thanks so much
but how come i can't save or read to the file?
 
  • #6
ERROR: memory of array buf in fread(buf,size,count,stream) is less than the specified input stream at line 139 in file
 
  • #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.
 
  • #8
UberGoober said:
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.

im not sure I am still confused about that problem
 
  • #9
Code:
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

Parameters
ptr
Pointer to a block of memory with a minimum size of (size*count) bytes.
size
Size in bytes of each element to be read.
count
Number of elements, each one with a size of size bytes.
stream
Pointer to a FILE object that specifies an input stream.

http://www.cplusplus.com/reference/clibrary/cstdio/fread/


Here's yours
Code:
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?
 
  • #10
jreelawg said:
http://www.cplusplus.com/reference/clibrary/cstdio/fread/


Here's yours
Code:
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?

what I am 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?
 
  • #11
dudforreal said:
what I am 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?

I think maybe you should try this

Code:
fread(toRead, sizeof(Car_t), 10, fFleet);
instead of this
Code:
fread(toRead, 10*sizeof(Car_t), 1, fFleet);
 
Last edited:
  • #12
jreelawg said:
I think maybe you should try this

Code:
fread(toRead, sizeof(Car_t), 10, fFleet);
instead of this
Code:
fread(toRead, 10*sizeof(Car_t), 1, fFleet);

unfortunately it is not working and still displaying the error message
 
  • #13
jreelawg said:
I think maybe you should try this

Code:
fread(toRead, sizeof(Car_t), 10, fFleet);
instead of this
Code:
fread(toRead, 10*sizeof(Car_t), 1, fFleet);

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.
 
  • #14
AlephZero said:
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.

im using a compiler called ChIDE on the mac OS is that a problem?
 
  • #15
Looking at the ChIDE website, they seem to have written their own C/C++ interpreter.
We have developed a scripting language environment called Ch as a superset of C with high-level extensions, and salient features from C++ and other languages so that users can learn C language once and use it anywhere for almost any programming purpose.
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.
 
  • #16
AlephZero said:
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.

oh i didn't realize..so what is your recommendation for a compiler?
 
  • #17
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.
 
  • #18
AlephZero said:
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.

i tried code blocks on windows and it doesn't compile when i clicked on it
 
Last edited:
  • #19
i got the compiling to work but read and save to file still don't work on code blocks any suggestions?
 
Last edited:
  • #20
dudforreal said:
i tried code blocks on windows and it doesn't compile when i clicked on it

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.
 
  • #21
phylotree said:
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.

i already fixed that issue thanks... the problem is that the read and save to file option still doesn't work even on Code Blocks
 
  • #22
When you init, you only initialize make[0]. It probably doesn't matter, but why don't you initialize all of the elements.
 
  • #23
read and save to file still not working on code blocks any suggestions?
 

FAQ: C programming compiling syntax errors

What is a syntax error in C programming?

A syntax error in C programming occurs when the code written does not follow the proper syntax or grammar rules of the language. This can include missing or incorrect punctuation, misspelled keywords, or using incorrect data types.

2. How do I fix syntax errors in my C program?

To fix syntax errors, carefully review your code and compare it to the syntax rules of the C programming language. Look for any missing punctuation, misspelled keywords, or incorrect data types. Use a compiler or an IDE to help identify and highlight syntax errors in your code.

3. Why do I keep getting syntax errors when compiling my C program?

Syntax errors can occur when there are mistakes in the code, such as missing semicolons or brackets. These errors prevent the code from being compiled and executed properly. It is important to carefully check the code and fix any syntax errors before attempting to compile it again.

4. Can I ignore syntax errors and still run my C program?

No, syntax errors must be fixed before the code can be compiled and run. Ignoring syntax errors can lead to unexpected results or cause the program to crash.

5. How can I prevent syntax errors in my C program?

To prevent syntax errors, it is important to have a good understanding of the C programming language and its syntax rules. Use an IDE or a code editor that can help identify and highlight potential syntax errors. Also, make sure to carefully review and test your code before attempting to compile it.

Back
Top