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

问题是

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

这些元素还不存在。您必须使用push_back动态增加向量的大小。

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

插入向量的方式无效。你应该这样做:

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

std::vector 本身包含有关他分配的内存块的信息;块的大小;当前存储的数据大小等。需要时,它会扩展内存块以允许您推送新值。因此,当您使用索引时,您只会用完分配的内存块(在这种情况下)。无论如何,将值添加到向量中都不是有效的方法,因此您必须使用匹配方法替换。请参阅链接以获取有关 std::vector 方法的帮助。

于 2012-09-23T18:06:18.503 回答