1

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.

4

2 に答える 2

4

The problem is

hoursVector[vectorLocation] = hours;
chargeVector[vectorLocation] = charges;
carVector[vectorLocation] = carNumber++;

the elements don't exist yet. You have to use push_back to grow the size of the vector dynamically.

于 2012-09-23T17:58:06.610 に答える
1
hoursVector[vectorLocation] = hours;
chargeVector[vectorLocation] = charges;
carVector[vectorLocation] = carNumber++;
vectorLocation++;

It is invalid way to insert into vector. You should do something like this:

hoursVector.push_back( hours );
chargeVector.push_back( charges );
carVector.push_back( carNumber++ );

The std::vector itself holds info about memory block he allocated; size of block; size of data currently stored and so on. When needed, it is extending memory block to allow you to push new values. So when you use indexes you simply run out of allocated memory block (in this case). It is not valid way to to add values into vector anyway, so you must replace with matching methods. See link to get help about std::vector's methods.

于 2012-09-23T18:06:18.503 に答える