2nd update on vehicle registration fee

My previous post, http://cplusplus.com/forum/beginner/274895/ opened my eyes up on some of the code I was writing. I have come up with a new code:

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 highwayfund = 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 - ((100.0 * tax) - (100 * discount)) + highwayfund);
		}
		if (type == "BUS")
		{
			basefee = (200 - ((200.0 * tax) - (200 * discount)) + highwayfund);
		}
		if (type == "TRUCK")
		{
			basefee = (500 - ((500.0 * (0.22 + tax)) - (500 * discount)) + highwayfund);
		}


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


	}
	return 0;
}


However, I keep getting the code C2677 on the line that shows age = 2020-year. How do I fix this? I've looked into it, and I'm supposed to overload the operator, but I don't understand.
@kmcfall

However, I keep getting the code C2677 on the line that shows age = 2020-year. How do I fix this? I've looked into it, and I'm supposed to overload the operator, but I don't understand.


One problem with it is that you declared year to be a string and you are trying to subtract 2 ints, age and year, from it. Might be better to declare the year as an int as well.
Thank you. Is there anyway to stop infinite compilations?
@kmcfall

What do you mean, "infinite compilations" ?
@OP 'infinite compilations' is a mystery to me too but the following might be a little clearer:

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
59
60
61
62
63
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;
    
    string VIN, make, model, type;
    
    int year{0};
    int THIS_YEAR = 2020;
    int age{0};
    
    double basefee{0}, weight{0};
    double tax{0};
    double highwayfund{2};
    double discount{0};
    
    double FEE{0};
    
    inFile.open("VehicleInput.txt");
    outFile.open("VehicleOutput.txt");
    
    while (inFile >> VIN >> make >> model >> year >> type)
    {
        weight = 0;
        
        outFile << VIN << " " << make << " " << model << " " << year << " " << type;
        
        age = THIS_YEAR - year;
        if (age <= 7)
            discount = 0.1 * age;
        else
            discount = 0.7;
        
        if ((type == "CAR") || (type == "SUV"))
            basefee = 100;
        
        if (type == "BUS")
            basefee = 200;
        
        if (type == "TRUCK")
        {
            inFile >> weight;
            outFile << " "  << weight ;
            
            if(weight <= 12000)
                basefee = 500;
            else
                basefee = 500 * 1.22;
        }
        
        tax = basefee * .065;
        FEE = basefee + tax + highwayfund;
        
        outFile << " $" << FEE << endl;
    }
    return 0;
}
Last edited on
PS you might need to use <iomanip> to format the output to 2 decimal places for currency.
In the beginning this is the input file that was provided. http://www.cplusplus.com/forum/beginner/274801/

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



The more updated version of the directions can be found at http://www.cplusplus.com/forum/beginner/274895/

Hello kmcfall,

Sorry for the delay. I saw the post in the other message just as I went to bed.

Looking at the program you start with:
1
2
3
4
5
6
7
string VIN, make, model, type, year;
string truck = "TRUCK";  // <--- Should be defined as a constant.
double basefee, weight;
double tax = 0.065;  // <--- Should be defined as a constant.
double highwayfund = 2.0;  // <--- Should be defined as a constant.
int age;
double discount;

Why did you make year a string? "year" is a number and should be an "int".

I would suggest this:
1
2
3
4
5
6
7
8
9
constexpr double TAX = 0.065;
constexpr double HIGHWAY_FUND = 2.0;
const string TRUCK = "TRUCK";  // <--- Defined, but never used.

int year{}, weight{};
string vin, make, model, type;
double basefee{};
int age{};
double discount{};

In line 6 using "VIN" gives the impression that it is a constant variable that can not be changed. It is a bit misleading, but OK if you want to leave it that way.

As I have said before you define the file streams and open the files, but how do you know that they are open and ready to use?

The while loop will not work the way you are think it will.

Line 24 reads the file, but is doing it wrong. How you set up the variables to read the fields in the file needs to match the way the file is laid out.

Line 25 is trying to read a field in the file that only exists if the "type" is a TRUCK. So this line is trying to read a weight, but the file pointer is at the beginning of the next line ready to read the next vin number, which starts with a letter, causing the stream to fail because it is expecting a number.

Given the line: age = (2020 - year); and as whitenite1 has pointed out you are trying to subtract a string from a number and this does not work. You would have to useage = (2020 - std::stoi(year)); to make this work. Or just make year an "int" to start with.

Line 27 should not be there. If a weight read from the file is above 12000 to qualify for the 22% additional charge then setting the weight to 12000 would eliminate this charge.

All the if statements need some work.

I do like this: basefee = (100 - ((100.0 * TAX) - (100 * discount)) + HIGHWAY_FUND);. Start with a base fee and then subtract the tax. Should you not add the tax before subtracting the discount.

NOTE: you only read the weight if the "type" is "TRUCK", hint hint nudge nudge.

The first thing you need to work on is getting the program to read the file properly. Until then the rest of the program is pointless because yo will not have the correct information to work with.

Writing as with debugging should be done in steps not all at once.

Andy
Consider:

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

using namespace std;

const int THIS_YEAR {2020};
const double highwayfund {2};

int main()
{
	ifstream inFile("VehicleInput.txt");
	ofstream outFile("VehicleOutput.txt");

	if (!inFile || !outFile)
		return (cout << "Cannot open files\n"), 1;

	string VIN, make, model, type;
	int year {0};

	while (inFile >> VIN >> make >> model >> year >> type) {
		const int age {THIS_YEAR - year};
		const double discount {(age < 7) ? 0.1 * age : 0.7};

		double basefee {0};

		outFile << VIN << " " << make << " " << model << " " << year << " " << type;

		if ((type == "CAR") || (type == "SUV"))
			basefee = 100;
		else
			if (type == "BUS")
				basefee = 200;
		else
			if (type == "TRUCK") {
				double weight {};

				inFile >> weight;
				outFile << " " << weight;

				basefee = (weight <= 12000) ? 500 : 500 * 1.22;
			}

		const double tax {basefee * .065};
		const double FEE {basefee + tax + highwayfund};

		outFile << " $" << FEE << '\n';
	}
}

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
59
60
61
62
63
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;
    
    string VIN, make, model, type;
    
    int year{0};
    int THIS_YEAR = 2020;
    int age{0};
    
    double basefee{0}, weight{0};
    double tax{0};
    double highwayfund{2};
    double discount{0};
    
    double FEE{0};
    
    inFile.open("VehicleInput.txt");
    outFile.open("VehicleOutput.txt");
    
    while (inFile >> VIN >> make >> model >> year >> type)
    {
        weight = 0;
        
        outFile << VIN << " " << make << " " << model << " " << year << " " << type;
        
        age = THIS_YEAR - year;
        if (age <= 7)
            discount = 0.1 * age;
        else
            discount = 0.7;
        
        if ((type == "SUV") || (type == "CAR"))
            basefee = 100;
        
        if (type == "BUS")
            basefee = 200;
        
        if (type == "TRUCK")
        {
            inFile >> weight;
            outFile << " "  << weight ;
            
            if(weight <= 12000)
                basefee = 500;
            else
                basefee = 500 * 1.22;
        }
        
        tax = basefee * .065;
        FEE = basefee + tax + highwayfund;
        
        outFile << " $" << FEE << endl;
    }
    return 0;
}
@seeplus and againtry,

Read the instructions! The while must use the ".eof" function.
Didn't re-read the previous post and forget that the 'professor' is trying to teach bad habits.

OK. Using while and .eof() if you really insist:

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

using namespace std;

const int THIS_YEAR {2020};
const double highwayfund {2};

int main()
{
	ifstream inFile("VehicleInput.txt");
	ofstream outFile("VehicleOutput.txt");

	if (!inFile || !outFile)
		return (cout << "Cannot open files\n"), 1;

	string VIN, make, model, type;
	int year {0};

	while (!inFile.eof())
		if (inFile >> VIN >> make >> model >> year >> type) {
			const int age {THIS_YEAR - year};
			const double discount {(age < 7) ? 0.1 * age : 0.7};

			double basefee {0};

			outFile << VIN << " " << make << " " << model << " " << year << " " << type;

			if ((type == "CAR") || (type == "SUV"))
				basefee = 100;
			else
				if (type == "BUS")
					basefee = 200;
				else
					if (type == "TRUCK") {
						double weight {};

						inFile >> weight;
						outFile << " " << weight;

						basefee = (weight <= 12000) ? 500 : 500 * 1.22;
					}

			const double tax {basefee * .065};
			const double FEE {basefee + tax + highwayfund};

			outFile << " $" << FEE << '\n';
		}
}


Giving an output file:


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

Last edited on
@seeplus,

If you have not followed all 3 posts on the same program you have missed a lot.

When I worked on the code from this OP I got this:

 AB54H77HG553DHJ8J8 TOYOTA CAMRY 2008 $ 38.50
 C745D4S78SSSWERDDF NISSAN PATHFINDER 2014 $ 48.50
 WAGGT345ADFGGGS234 ATLANTIC EXPRESS 2013 $ 329.00
 LKU338NGTH0988J77H KENWORTH T800 2009 20000 $ 709.50
 TNNH75RDG88J0R6669 FORD FOCUS 2005 20000 $ 38.50


It looks like you forgot to subtract the discount.

Andy

P.S. Still do not know what the proper output should be.
Last edited on
OK. Included discount. Don't know how I forget..... If there'd be some proper output to check against!

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

using namespace std;

const int THIS_YEAR {2020};
const double highwayfund {2};

int main()
{
	ifstream inFile("VehicleInput.txt");
	ofstream outFile("VehicleOutput.txt");

	if (!inFile || !outFile)
		return (cout << "Cannot open files\n"), 1;

	string VIN, make, model, type;
	int year {0};

	while (!inFile.eof())
		if (inFile >> VIN >> make >> model >> year >> type) {
			const int age {THIS_YEAR - year};
			const double discount {(age < 7) ? 0.1 * age : 0.7};

			double basefee {0};

			outFile << VIN << " " << make << " " << model << " " << year << " " << type;

			if ((type == "CAR") || (type == "SUV"))
				basefee = 100;
			else
				if (type == "BUS")
					basefee = 200;
				else
					if (type == "TRUCK") {
						double weight {};

						inFile >> weight;
						outFile << " " << weight;

						basefee = (weight <= 12000) ? 500 : 500 * 1.22;
					}

			basefee -= basefee * discount;

			const double tax {basefee * .065};
			const double FEE {basefee + tax + highwayfund};

			outFile << " $" << FEE << '\n';
		}
}




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


...which differs from @andy's output above - so at least one of us is wrong!
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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;
    
    string VIN, make, model, type;
    
    int year{0};
    int THIS_YEAR = 2020;
    int age{0};
    
    double basefee{0}, weight{0};
    double tax{0};
    double highwayfund{2};
    double discount{0};
    
    double FEE{0};
    
    inFile.open("VehicleInput.txt");
    outFile.open("VehicleOutput.txt");
    
    while (inFile >> VIN >> make >> model >> year >> type)
    {
        weight = 0;
        
        outFile << VIN << " " << make << " " << model << " " << year << " " << type;
        
        age = THIS_YEAR - year;
        if (age <= 7)
            discount = 0.1 * age;
        else
            discount = 0.7;
        
        if ((type == "CAR") || (type == "SUV"))
            basefee = 100;
        
        if (type == "BUS")
            basefee = 200;
        
        if (type == "TRUCK")
        {
            inFile >> weight;
            outFile << " "  << weight ;
            
            if(weight <= 12000)
                basefee = 500;
            else
                basefee = 500 * 1.22;
        }
        
        basefee *= 1 - discount;
        tax = basefee * .065;
        FEE = basefee + tax + highwayfund;
        
        outFile << " $" << FEE << endl;
    }
    return 0;
}



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


1. The discount was indeed not fully accounted for but now is.
2. The weight input requirement for trucks-alone meets the specification.
3. The eof 'issue' is trivial unless we need to have a 'just following orders debate'.
4. @OP has the basics sorted.
5. @OP needs to adapt to a functional approach as required.


Topic archived. No new replies allowed.