update on vehicle registration

Hi all! So, the other day I posted asking for help on writing a program to meet the following criteria:
Problem: Write an application in C++ that calculates the vehicle registration fee.

Input: The program should read the required inputs from a file to calculate the fee. You can use the attached file as your input (VehicleInput.txt). Allow the program to read multiple inputs from the file using while loop and eof() function.
Output: A breakdown of the registration fee and the total registration fee for each vehicle. Output should be properly formatted. Allow the program to save the output for all vehicles in a single text file.
Rules for fee calculation:
All vehicles will have a VIN number, make, model, and year. Trucks will also have weight as the input.
All new cars and SUVs will have a base fee of $100.00, buses will have a base fee of $200.00, and trucks will have a base fee of $500.00.
Trucks above 12,000 LBS will have a surcharge of 22% added to the base fee. This will make up the new base fee for the trucks above 12,000 LBS.
Depending upon the age of the vehicle, base fee will be reduced by 10% for each year up to a maximum of 7 years. For example if the year of the vehicle is 2015, then base fee will reduce by 50% (2020 – 2015 = 5 Years and 10% for every year).
There is a 6.5% tax added to the base fee.
There is $2.00 additional highway fund that is added to the fee.
Submission (One MS Word document containing the following deliverables):
Algorithms
An algorithm for every function.
Constants
All constants defined for this application.
Source Code
Implement above application in C++ and include your source code under this section.
Input File Contents
Copy the contents of your input file under this section. Your input file should contain at least 5 vehicles. See VehicleInput.txt file.
Output File Contents
Copy the contents of your output file under this section
* Please be sure to follow good coding practices (modular code with pre-defined and user defined functions, comments, indentations, readable code)
I took a lot of the suggestions that were made and looked back at previous codes that I have written as well. Here is the code that I have come up with now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #include<iostream>
#include<fstream>
#include<string>
using namespace std;

int basefee(bool type[], double fee);

int year{}, weight{};
string vin, brand, model, type;
double fee{};

int main()
{
    ifstream inFile;
    inFile.open("VehicleInput.txt");

    while (!inFile.eof())
    {
        inFile >> vin >> brand >> model >> year >> type >> weight;
        string type[4] = {"car","suv","bus","truck"};
        void basefee();
        double yeardisc = 2020 - year;
        if (int year <= 2020)
            fee = basefee() - (.1 * (yeardisc));
        else if (yeardisc <= 7)
            fee = basefee() - (.1 * (yeardisc));
        else fee = basefee();

    }

    ofstream outFile;
    outFile.open("VehicleRegFee.txt");

   
}

//calculates basefee based on type
int basefee(bool type[], double fee)
{
    if (type[0] = "car")
        fee = 100;
    else if (type[1] = "suv")
        fee = 100;
    else if (type[2] = "bus")
        fee = 200;
    else if (type[3] = "truck")
        fee = 500;
    else fee = 0;
}


Any pointers on what needs improvement?
your function is of no use. If it even compiles and runs, it just sets type[0] to "car".


fee is not passed by reference, so it won't be saved there for the caller.
and the int function has no returns.

why is it an array? I can't make much sense of the array part.
comparison is ==, assignment is =. You can assign in a condition, but it is almost always wrong to do that....

you compare bools and strings. strings are always true, or your strings are, not sure if empty string is false or not (could be, I don't know and don't need to know right now).

there is a great deal more nonsense here. line 21. line 27. line 33 (what to do with that opened file?!). line 9 (global variables, some over-ridden in other places like type). Local variables inside the loop (this is legal but a little unusual).

1
2
3
4
5
6
7
8
9
consider?
int basefee(string type)
{
    if(type == "car") return 100;
    if(type == "suv") return 100;
    if(type == "bus") return 200;
    ...
    return 0; //default. 
}


You need to slow down and revisit what you know about c++ syntax. This is too complicated to be a first program, but it kind of looks like a first cut at your first program due to all the syntax mistakes.
Last edited on
Hello kmcfall,


Any pointers on what needs improvement?


All of it.

I thought you might have learned something from the first post http://www.cplusplus.com/forum/beginner/274801/ , but apparently not.

The code above does not compile. It gives me 9 errors.

The function prototype/forward declaration and function definition do match, but the function call does not. And why did you feel the need to change "type", defined as a global variable, a string to an array of "bool"s?

The original input file has the vehicle type in all capital letters and you are trying to compare that to all lower case letters.

The while loop does not work. the first line is wanting to read 5 fields from the input file, but not all lines have 5 fields. Unless the input file has changed and you did not tell us.

Working off your first code I came up with this output:

 The fee for a CAR   12 years old is:  38.50
 The fee for a SUV    6 years old is:  48.50
 The fee for a BUS    7 years old is: 145.00
 The fee for a TRUCK 11 years old is: 574.50
 The fee for a CAR   15 years old is:  38.50



And

 The fee for a CAR   12 years old is:  38.50
 The fee for a SUV    6 years old is:  48.50
 The fee for a bUS    7 years old is:   0.00  Invalid type for bUS. Must be capital letters.
 The fee for a TRUCK 11 years old is: 574.50
 The fee for a cAR   15 years old is:   0.00  Invalid type for cAR. Must be capital letters.


I think this is correct, but I have nothing to compare the numbers to.

With what you have you do not add anything else to the base fee that is or may be needed.

Andy
Hello kmcfall,

This new version of your program does not work. It would be best to scrap it and start over.

Given instructions:

Input: The program should read the required inputs from a file to calculate the fee.
You can use the attached file as your input (VehicleInput.txt). Allow the program to read multiple inputs from
the file using while loop and eof() function.


I would start with this.

First work on opening the file and checking that it is open.

If there is no problem then define your variables. They should not be global variables.

Start by reading the first line and printing it out.

When this works then add the while loop to read the whole file taking into account the line that has the "type" of TRUCK.

When you can achieve the output of:

VIN number         brand  model year type   weight
AB54H77HG553DHJ8J8 TOYOTA CAMRY 2008 CAR 0
C745D4S78SSSWERDDF NISSAN PATHFINDER 2014 SUV 0
WAGGT345ADFGGGS234 ATLANTIC EXPRESS 2013 BUS 0
LKU338NGTH0988J77H KENWORTH T800 2009 TRUCK 20000
TNNH75RDG88J0R6669 FORD FOCUS 2005 CAR 0


You now have something to work with. Then you can work on the function for "BaseFee" and continue on from there.

Andy
Hello Handy Andy. I scrapped the whole code, and decided against using outside functions because that was what I was struggling with the most. Here is what I have come up with so far. I also used the code that you showed me the other day to see if the file would open, and it opens successfully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	ifstream inFile;
	ofstream outFile;

	string VIN, make, model, type, year;
	string truck = "TRUCK";
	double basefee, weight;
	double tax = 0.065;
	double highwayfee = 2.0;
	int age;
	double discount;

	inFile.open("VehicleInput.txt");
	outFile.open("VehicleOutput.txt");

	while (!inFile.eof())
	{
		inFile >> VIN >> make >> model >> type >> year;
		inFile >> weight;
		age = (2020 - year);
		weight = 12000;
		discount = (age * 0.1);

		if (age >= 7)
		{
			discount = 0.7;
		}

		if ((type == "CAR") || (type == "SUV"))
		{
			basefee = (((100.0 * tax) - (100 * discount)) + 100);
		}
		if (type == "BUS")
		{
			basefee = (((200.0 * tax) - (100 * discount)) + 200.0);
		}
		if (type == "TRUCK")
		{
			basefee = (((500.0 * (0.22 + tax)) - (500 * discount)) + 500.0);
		}


		outFile << " " << VIN << " " << make << " " << model << " " << year;
		if (weight > 0) outFile << weight;
		outFile << "$ " << basefee << endl;


	}
	return 0;
}


I'm having issues with the age. I'm currently using Microsoft Visual Studio to compile this, and get error code C2677, meaning that there is no global operator found for "-"
Any pointers?
Last edited on
Topic archived. No new replies allowed.