2016-03-01 81 views
0

我不確定哪個向量導致錯誤或問題來自何處。我試圖從一個文件輸入名字和生日,並將它們設置爲向量中的一部分。該文件是:我的程序返回錯誤「矢量下標超出範圍」。

Mark,12/21/1992 
Jay,9/29/1974 
Amy Lynn,3/17/2010 
Bill,12/18/1985 
Julie,7/10/1980 
Debbie,5/21/1976 
Paul,1/3/2001 
Ian,2/29/1980 
Josh,10/31/2003 
Karen,8/24/2011 

我甚至不知道如果我的代碼完成這個,因爲錯誤。我試圖閱讀更多的stringstream,但我不明白如何正確實現它。所提及的日期類可以在必要時提供,但時間很長。任何有關改進方案的意見以及爲什麼會發生這個問題,我們都非常感謝。 這裏是我的代碼:再次

#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
#include "c://cpp/classes/Date.cpp" 
using namespace std; 

struct person { 
    vector<string> name; // One name string vector 
    vector<Date> birthdate; // One birthdate vector 
    vector<Date> birthday; // birthday vector 
}; 

int main() { 
    string input, input2; // Two string inputs 
    const int PEOPLE_NUM = 10; // Amount of people 

    vector<person> People(PEOPLE_NUM); // Define vector called People with 10 positions 
    string test; 
    ifstream inputFile("C:/Users/Taaha/Documents/CMSC226/Project 3/Names.txt", ios::in); 
    for (int i = 0; i < PEOPLE_NUM; i++) { 
     getline(inputFile, input, ','); // input the line, stop when a comma 
     People[i].name[i] = input; // Add input into the vector 
     getline(inputFile, input2, ','); 
     People[i].birthdate[i] = input2; 
     cout << i; 
    } 
    inputFile.close(); // close file 

    Date birthday; 
    for (int i = 0; i < PEOPLE_NUM; i++) { 
     Date birthday(People[i].birthday[i].getDay(), People[i].birthday[i].getMonth(), Date().getYear()); 
     People[i].birthday[i] = birthday; 
    } // Not finished yet, but turns birthdate into birthday 
    return 0; 
} 

感謝:]

+0

偏題:而不是有'vector's的結構,而是考慮結構的vector。很可能會簡化您的代碼。 – user4581301

+0

您正在訪問尚不存在的索引(又名'vector.name [i]')。您必須使用'push_back()'或'emplace_back()'爲您想要放入矢量的東西創建插槽。另外,我幾乎可以肯定你在'struct person'中濫用向量。除非人們突然開始有不止一個生日,否則很可能你不想在「人」中出現媒介。 –

+0

好的,你認爲我得到的問題是從那裏來的嗎? – Cool

回答

2

People[i].name[i] = input;在for循環,People[i].name仍然是一個空載體,在其上調用operator[]將UB。你可能會提前resize,或使用push_back

for (int i = 0; i < PEOPLE_NUM; i++) { 
    getline(inputFile, input, ','); 
    People[i].name[i] = input;  // People[i].name is still empty here 
    getline(inputFile, input2, ','); 
    People[i].birthdate[i] = input2; // People[i].birthdate is still empty here 
    cout << i; 
} 

您可以使用push_back,比如,

for (int i = 0; i < PEOPLE_NUM; i++) { 
    getline(inputFile, input, ','); 
    People[i].name.push_back(input); 
    //   ~~~~~~~~~~ 
    getline(inputFile, input2, ','); 
    People[i].birthdate.push_back(input2); 
    //     ~~~~~~~~~~ 
    cout << i; 
} 

而且它同另一個for循環。

for (int i = 0; i < PEOPLE_NUM; i++) { 
    Date birthday(People[i].birthdate[i].getDay(), People[i].birthdate[i].getMonth(), Date().getYear()); // typo of birthdate? 
    //      ~~~~~~~~~      ~~~~~~~~~ 
    People[i].birthday.push_back(birthday); 
    //     ~~~~~~~~~~ 
} // Not finished yet, but turns birthdate into birthday 
+0

我剛剛嘗試過,不幸的是,我仍然收到1232行的錯誤(表達式:向量下標超出範圍)。我也收到錯誤「標準C++庫超出範圍」&& 1233行。&& – Cool

+0

請給我有一刻要修改我的代碼,謝謝。 – Cool

+0

@Cool第1232行的內容是什麼? – songyuanyao