C++ 运行失败,没有任何异常
请看一下下面的代码C++ 运行失败,没有任何异常,c++,vector,while-loop,runtime-error,C++,Vector,While Loop,Runtime Error,请看一下下面的代码 #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 a
#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;
}
}
#包括
#包括
#包括
使用名称空间std;
int静态卡号=1//清点车号
静态int向量位置=0;//用于获取向量位置
双倍合计=0;//收费总额
向量小时向量//每小时追踪每辆车的帕克斯
向量向量//跟踪每一次充电
矢量器//追踪每个车号
双倍计算计费(双倍小时数);
void printData();
无效插入向量(双倍小时,双倍费用);
int main()
{
问题是
hoursVector[vectorLocation] = hours;
chargeVector[vectorLocation] = charges;
carVector[vectorLocation] = carNumber++;
元素还不存在。您必须使用push_back
动态增大向量的大小。问题是
hoursVector[vectorLocation] = hours;
chargeVector[vectorLocation] = charges;
carVector[vectorLocation] = carNumber++;
hoursVector[vectorLocation] = hours;
chargeVector[vectorLocation] = charges;
carVector[vectorLocation] = carNumber++;
vectorLocation++;
元素还不存在。您必须使用push_back
动态增大向量的大小
hoursVector[vectorLocation] = hours;
chargeVector[vectorLocation] = charges;
carVector[vectorLocation] = carNumber++;
vectorLocation++;
插入向量的方式无效。您应该执行以下操作:
hoursVector.push_back( hours );
chargeVector.push_back( charges );
carVector.push_back( carNumber++ );
vector本身保存有关他分配的内存块的信息;块的大小;当前存储的数据的大小等等。当需要时,它会扩展内存块以允许您推送新值。因此,当您使用索引时,您只需耗尽分配的内存块(在本例中)。无论如何,将值添加到vector中不是有效的方法,因此必须用匹配的方法替换。有关std::vector方法的帮助,请参阅
插入向量的方式无效。您应该执行以下操作:
hoursVector.push_back( hours );
chargeVector.push_back( charges );
carVector.push_back( carNumber++ );
vector本身保存有关他分配的内存块的信息;块的大小;当前存储的数据的大小等等。当需要时,它会扩展内存块以允许您推送新值。因此,当您使用索引时,您只需耗尽分配的内存块(在本例中)。无论如何,将值添加到vector中是无效的方法,因此您必须使用匹配方法进行替换。请参阅以获取有关std::vector方法的帮助。亲爱的用户,请不要对此线程中的帮助者投反对票。他们正试图提供帮助,请不要降低他们的积极性。我将通过放弃投票来取消反对票。谢谢您的支持rt.人们忘记了为什么存在。那么,推后解决了问题还是没有解决?非常感谢你的帮助。我真的很感激:)。是的,这种方法解决了问题!亲爱的用户,请不要投票否决此线程中的帮助者。他们正试图帮助,请不要降低他们的积极性。我通过放弃投票来取消否决票。谢谢或者支持。人们忘记了为什么存在。那么,推回解决了问题还是没有?非常感谢你的帮助。我真的很感激:)。是的,这种方法解决了问题!非常感谢你的回复。我真的很感激:)非常感谢你的回复。我真的很感激:)