How would you convert the following line to c++?
How does cin handle input like that? Reading in the integer is obvious but is there a way to tell cin to expect the "a=" before that?Code:scanf("a=%d",a);
This is a discussion on scanf in c++ within the C++ Programming forums, part of the General Programming Boards category; How would you convert the following line to c++? Code: scanf("a=%d",a); How does cin handle input like that? Reading in ...
How would you convert the following line to c++?
How does cin handle input like that? Reading in the integer is obvious but is there a way to tell cin to expect the "a=" before that?Code:scanf("a=%d",a);
You would have to use a different method, to do that in C++ - reading a string and splitting it would be the approach that would work best, I think. There are some boost functionality that allows you to use C-style format strings to parse strings and/or input, I think, but I'm no expert on boost.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Wow. Score one for C I suppose. Oh well. Thanks for the reply.
Sure, there's some things that scanf/printf do that are hard to do other ways. I personally have no objection to using for example sscanf() and sprintf() in conjunction with C++, but I do know that the more purist part of C++ programmers will say "you shouldn't do that", and suggest that you solve it some other way.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I do not think so. There would be Boost.Regex and Boost.Spirit, but both would likely be overkill here. There is Boost.Format too, but that is for formatting output in a type safe manner yet using format strings.Originally Posted by matsp
hmm... I did not think of this before, even though something similiar is quite prevalent in Boost.Spirit. Perhaps it would make for a proposal to add a new manipulator to the C++0x standard library?Originally Posted by brewbuck
EDIT:
Okay, something cobbled together from my inspection of MinGW port of g++ source:
Code:#include <string> #include <memory> #include <iostream> template<typename charT, typename traits = std::char_traits<charT>, typename allocator = std::allocator<charT> > class match_token_ { public: match_token_(const std::basic_string<charT, traits, allocator>& token) : token_(token) {} const std::basic_string<charT, traits, allocator>& token() const { return token_; } private: std::basic_string<charT, traits, allocator> token_; }; template<typename charT, typename traits, typename allocator> inline match_token_<charT, traits, allocator> match_token(const std::basic_string<charT, traits, allocator>& token) { return match_token_<charT, traits, allocator>(token); } template<typename charT> inline match_token_<charT> match_token(const charT* token) { return match_token_<charT>(std::basic_string<charT>(token)); } template<typename charT, typename traits, typename allocator> std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& in, match_token_<charT, traits, allocator> matcher) { typename std::basic_string<charT, traits, allocator>::const_iterator i = matcher.token().begin(), end = matcher.token().end(); typename std::basic_istream<charT, traits>::char_type c; while (i != end && in.get(c)) { if (*i == c) { ++i; } else { in.clear(std::basic_ios<charT, traits>::failbit); break; } } return in; } int main() { const std::string exclaim("!"); int a; if (std::cin >> match_token("a=") >> a >> match_token(exclaim)) { std::cout << "You entered: " << a << std::endl; } else { std::cerr << "Input error" << std::endl; } }
Last edited by laserlight; 04-06-2009 at 12:27 PM.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way