Thread: Car taxation problem

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    2

    Unhappy 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
    Last edited by qwerty96; 04-05-2016 at 02:59 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    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.

    2. Add a condition which reads along the lines of
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2016
    Posts
    2
    Quote Originally Posted by Salem View Post
    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.

    2. Add a condition which reads along the lines of
    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");
    
    norecs = read_records("data.txt", car);
    
    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. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    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
    Last edited by whiteflags; 04-06-2016 at 02:11 AM. Reason: variable renaming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM