2012-09-23 216 views
1

請看看下面的代碼運行失敗

#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++的新手,並且自己學習它。請幫我糾正這段代碼,並運行它沒有問題。

回答

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本身擁有關於他分配的內存塊的信息;塊的大小;當前存儲的數據大小等。在需要時,它會擴展內存塊以允許您推送新值。所以,當你使用索引時,你只是用盡了分配的內存塊(在這種情況下)。無論如何將值添加到向量中都不是有效的方法,因此您必須使用匹配方法進行替換。請參閱link以獲得有關std :: vector方法的幫助。

+0

Votingdown應該有一個原因.. – PSIAlt

+0

親愛的用戶,請不要在這個帖子中投票給幫手。他們正在努力幫助,請不要讓他們失望。我正在通過投票進行反對票。謝謝 –

+0

感謝您的支持。人們忘記爲什麼[投下來](http://stackoverflow.com/privileges/vote-down)存在。那麼,push_back是否解決了問題? – PSIAlt

4

問題是

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

元素還不存在。您必須使用push_back來動態增加矢量的大小。

+0

非常感謝回覆。我真的很感激:) –