# C programming compiling syntax errors

1. May 13, 2012

### dudforreal

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 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)
if (whichDate==1)
do
{
printf("\nEnter the date:\nYear:");
scanf("%d", &toGet->year);
if(toGet->year < 2011 && toGet->year > 1900)
yearCheck = 1;
else
}while(!yearCheck);
do
{
printf("\nEnter the date:\nMonth:");
scanf("%d", &toGet->month);
if(toGet->month <13 && toGet->month > 0)
monthCheck = 1;
else
}while(!monthCheck);
do
{
printf("\nEnter the date:\nDay:");
scanf("%d", &toGet->day);
if(toGet->day <31 && toGet->day >1)
dayCheck = 1;
else
}while(!dayCheck);
return 0;
}

void initFleet(Car_t fleet[])
{
int i=0;
for(i=0; i<FLEETSIZE; i++)
{
clearFleetElement(&fleet[i]);
}
}

{
FILE *fFleet;
fFleet = fopen("Fleet_File.bin", "rb");
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];
initFleet(fleet);
do
{
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");
{
case 1:
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);
break;
case 5:
currentCar=FLEETSIZE;
break;
case 0:
printf("Thank you for using this program!");
break;
default:
printf("Invalid selection. Please make a selection between 1 and 5, or enter 0 (zero) to exit.");
break;
}
system("pause");
return 0;

}

Last edited by a moderator: May 14, 2012
2. May 13, 2012

### Staff: Mentor

Please tell us what those error messages are and which lines of your code they apply to.

3. May 13, 2012

### dudforreal

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. May 13, 2012

### UberGoober

You forgot data types on the readFleet and saveFleet functions (you probably meant to put void).

5. May 13, 2012

### dudforreal

oh thanks so much
but how come i cant save or read to the file?

6. May 13, 2012

### dudforreal

ERROR: memory of array buf in fread(buf,size,count,stream) is less than the specified input stream at line 139 in file

7. May 13, 2012

### UberGoober

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. May 13, 2012

### dudforreal

im not sure im still confused about that problem

9. May 13, 2012

### jreelawg

Here's yours
Code (Text):
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. May 13, 2012

### dudforreal

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?

11. May 13, 2012

### jreelawg

I think maybe you should try this

Code (Text):
Code (Text):

Last edited: May 13, 2012
12. May 14, 2012

### dudforreal

unfortunately it is not working and still displaying the error message

13. May 14, 2012

### AlephZero

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. May 14, 2012

### dudforreal

im using a compiler called ChIDE on the mac OS is that a problem?

15. May 14, 2012

### AlephZero

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.

16. May 15, 2012

### dudforreal

oh i didn't realise..so what is your recommendation for a compiler?

17. May 15, 2012

### AlephZero

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. May 15, 2012

### dudforreal

i tried code blocks on windows and it doesnt compile when i clicked on it

Last edited: May 16, 2012
19. May 18, 2012

### dudforreal

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
20. May 18, 2012

### phylotree

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.