Thread: scanf in c++

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    127

    scanf in c++

    How would you convert the following line to c++?

    Code:
    scanf("a=%d",a);
    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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    Wow. Score one for C I suppose. Oh well. Thanks for the reply.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by herWter View Post
    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.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    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
    I use sscanf() in C++ without remorse. iostreams would be more useful if it had the ability to match constant portions of the input.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by matsp
    There are some boost functionality that allows you to use C-style format strings to parse strings and/or input
    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.

    Quote Originally Posted by brewbuck
    iostreams would be more useful if it had the ability to match constant portions of the input.
    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?

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM