All, I wish to have string (combination of string value and int value) which increments until 100. The code is as below - --- #include <iostream> #include <cstdlib> #include <string> #include <boost/lexical_cast.hpp> using namespace std; int main() { std::string TID = "Apple"; int TagID = 01, Value = 000 /* 100 IDs*/; std::string TagName, TagInitial, TagUId; TagName = TID + boost::lexical_cast<std::string>(TagID); // Should be as -> Apple01 TagInitial = TagName + boost::lexical_cast<std::string>(Value); // Should be as -> Apple01000 int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be as -> Apple01001 TagUId = boost::lexical_cast<std::string>(i); std::cout << "Tag ID .." << TagUId << endl; } I get below error message - -- terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>
' what(): bad lexical cast: source type value could not be interpreted as target Aborted (core dumped) --
Any clue? ~ Thanks!!
2013/11/2 Rahul Mathur <srivmuk@gmail.com>
All,
I wish to have string (combination of string value and int value) which increments until 100. The code is as below -
--- #include <iostream> #include <cstdlib> #include <string> #include <boost/lexical_cast.hpp>
using namespace std;
int main() { std::string TID = "Apple"; int TagID = 01, Value = 000 /* 100 IDs*/; std::string TagName, TagInitial, TagUId; TagName = TID + boost::lexical_cast<std::string>(TagID); // Should be as -> Apple01 TagInitial = TagName + boost::lexical_cast<std::string>(Value); // Should be as -> Apple01000 int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be as -> Apple01001
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I think this is throwing the exception, the cast seems invalid
TagUId = boost::lexical_cast<std::string>(i); std::cout << "Tag ID .." << TagUId << endl; }
I get below error message -
-- terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>
' what(): bad lexical cast: source type value could not be interpreted as target Aborted (core dumped) --
Any clue?
~ Thanks!!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Daniel D'un coté s'étend le bien, de l'autre, le vide de la vie quotidienne.* (F.Alberoni)*
This given link - http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast/performance... string->int conversion is possible. On Sat, Nov 2, 2013 at 3:43 PM, Daniel Anderson <wonixen@gmail.com> wrote:
2013/11/2 Rahul Mathur <srivmuk@gmail.com>
All,
I wish to have string (combination of string value and int value) which increments until 100. The code is as below -
--- #include <iostream> #include <cstdlib> #include <string> #include <boost/lexical_cast.hpp>
using namespace std;
int main() { std::string TID = "Apple"; int TagID = 01, Value = 000 /* 100 IDs*/; std::string TagName, TagInitial, TagUId; TagName = TID + boost::lexical_cast<std::string>(TagID); // Should be as -> Apple01 TagInitial = TagName + boost::lexical_cast<std::string>(Value); // Should be as -> Apple01000 int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be as -> Apple01001
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I think this is throwing the exception, the cast seems invalid
TagUId = boost::lexical_cast<std::string>(i); std::cout << "Tag ID .." << TagUId << endl; }
I get below error message -
-- terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>
' what(): bad lexical cast: source type value could not be interpreted as target Aborted (core dumped) --
Any clue?
~ Thanks!!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Daniel
D'un coté s'étend le bien, de l'autre, le vide de la vie quotidienne.* (F.Alberoni)*
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
You can't convert Apple01000 to an int. Cast from string to int is possible, but the string must contain a valid integer. A On Nov 2, 2013 7:20 AM, "Rahul Mathur" <srivmuk@gmail.com> wrote:
This given link - http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast/performance... string->int conversion is possible.
On Sat, Nov 2, 2013 at 3:43 PM, Daniel Anderson <wonixen@gmail.com> wrote:
2013/11/2 Rahul Mathur <srivmuk@gmail.com>
All,
I wish to have string (combination of string value and int value) which increments until 100. The code is as below -
--- #include <iostream> #include <cstdlib> #include <string> #include <boost/lexical_cast.hpp>
using namespace std;
int main() { std::string TID = "Apple"; int TagID = 01, Value = 000 /* 100 IDs*/; std::string TagName, TagInitial, TagUId; TagName = TID + boost::lexical_cast<std::string>(TagID); // Should be as -> Apple01 TagInitial = TagName + boost::lexical_cast<std::string>(Value); // Should be as -> Apple01000 int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be as -> Apple01001
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I think this is throwing the exception, the cast seems invalid
TagUId = boost::lexical_cast<std::string>(i); std::cout << "Tag ID .." << TagUId << endl; }
I get below error message -
-- terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>
' what(): bad lexical cast: source type value could not be interpreted as target Aborted (core dumped) --
Any clue?
~ Thanks!!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Daniel
D'un coté s'étend le bien, de l'autre, le vide de la vie quotidienne.* (F.Alberoni)*
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
YES, the string should contain a valid integer. Thanks all ..!! BTW.." I have a sinerio as - 1. One string as "Apple" 2. Another string or int value as "01" or 1. 3. Final third being a string or int value as "000" or 1. CASE:A I have to print as -> Apple01001 with the last three digits from right being incremented when needed e.g Apple01001, Apple01002, Apple01003 and so on.. CASE:B Also, the fourth and fifth digit from right being incremented when needed, alongwith last three digits from right as - Apple02001, Apple02002, Apple02003 and so on .. Thought boost::lexical_cast would have done as referred from link - http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast/performance... Any approach? On Sat, Nov 2, 2013 at 5:22 PM, Alain Leblanc <aalebl@gmail.com> wrote:
You can't convert Apple01000 to an int. Cast from string to int is possible, but the string must contain a valid integer.
A On Nov 2, 2013 7:20 AM, "Rahul Mathur" <srivmuk@gmail.com> wrote:
This given link - http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast/performance... string->int conversion is possible.
On Sat, Nov 2, 2013 at 3:43 PM, Daniel Anderson <wonixen@gmail.com>wrote:
2013/11/2 Rahul Mathur <srivmuk@gmail.com>
All,
I wish to have string (combination of string value and int value) which increments until 100. The code is as below -
--- #include <iostream> #include <cstdlib> #include <string> #include <boost/lexical_cast.hpp>
using namespace std;
int main() { std::string TID = "Apple"; int TagID = 01, Value = 000 /* 100 IDs*/; std::string TagName, TagInitial, TagUId; TagName = TID + boost::lexical_cast<std::string>(TagID); // Should be as -> Apple01 TagInitial = TagName + boost::lexical_cast<std::string>(Value); // Should be as -> Apple01000 int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be as -> Apple01001
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I think this is throwing the exception, the cast seems invalid
TagUId = boost::lexical_cast<std::string>(i); std::cout << "Tag ID .." << TagUId << endl; }
I get below error message -
-- terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>
' what(): bad lexical cast: source type value could not be interpreted as target Aborted (core dumped) --
Any clue?
~ Thanks!!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Daniel
D'un coté s'étend le bien, de l'autre, le vide de la vie quotidienne.* (F.Alberoni)*
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 3/11/2013 01:12, Quoth Rahul Mathur:
YES, the string should contain a valid integer. Thanks all ..!!
Actually the string must consist entirely of a valid integer, not merely contain one. Any non-numeric characters are invalid.
1. One string as "Apple" 2. Another string or int value as "01" or 1. 3. Final third being a string or int value as "000" or 1.
CASE:A I have to print as -> Apple01001 with the last three digits from right being incremented when needed e.g Apple01001, Apple01002, Apple01003 and so on..
CASE:B Also, the fourth and fifth digit from right being incremented when needed, alongwith last three digits from right as - Apple02001, Apple02002, Apple02003 and so on ..
If you already have the components broken out like that, then it is trivial to convert "001" to 1 via lexical_cast, and then do whatever operations you want on each component separately. (If you don't have them broken out, then you will need to use some kind of parser to break them out into components first.) You can't use lexical_cast to go the other way and produce "001" from 1 -- it will produce only "1", and you would have to use something else to insert the leading zeroes that you desire. You might want to look at Boost.Format, eg: (boost::format("%1%%|2$02|%|3$03|") % tid % tagId % value).str()
Try this it should work - #include <iostream> #include <cstdlib> #include <string> #include <cstdio> #include <boost/lexical_cast.hpp> using namespace std; int main() { std::string APPLE = "Apple", AppleID = "01", APPLEID, FINAL_PRINT; int age = 1; APPLEID = APPLE + boost::lexical_cast<std::string>(AppleID); std::cout << "APPLEID .." << APPLEID << endl; std::string age_s = boost::lexical_cast<std::string>(age); string dest = std::string( 5, '0').append(age_s); FINAL_PRINT = APPLEID + boost::lexical_cast<std::string>(dest); std::cout << "FINAL PRINT .." << FINAL_PRINT << endl; } Good luck !! On Mon, Nov 4, 2013 at 6:13 AM, Gavin Lambert <gavinl@compacsort.com> wrote:
On 3/11/2013 01:12, Quoth Rahul Mathur:
YES, the string should contain a valid integer. Thanks all ..!!
Actually the string must consist entirely of a valid integer, not merely contain one. Any non-numeric characters are invalid.
1. One string as "Apple"
2. Another string or int value as "01" or 1. 3. Final third being a string or int value as "000" or 1.
CASE:A I have to print as -> Apple01001 with the last three digits from right being incremented when needed e.g Apple01001, Apple01002, Apple01003 and so on..
CASE:B Also, the fourth and fifth digit from right being incremented when needed, alongwith last three digits from right as - Apple02001, Apple02002, Apple02003 and so on ..
If you already have the components broken out like that, then it is trivial to convert "001" to 1 via lexical_cast, and then do whatever operations you want on each component separately. (If you don't have them broken out, then you will need to use some kind of parser to break them out into components first.)
You can't use lexical_cast to go the other way and produce "001" from 1 -- it will produce only "1", and you would have to use something else to insert the leading zeroes that you desire.
You might want to look at Boost.Format, eg:
(boost::format("%1%%|2$02|%|3$03|") % tid % tagId % value).str()
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
thanks Gavin for valuable inputs. Good day. On Mon, Nov 4, 2013 at 6:13 AM, Gavin Lambert <gavinl@compacsort.com> wrote:
On 3/11/2013 01:12, Quoth Rahul Mathur:
YES, the string should contain a valid integer. Thanks all ..!!
Actually the string must consist entirely of a valid integer, not merely contain one. Any non-numeric characters are invalid.
1. One string as "Apple"
2. Another string or int value as "01" or 1. 3. Final third being a string or int value as "000" or 1.
CASE:A I have to print as -> Apple01001 with the last three digits from right being incremented when needed e.g Apple01001, Apple01002, Apple01003 and so on..
CASE:B Also, the fourth and fifth digit from right being incremented when needed, alongwith last three digits from right as - Apple02001, Apple02002, Apple02003 and so on ..
If you already have the components broken out like that, then it is trivial to convert "001" to 1 via lexical_cast, and then do whatever operations you want on each component separately. (If you don't have them broken out, then you will need to use some kind of parser to break them out into components first.)
You can't use lexical_cast to go the other way and produce "001" from 1 -- it will produce only "1", and you would have to use something else to insert the leading zeroes that you desire.
You might want to look at Boost.Format, eg:
(boost::format("%1%%|2$02|%|3$03|") % tid % tagId % value).str()
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
only when string look like an int 2013/11/2 Rahul Mathur <srivmuk@gmail.com>
This given link - http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast/performance... string->int conversion is possible.
On Sat, Nov 2, 2013 at 3:43 PM, Daniel Anderson <wonixen@gmail.com> wrote:
2013/11/2 Rahul Mathur <srivmuk@gmail.com>
All,
I wish to have string (combination of string value and int value) which increments until 100. The code is as below -
--- #include <iostream> #include <cstdlib> #include <string> #include <boost/lexical_cast.hpp>
using namespace std;
int main() { std::string TID = "Apple"; int TagID = 01, Value = 000 /* 100 IDs*/; std::string TagName, TagInitial, TagUId; TagName = TID + boost::lexical_cast<std::string>(TagID); // Should be as -> Apple01 TagInitial = TagName + boost::lexical_cast<std::string>(Value); // Should be as -> Apple01000 int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be as -> Apple01001
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I think this is throwing the exception, the cast seems invalid
TagUId = boost::lexical_cast<std::string>(i); std::cout << "Tag ID .." << TagUId << endl; }
I get below error message -
-- terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>
' what(): bad lexical cast: source type value could not be interpreted as target Aborted (core dumped) --
Any clue?
~ Thanks!!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Daniel
D'un coté s'étend le bien, de l'autre, le vide de la vie quotidienne.* (F.Alberoni)*
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Daniel D'un coté s'étend le bien, de l'autre, le vide de la vie quotidienne.* (F.Alberoni)*
Rahul Mathur <srivmuk <at> gmail.com> writes:
TagName = TID + boost::lexical_cast<std::string>(TagID); // Should be as - Apple01
TagInitial = TagName + boost::lexical_cast<std::string>(Value); // Should be as -> Apple01000
int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be as ->
Apple01001
TagUId = boost::lexical_cast<std::string>(i);
You cannot cast "Apple01000" to int. Try this: // Should be as -> Apple01001 TagUId = TagName + boost::lexical_cast<std::string>(++Value);
Thanks All. It worked finally. On Mon, Nov 4, 2013 at 4:57 PM, Marcel Raad <raad@teamviewer.com> wrote:
Rahul Mathur <srivmuk <at> gmail.com> writes:
Apple01
TagName = TID + boost::lexical_cast<std::string>(TagID); // Should be as
TagInitial = TagName + boost::lexical_cast<std::string>(Value); // Should be as -> Apple01000
int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be as ->
Apple01001
TagUId = boost::lexical_cast<std::string>(i);
You cannot cast "Apple01000" to int. Try this:
// Should be as -> Apple01001
TagUId = TagName + boost::lexical_cast<std::string>(++Value);
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 5 November 2013 03:55, Rahul Mathur <srivmuk@gmail.com> wrote:
Thanks All. It worked finally.
Forgive I dare, but IMHO the most important thing is you understand why it didn't work in the first place. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net
participants (7)
-
Alain Leblanc
-
Daniel Anderson
-
Gavin Lambert
-
Marcel Raad
-
Mateusz Loskot
-
Mukkaysh Srivastav
-
Rahul Mathur