2013-05-07 45 views
0

我想要做的是让用户输入一个密钥代码,如果它匹配显示数据输入文件的信息。我得到这个错误,我不知道为什么。std :: out_of_range在内存位置0x0043f7c4 ..使用向量和输入文件时出错

的std :: out_of_range内存位置0x0043f7c4 ..

下面是我的输入文件和我的代码

HEA,EMA,British Airways,030,025 
HEA,EMA,Thomas Cook Airlines,020,040 
HEA,DUB,British Airways,420,450 
HEA,DUB,Thomas Cook Airlines,400,550 
EMA,BAR,British Airways,120,140 
EMA,BAR,Thomas Cook Airlines,100,150 
ROM,EMA,British Airways, 120,125 
ROM,EMA,Thomas Cook Airlines,150,090 
ROM,BAR,British Airways,106,050 
ROM,BAR,Thomas Cook Airlines,100,080 
BAR,HEA,British Airways,125,090 
BAR,HEA,Thomas Cook Airlines,100,120 
DUB,EMA,Thomas Cook Airlines,450,380 
DUB,EMA,Thomas Cook Airlines,420,450 
下面

是我的C++代码

#include <vector> 
#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 

//#include "TravelFunctions.h" 




using namespace std; 



vector<string>flightDetails; 
vector<string>flightSearch; 
string airportDepart; 
string airportArrive; 
bool errorArrive = false; 
bool errorDepart = false; 
string timeTaken; 
int tax; 
int cost; 
bool needtoentercodes = false; 
string entry; 


int main() 
{ 
    // first i will read in the airport.txt data and store the information into a vector. 
ifstream flightdata("flightinfo.txt"); 
    string flightnextline; 
    if (!flightdata) 
    { 
     cout << "Cannot Open the file 'flightdata.txt'"; 
    } 
    else 
    { 
     while (flightdata.good()) 
     { 
      getline (flightdata, flightnextline); 
      flightDetails.push_back(flightnextline); 

     } 
     flightdata.close(); 

    } 







cout << " ___________________ " << endl; 
cout << "| Airport Key Code |" << endl; 
cout << "|EMA = East Midlands|" << endl; 
cout << "|HEA = Heathrow  |" << endl; 
cout << "|BAR = Barcelona |" << endl; 
cout << "|ROM = ROME   |" << endl; 
cout << "|DUB = DUBAI  |" << endl; 
cout << "|     |" << endl; 
cout << "|___________________|" << endl; 
cout << endl; 
cout << endl; 
cout << endl; 




entry = "Please enter the ID code of the starting destination.\n"; 

while (needtoentercodes == false) 
{ 

    cout<< entry; 
    cout << "Use the key above to see which airports are available. \n"; 
    string userdepartid; 
    cin>> userdepartid; 
    bool k = false; 

    //VALIDATING USER INPUT FOR DEPARTURE AIRPORT CODE - As mentioned above, this little section validates the starting departure id code. 
    while (k==false){ 
     if ((userdepartid != "HEA") && (userdepartid != "EMA") && (userdepartid != "DUB") && (userdepartid != "BAR") && (userdepartid != "ROM")) 
     { 
      cout << "You have entered an incorrect departure code.\nPlease Try Again...\n"; 
      cin >> userdepartid; 
     } 
     else 
     { 
      k=true; 
     } 
    } 

    cout << "\n"; 
    cout << "Please enter the code of the arrival destination.\n"; 
    string userarrivalid; 
    cin >> userarrivalid; 

    //VALIDATING USER INPUT FOR ARRIVAL AIRPORT CODE - This little section of code validates the arrival id airport code inputted in by the user. 
    bool a = false; 
     while (a==false){ 
     if ((userarrivalid != "HEA") && (userarrivalid != "EMA") && (userarrivalid != "ROM") && (userarrivalid != "DUB") && (userarrivalid != "BAR")) 
     { 
      cout << "You have entered an incorrect departure code.\nPlease Try Again...\n"; 
      cin >> userarrivalid; 
     } 
     else 
     { 
      a=true; 
     } 
    } 

    int j = 1; 
    bool resultsfound = false; 
    cout << "\n"; 



    //RETURN THE RESULTS AND PUT THE RESULTS IN AN ARRAY - This little section places the searched results in a unique vector which then can be used later on in the program. 
    for (int i=0; i < flightDetails.size(); i++) 
    { 
     string tempflightdata = flightDetails[i]; 
     string departid = tempflightdata.substr(0,3); 
     string arrivalid = tempflightdata.substr(4,3); 
     if ((departid == userdepartid) && (arrivalid == userarrivalid)) 
     { 
      cout << j << ":" << flightDetails[i] << "\n"; 
      flightSearch.push_back(flightDetails[i]); 
      j++; 
      needtoentercodes = false; 
     } 
     else 
     { 
      entry = "| Incorrect Entry. No direct connections! |\nPlease enter the ID code of the starting destination.\n";     
     } 
    } 
} 

} 
+0

也得到这个错误,真的让初学者困惑C++运行时检查失败#0 - 在函数调用中没有正确保存ESP的值。这通常是调用一个调用约定的函数声明的结果,其中函数指针声明的调用约定不同。 – user2359244 2013-05-07 18:36:54

回答

0

这是旧的,老问题

while (flightdata.good()) 
    { 
     getline (flightdata, flightnextline); 
     flightDetails.push_back(flightnextline); 
    } 
    flightdata.close(); 

应该是

while (getline (flightdata, flightnextline)) 
    { 
     flightDetails.push_back(flightnextline); 
    } 
    flightdata.close(); 

while循环去圆一个太多次,因为flightdata仍然good()甚至后你读过的最后一行,所以你最终会添加一个空行到您的flightDetails载体。稍后当您在空白行上执行tempflightdata.substr(4,3);时,会导致您看到的out_of_range错误。

警告,我只看过代码,没有调试它。

+0

嗯,我似乎仍然得到这个错误:S也中止() – user2359244 2013-05-07 18:44:20

+0

嗯,我明白,我想知道如何打印与机场相匹配的线? – user2359244 2013-05-07 18:46:14

+0

@ user2359244这是一个不同的问题。不要那么快地寻求帮助。想一想,并制定一个[sscce](http://sscce.org)。 – 2013-05-07 19:17:04