Function not operating

My function for the "getbasefees" is not running correctly. When I run my program the only output is the else statement, or "200". What do I need to fix to get it working properly?

This is the VehicleInput.txt

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

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
#include <fstream>
#include<string>
#include <sstream>
#include <cmath>
#define CURRENTYEAR 2021
#define HIGHWAYTAX 2
using namespace std;
int getYearDifference(int currentyear,int year){
   return currentyear-year;
}
int getreductionpercentage(int currentyear,int year){
   return 10*(currentyear-year);
}
int getbasefees(string type){
   if(type == "TRUCK"){
       return 500;
   }
   else if(type == "CAR" || type == "SUV"){
       return 100;
       
   }
   else
       return 200;
}
int main(){
   ifstream inFile("VehicleInput.txt");
   ofstream outFile("VehicleOutput.txt");
   outFile<<"Type of Vehicle, Base Fee, New base fee with surcharge, Percent reduction, Base fees after reduction, Tax, Highway fee, Total Base fee\n";
    
    string str;
    while (getline(inFile, str))
    {
        stringstream check1(str);
        string token;
        string lineargs[6];
        int index=0;
        int basefees;
        int surcharge;
        while(getline(check1, token, ' '))
        {
           cout<<token<<" ";
           lineargs[index]=token;
           index++;
        }
        cout<<" "<<endl;
        outFile<<lineargs[0]<<",";
        basefees=getbasefees(lineargs[0]);
           outFile<< "Base fee: " << basefees<<",";
            cout<<"Base fee: "<<basefees<<endl;
               if(basefees==500){
                   stringstream con(lineargs[5]);
                   int weight = 0;
                   con >> weight;
                       if(weight>1200){
                           surcharge = (500*22)/100 + basefees;
                           outFile << surcharge;
                           cout << "Base fee after surcharge: " << surcharge << endl;
                           
                       }
                       else
                           cout << "NO Surcharge" << endl;
               }
  
      
           stringstream con(lineargs[3]);
           int year = 0;
           con >> year;
           if(getYearDifference(CURRENTYEAR,year) >= 7){
               int reducedFees= 70;
               outFile<<"Percent reduction: " << reducedFees<<",";
               cout<<"Percent reduction: "<<reducedFees << "% "<<endl;
               basefees=(basefees*(100-reducedFees))/100;
               outFile<<basefees<<",";
               cout<<"Reduced base fee: "<<basefees<<endl;
           }
           else if(getYearDifference(CURRENTYEAR,year)>=1){
               int reducedFees=getreductionpercentage(CURRENTYEAR,year);
               outFile<<"Percent reduction:" << reducedFees<< ",";
               cout<<"Percent reduction: "<<reducedFees<<"% "<<(CURRENTYEAR-year)<<endl;
               basefees=(basefees*(100-reducedFees))/100;
               outFile<<"Reduced base fee" << basefees<<",";
               cout<<"Reduced base fee: "<<basefees<<endl;
           }
           else{
               outFile<<"NA, NA,";
               cout<<"NO reduction"<<endl;
           }
           float tax=(basefees*6.5)/100;
           cout<<"Tax: "<<tax<<endl;
           cout<<"Highway Tax: "<<HIGHWAYTAX<<endl;
           basefees=basefees+tax+HIGHWAYTAX;
               outFile<<"Tax: "<<tax<<",";
               outFile<<"Highway Tax: " <<HIGHWAYTAX<<",";
               outFile<<"Total base fee: "<<basefees<<",\n";
           cout<<"Total base fee: "<<basefees<<endl;
           cout<<"--------------------------------------------------"<<endl;
    }
       outFile.close();
}
Time to run your program in the debugger and see, step by step, what is actually going on ;-)

https://en.wikipedia.org/wiki/Debugger

This will help you to figure out where things don't go as expected, so you can fix it or ask the right question.

(Guessing, or hoping that other people will do your work, probably won't give you much insight)
Last edited on
Learning to use a debugger is certainly a good idea.
What IDE do you use ?

When you set a breakpoint at line 16 you will see that type has the value "AB54H77HG553DHJ8J8", no wonder that you get wrong result.
basefees=getbasefees(lineargs[0]);

Well, from your input file, lineargs[0] appears to be the engine number or something like that. It certainly isn't "TRUCK" or "CAR".

Did you mean lineargs[4] there?
Your indexing for linargs doesn't match the file layout. TypeOfVehicle is 4, not 0
Topic archived. No new replies allowed.