Calculate the registration fee

Okay, so I'm trying to write a code that calculates the registration fee of different vehicles based on a given criteria. I'm supposed to write outside functions and read the data from in file while using the eof() function. Here is what I have so far, but I'm all sorts of confused. Any tips?

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
57
58
  #include<iostream>
#include<fstream>
#include<string>
using namespace std;

double basefee(string x, double y);

string brand, model, type;
char vin;
double year, fee, weight;

int main()
{
	ifstream inFile;

	inFile.open("VehicleInput.txt");

	while (!inFile.eof())
	{
		inFile >> vin >> brand >> model >> year >> type >> weight;
		double basefee();
		cout << basefee();


	}



	return 0;
}

// different criteria for base fee based on type of vehicle
double basefee(string x, double y)
{
	int fee;
	switch (string type)
	{
	case 'car':
		fee = 100
		break;

	case 'suv':
		fee = 100;
		break;

	case 'bus':
		fee = 200;
		break;

	case 'truck':
		fee = 500;
		break;

	default:
		fee = 0;
		break;
	}
}

The input file has the following information:
VIN number brand model year type weight
AB54H77HG553DHJ8J8 TOYOTA CAMRY 2008 CAR
C745D4S78SSSWERDDF NISSAN PATHFINDER 2014 SUV
WAGGT345ADFGGGS234 ATLANTIC EXPRESS 2013 BUS
LKU338NGTH0988J77H KENWORTH T800 2009 TRUCK 20000
TNNH75RDG88J0R6669 FORD FOCUS 2005 CAR

Here are the instructions I was given for the code:
Write an application in C++ that calculates the vehicle registration fee.
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.
Last edited on
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
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

enum type{car, suv, bus, truck}; // <--

double basefee(type x); // <--

string brand, model; // <--
char vin;
double year, fee, weight;

int main()
{
    cout << "Car: $" << basefee(car) << '\n';
    cout << "SUV: $" << basefee(suv) << '\n';
    
    return 0;
}

// different criteria for base fee based on type of vehicle
double basefee(type x)
{
    int fee;
    switch (x)
    {
        case car:
            fee = 101;
            break;
            
        case suv:
            fee = 203;
            break;
            
        case bus:
            fee = 400;
            break;
            
        case truck:
            fee = 800;
            break;
            
        default:
            fee = 0;
            break;
    }
    
    return fee;
}


Car: $101
SUV: $203
Program ended with exit code: 0

Last edited on
Hello kmcfall,

You read from an input, but where is it? And what is in it?

After that the code does not compile, so even if there is an input file to use the program can not be tested.

You define your variables as global variables. This should be avoided unless they are constant as any line of code that follows can change their values and it is hard to track down where it went wrong.

You have opened a file for input, but how do you know it is open?

Your while loop does not work the way that you think it does. Nice of someone to tell you about the ".eof" function, but you should be asking why it was not mentioned on how to properly use it.

As is the read is inside the while loop, so yo would set "eof" and process either the last values read into the variables or empty variables. Before the while condition detects the "eof".

Line 21 is a nice prototype, but it is in the wrong place.

Line 22 the function call is missing some information.

The function promises to return a "double", but the function does not return anything.

A switch works on an integral type variable. That means it need to be an "int" number or a "char". You could also use an "enum" since it is just a number.

You can not use a "std::string" to switch on especially the way that you have tried.

Beyond that without see the instructions that you were given and the input file I do not know what else to tell you yet, but I do have some ideas that could be done differently.

Andy
Handy Andy, I updated my post for more information. These are all topics that I've done before and understood, but I'm getting lost trying to produce the output needed. And .eof functions are difficult when you haven't learned the proper use of them. I really appreciate all the advice you give. It's been really helpful
Last edited on
@OP
Now that this has become the ol' XY problem why not just direct yourself to classes, inheritance and something along the lines of the Factory design pattern.

e.g. https://stackoverflow.com/questions/57307658/what-is-the-difference-between-factory-method-design-pattern-and-bridge-pattern

Note that Car, Suv, truck etc are VehicleTypes and neither int's or <string>'s
To read the file consider which reads the file and displays the records. Note that you don't use .eof(). You use the status of the returned stream from stream extraction to determine whether all data has been correctly processed or not. Note case sensitive.

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
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>

struct Vehicle {
	std::string vin;
	std::string brand;
	std::string model;
	int year {};
	std::string type;
	double weight {};
};

std::istream& operator>>(std::istream& is, Vehicle& veh)
{
	is >> veh.vin >> veh.brand >> veh.model >> veh.year >> veh.type;

	if (veh.type == "TRUCK" && is)
		is >> veh.weight;

	return is;
}

std::ostream& operator<<(std::ostream& os, const Vehicle& veh)
{
	os << std::left << veh.vin << "  " << std::setw(12) << veh.brand << std::setw(15) << veh.model << "  " << veh.year << "  " << veh.type;

	if (veh.type == "TRUCK")
		os << "  " << veh.weight;

	return os;
}

int main()
{
	std::ifstream ifs("vehicleinput.txt");

	if (!ifs.is_open())
		return (std::cout << "Cannot open input file\n"), 1;

	for (Vehicle veh; ifs >> veh; ) {
		std::cout << veh << '\n';
		// Process data here
	}
}



AB54H77HG553DHJ8J8  TOYOTA      CAMRY            2008  CAR
C745D4S78SSSWERDDF  NISSAN      PATHFINDER       2014  SUV
WAGGT345ADFGGGS234  ATLANTIC    EXPRESS          2013  BUS
LKU338NGTH0988J77H  KENWORTH    T800             2009  TRUCK  20000
TNNH75RDG88J0R6669  FORD        FOCUS            2005  CAR


if you really, really have to use .eof(), then for one way consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	std::ifstream ifs("vehicleinput.txt");

	if (!ifs.is_open())
		return (std::cout << "Cannot open input file\n"), 1;

	do {
		Vehicle veh;

		ifs >> veh;
		if (!ifs.eof()) {
			std::cout << veh << '\n';
			// Process data here
		}
	} while (!ifs.eof());
}


However the previous method is the preferred C++ method.
Last edited on
Hello kmcfall,

Thank you for the input file. Very helpful.

As mentioned there is the XY problem. https://xyproblem.info/

I have no idea what you have learned so far and what you can use. Also it helps to mention what IDE/compiler you are using.

The first thing you need to do it work on reading the file and storing the data. Before this part is complete nothing else matters until you have something to work with.

Once you read the file you need to decide on what you are going to store the information in. You have a struct or a class you can work with. If you have not studied classes the struct is sufficient.

Then you have a choice of an array or vector

I revised the start of your code, but still I do not know what you can do, so some of this may not be available to you yet.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

double basefee(string x, double y);

int main()
{
    const std::string inFileName{ "VehicleInput.txt" };  // <--- Put File name here.

    std::ifstream inFile(inFileName);

    if (!inFile)
    {
        std::cout << "\n File " << std::quoted(inFileName) << " did not open." << std::endl;
        //std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;

        return 1;
    }

    int year{}, weight{};  // <--- Moved these here. They do not need to be global variables. Also changed a bit.
    string vin, brand, model, type;
    double fee{};

When first dealing with files I found this code I came up with to be helpful. When opening a file stream for input or output it is a must to check that the file has opened properly. You would not want to continue your program with a file stream that can not be used.

Just because the file stream did not open properly does no mean that it will set the "eof" bit. It is more likely to set the "fail" bit. http://www.cplusplus.com/reference/fstream/ifstream/open/

Since the while loop condition only check the "eof" bit it would evaluate to true even though you can not use the stream since the "fail" bit is set and the stream is unusable.

First I would get the file to open then you could read the first line and display what was read just to check that it works.

I am leaning twords using a struct and array based on what I think you know at this point.

After you get reading the file properly we can work on the while loop.

Andy
Topic archived. No new replies allowed.