Like Tree1Likes
  • 1 Post By laserlight

scanf in c++

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 ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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
    22,256
    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 01:27 PM.
    whiteflags likes this.
    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

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, 08:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 07: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, 05:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21