請看看下面的代碼運行失敗
#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個數據後,程序將終止給予錯誤
運行失敗(退出值1,總時間:5s)
我不明白爲什麼。我是C++的新手,並且自己學習它。請幫我糾正這段代碼,並運行它沒有問題。
Votingdown應該有一個原因.. – PSIAlt
親愛的用戶,請不要在這個帖子中投票給幫手。他們正在努力幫助,請不要讓他們失望。我正在通過投票進行反對票。謝謝 –
感謝您的支持。人們忘記爲什麼[投下來](http://stackoverflow.com/privileges/vote-down)存在。那麼,push_back是否解決了問題? – PSIAlt