我要解決以下任務:MVS C++錯誤:串標超出範圍
有被賦予一個文本文件「pesel.txt」,其中包含150所國家認同。每行包含一個國家標識,這是一個11位數字編號。前兩位數字從左邊開始確定年份,一個人出生在哪一年,後兩位數字決定月份,下兩個決定日期。
爲了縮短:0-1 =年
數字 位2-3 =月 數字4-5 =天 位6-11 =確定別的東西,是什麼並不重要
我需要閱讀這個文件,檢查有多少人在十二月出生。我想這以下列方式:
- 讀取每一行直至到達文件末尾
- 在每一行我檢查字符串中的第三個字符是否等於1,如果第四個字符等於2,如果是我增加變量,這是我出生在十二月的人反,否則在下一個循環中執行
這裏是代碼:
int _tmain(int argc, _TCHAR* argv[])
{
ifstream file("C:\\Kuba\\Studia & Nauka\\MATURA XDDD
\\INFA\\1\\Dane_PR\\pesel.txt");
string line;
int bornInDecember=0;
if(!file.is_open()){
cout << "Cannot read the file." << endl ;
}else{
while(file.good()){
getline(file, line);
if( line[2] == '1' && line[3] == '2' ){
bornInDecember++ ; // 0-1 year, 2-3 month, 4-5 day
}
}
cout << "Amount of people born in december : "<< bornInDecember<< endl;
file.close();
}
system("pause");
return 0;
}
的問題是,我出現以下錯誤和我不知道爲什麼..
http://img10.imageshack.us/i/mvserr.png/
行是空的,或者您正在訪問不存在的數據。 – DumbCoder 2011-03-28 10:58:18