please have a look at the following code
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int static carNumber = 1; //Counts the car number
static int vectorLocation = 0; // used to get the vector location
double total=0; // total amount of charges
vector<double>hoursVector; //tracks each car's parkes hour
vector<double>chargeVector; //tracks each charge
vector<int>carVector; //tracks each car number
double calculateCharge(double numberOfHours);
void printData();
void insertIntoVector(double hours, double charges);
int main()
{
cout << "Start entering data. -1 to exit \n\n " << endl;
double numberOfHours=0;
while(true)
{
cout << "Enter Number Of Hours"<< endl;
cin >> numberOfHours;
if(numberOfHours==-1)
{
break;
}
else
{
total = total + calculateCharge(numberOfHours);
}
}
printData();
}
//This code will calculate the charge values
double calculateCharge(double numberOfHours)
{
double charge = 0;
double extraHours = 0;
double extraCharge = 0;
if(numberOfHours<=3)
{
charge = 2;
insertIntoVector( numberOfHours, charge);
}
else if(numberOfHours>3 && numberOfHours<24)
{
extraHours = numberOfHours-3;
extraCharge = extraHours * 0.5;
charge = 2+extraCharge;
insertIntoVector( numberOfHours, charge);
}
else if(numberOfHours==24)
{
charge = 10;
insertIntoVector( numberOfHours, charge);
}
else if(numberOfHours>24)
{
charge = 0;
insertIntoVector( numberOfHours, charge);
}
return charge;
}
//This code is used to enter data into vectors
void insertIntoVector(double hours, double charges)
{
hoursVector[vectorLocation] = hours;
chargeVector[vectorLocation] = charges;
carVector[vectorLocation] = carNumber++;
vectorLocation++;
carNumber++;
}
//This method is used to print data
void printData()
{
cout << "Car"<< setw(6)<< "Hours" << setw(6) << "Charge" << endl;
for(size_t i=0;i<hoursVector.size();i++)
{
cout << carVector[i] << setprecision(2) << fixed << setw(6) << hoursVector[i] << setw(6) << chargeVector[i] << endl;
}
}
In here, after giving 1 data inside the while loop, the program terminates giving the error
RUN FAILED (exit value 1, total time: 5s)
I can't understand why. I am new to C++ and learning it by my self. Please help me to correct this code and run it without an issue.