我正在尝试为作业编写程序,但每当我运行它时,程序就会终止,并说:
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";
}
答案 0 :(得分:-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";
}