2014-05-07 134 views
-3

我試圖寫一個程序可以轉讓,但每當我運行它,該程序被終止,他說:的std :: out_of_range什麼():basic_string的:: SUBSTR

terminate called after throwing an instance of 'std::out_of_range' 
what(): basic_string::substr 

這裏是我的代碼爲整個程序:

//Jaime Rhoda! Program 6! Sleep deprivation! 
//This program will read in a list of dates. 
//It will then seperate the months, days, and years. 
//It will print the dates in a variety of styles. 

#include <fstream> 
#include <string> 
using namespace std; 

ifstream input ("input.txt"); 
ofstream output ("output.txt"); 
string readoriginaldate(); 
void breakoriginaldate (string, string&, string&, string&); 
void printdate3ways (string,string,string); 
int main() { 
    output<<"This is Jaime! Welcome to program 6! I'm tired :(\n\n"; 
    while (input) { 
     string month,day,year; 
     string date=readoriginaldate(); 
     breakoriginaldate(date, month, day, year); 
     printdate3ways(month,day,year); 
    } 
    return 0; 
} 
// This function takes no parameter. 
//It reads in one character string from a file. 
//It returns the string. Nice and simple. 
string readoriginaldate() { 
    string date; 
    input>>date; 
    return date; 
} 
//This function reads in a string (date). 
//It seperates the string into 3 parts. 
//It returns no value. 
void breakoriginaldate (string date, string &day, string &month, string &year) { 
    int pos=date.find('/'); 
    day=date.substr(0,pos-1); 
    int pos2= date.find('/',pos+1); 
    month=date.substr(pos+1,pos2-pos); 
    year=date.substr(pos2,2); 
    output<<date<<" is the original date.\n\n"; 
    output<<month<<" is the month \t"<<day<<" is the day.\t"<<year<<" is the year.\t\t";  
} 
//This function takes the month, day, and year. 
//It prints them the American way, and all the other ways. 
//It returns no value, like a bad invesment! 
void printdate3ways(string month,string day,string year) { 
    string wordmonth; 
    if (month=="1") 
     wordmonth="January"; 
    if (month=="2") 
     wordmonth="February"; 
    if (month=="3") 
     wordmonth="March"; 
    if (month=="4") 
     wordmonth="April"; 
    if (month=="5") 
     wordmonth="May"; 
    if (month=="6") 
     wordmonth="June"; 
    if (month=="7") 
     wordmonth="July"; 
    if (month=="8") 
     wordmonth="August"; 
    if (month=="9") 
     wordmonth="September"; 
    if (month=="10") 
     wordmonth="October"; 
    if (month=="11") 
     wordmonth="November"; 
    if (month=="12") 
     wordmonth="December"; 
    string fullmonth, fullday; 
    if (day.length()<2) 
     fullday='0'+day; 
    else 
     fullday=day; 
    if (month.length()<2) 
     fullmonth='0'+month; 
    else 
     fullmonth=month; 

    string european=day+'-'+month+'-'+year; 
    string american=wordmonth+""+day+", "+"20"+year; 
    string full=fullmonth+'-'+fullday+'-'+"20"+year; 
    output<<"The European way-> "<<european<<"\n\n"; 
    output<<"The American way-> "<<american<<"\n\n"; 
    output<<"The full way-----> "<<full<<"\n\n\n"; 
} 
+2

試着去了解什麼異常,告訴您:您訪問的東西是不是這裏的std :: out_of_range identicates; basic_string :: substr告訴你這是你的一個子串調用。因此,開始查看這些指數。從長遠來看,調試是一項很好的技能,可以幫助您。當你發現你的錯誤時,你可以給你的帖子寫一個答案,並解釋你做錯了什麼。這進一步增加了您的學習體驗。 – MatthiasB

回答

-1

如果您不正確地輸入數據可能會發生錯誤。由於您沒有檢查正確的格式,請嘗試正確輸入日期。你也提取數據的字段也是錯誤的。

試試下面的函數定義

void breakoriginaldate (const string &date, string &day, string &month, string &year) 
{ 
    string::size_type pos = date.find('/'); 
    day = date.substr(0, pos); 
    string::size_type pos2 = date.find('/', pos + 1); 
    month = date.substr(pos + 1, pos2 - pos - 1); 
    year = date.substr(pos2 + 1); 

    output << date << " is the original date.\n\n"; 
    output << month << " is the month \t"<<day << " is the day.\t" << year<<" is the year.\t\t";  
} 
相關問題