std::out_of_range what(): basic_string::substr
std::out_of_range what(): 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 :(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";
}
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- <string> 使用 for 循环写入向量