String not being added correctly in C++ -
i've spent 3 hours trying figure out searching google , other threads , haven't found solution working me.
i'm creating inventory program , i've run pretty unique problem. when run data files i'm given, program throws lexical_cast boost exception. know exact line throwing exception, doesn't make sense why exception being thrown.
if take data text file i'm given , copy , paste brand new text file, program runs fine. 100% functional. however, when run original data files, year string deleted when it's added.
std::cout << year << std::endl; //prints out year std::string date_str = ""; date_str += year; // <-- here's problem. //gets added empty string std::cout << date_str << " year should added" << std::endl; date_str += month; std::cout << date_str << " month added" << std::endl; date_str += date; std::cout << date_str << " date_str" << std::endl; std::cout << year << std::endl; //the following line throwing exception: boost::gregorian::date d = boost::gregorian::from_undelimited_string(date_str);
the print out is:
2010 year should added //notice space @ beginning year should be. 05 month added 0501 date_str 2010 terminate called after throwing instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >' what(): bad lexical cast: source type value not interpreted target abort (core dumped)
edit: there \r character @ end. added erase method call year , code works now.