【问题标题】:Run fails without any exception运行失败,没有任何异常
【发布时间】:2012-09-15 07:20:33
【问题描述】:

请看下面的代码

#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;
    }
}

在这里,在while循环中给出1个数据后,程序终止给出错误

RUN FAILED(退出值1,总时间:5s)

我不明白为什么。我是 C++ 新手,我自己学习它。请帮助我更正此代码并毫无问题地运行它。

【问题讨论】:

    标签: c++ vector while-loop runtime-error


    【解决方案1】:

    问题是

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

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

    【讨论】:

    • 非常感谢您的回复。我真的很感激:)
    【解决方案2】:
    hoursVector[vectorLocation] = hours;
    chargeVector[vectorLocation] = charges;
    carVector[vectorLocation] = carNumber++;
    vectorLocation++;
    

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

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

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

    【讨论】:

    • 亲爱的用户,请不要在此线程中为帮助者投票。他们正在努力提供帮助,请不要让他们失去动力。我通过投反对票来取消反对票。谢谢
    • 感谢您的支持。人们忘记了为什么vote down 存在。那么,push_back 解决了没有?
    • 非常感谢您的帮助。对此,我真的非常感激 :)。是的,这个方法解决了问题!
    猜你喜欢
    • 2016-04-14
    • 2021-01-13
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2019-05-12
    • 2016-07-02
    • 2021-05-01
    相关资源
    最近更新 更多