1. ## Car taxation problem

Hi I was asked to develop a C program and I was trying to do it for ages and whatever I do doesn't work I would really appreciate your help as I'm doing programing just for few weeks

I have biggest trouble with reading in the file and printing it

Here is what I was asked to do

Exercise 1: Car Registration
The file “indata.txt” contains pieces of information about a car, In each case, the first number denotes the year of fabrication, the second text the car’s number in format (year, county, number), the third piece of information is the color and the fourth – car’s engine volume in litres.

develop a program which will read the data from this file into an array of structures of type car_type (see below),
insert
Code:
```typedef struct car
{
int year;
char number[9];
char colour[10];
float engine;
} car_type;```
and for each of the cars calculate and output the corresponding tax to be paid using the following algorithm:

For cars no older than 5 years, the tax is Euro 150/year, if the engine is less and equal to 1.6 litres and Euro 300/year otherwise.

For older cars there are three levels: less than 1.4 litres, less than 1.6 litres but greater than 1.4 litres and greater or equal to 1.6 litres.
The tax is Euro 200 for the smallest engine cars, Euro 400 for the average engine size cars and Euro 600 for the highest engine size cars, respectively.

Test the program and record the test results.
Exercise 2: Car Taxation
Update the car_type structure to store the tax as well computed as above and write the output both on the screen and on a file using file-related functions. Use a user menu. Test the program by adding new lines to the “data.txt” file and record the test results.

“data.txt”
1991 91D2134 red 1.8
1999 99D2939 blue 1.5
2003 03C2334 red 1.2
2006 06G1123 white 1.0
1998 98D5564 blue 1.6
2005 05D1354 green 1.3

2. Show us what you've tried so far so we can see what you're doing wrong. Try just reading in the data and printing it.

3. Read this -> A development process

Here are your first two steps.

1. Read the file into your structure, then print it out using printfs. Until you can read and print successfully, any attempt at other parts of the problem will fail, because they'll be dealing with garbage data.

Code:
```if ( car.year >= (now-5) ) {
printf("new car\n");
} else {
printf("old car\n");
}```
Use functions in time.h to figure out what the current year is.

When you can reliably test the age of each car, then you move onto the next bullet point in your requirements.

Rinse and repeat until the whole program is complete.

4. Originally Posted by Salem
Read this -> A development process

Here are your first two steps.

1. Read the file into your structure, then print it out using printfs. Until you can read and print successfully, any attempt at other parts of the problem will fail, because they'll be dealing with garbage data.

Code:
```if ( car.year >= (now-5) ) {
printf("new car\n");
} else {
printf("old car\n");
}```
Use functions in time.h to figure out what the current year is.

When you can reliably test the age of each car, then you move onto the next bullet point in your requirements.

Rinse and repeat until the whole program is complete.

How can I read in the file I was trying to do it this way cause we got something similar in lectures but it just doesn't work.

Code:
```#include <stdio.h>
#include <stdlib.h>

/* structured type definition */
typedef struct car
{
int year;
char number[9];
char colour[10];
float engine;
} car_type;

int read_records (char* file_name, car_type car[]);

void print_results (car_type car[], int size);

/* main function */
int main()
{
car_type car[5000];

printf("Welcome to the program!\n");

print_results(car, norecs);

printf("Goodbye!\n");

return (EXIT_SUCCESS);
}
int read_records (char* file_name, car_type car[])
{
FILE* fp;
int norecs = 6;
char ch;

/* open file */
fp = fopen(file_name, "r");

while(!feof(fp))
{

fscanf(fp, "%d%c%d%c%d%c%d%c",&car[norecs].year, &ch,&car[norecs].number, &ch,&car[norecs].colour, &ch, &car[norecs].engine, &ch,);
norecs++;
}
/* close file */
fclose(fp);

}```

5. Well, I'm not sure what you were taught but the format string doesn't match your data at all.

Remember we're trying to fill this car type, and it's not that complex:
Code:
```typedef struct car
{
int year;
char number[9];
char colour[10];
float engine;
} car_type;

car_type dummy;
while (fscanf(fp, "%d %8s %9s %f", &dummy.year, dummy.number, dummy.color, &dummy.engine) == 4)
{
/* do stuff with the data */
car[norecs++] = dummy;
}```
Among the errors made were trying to give the address of a string to scanf, when you don't actually need to do that, and you were taught to use feof(fp) even though that's not how anyone reads files in C. FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com