std::out_of_range what(): basic_string::substr

std::out_of_range what(): basic_string::substr

本文关键字:basic string substr range out of std what      更新时间:2023-10-16

我想写一个赋值的程序,但是每当我运行它时,程序就会终止,显示:

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 :(nn";
    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.nn";
    output<<month<<" is the month t"<<day<<" is the day.t"<<year<<" is the year.tt";     
}
//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<<"nn";
    output<<"The American way-> "<<american<<"nn";
    output<<"The full way-----> "<<full<<"nnn";
}

如果输入的数据不正确,可能会出现错误。由于您没有检查正确的格式,那么请尝试正确输入日期。此外,您提取的数据字段也不正确。

尝试下面的函数定义

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.nn";
   output << month << " is the month t"<<day << " is the day.t" << year<<" is the year.tt";     
}